DOC PREVIEW
UNF COP 2551 - Data and Expressions

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 2Data and ExpressionsOutlineCharacter StringsThe println MethodThe print MethodString ConcatenationSlide 8PowerPoint PresentationOverloading OperatorsEscape SequencesSlide 12Public Directory for Roses.javaOutput from Roses:Slide 15VariablesVariable InitializationGeometry example…Page 68AssignmentConstantsSlide 21Slide 22Primitive DataNumeric Primitive DataSlide 25CharactersCharacter SetsSlide 28BooleanSlide 30Chapter 2Data and ExpressionsPart One© 2004 Pearson Addison-Wesley. All rights reserved 2-2/29Data and Expressions•Let's explore some other fundamental programming concepts•Chapter 2 focuses on:character stringsprimitive datathe declaration and use of variablesexpressions and operator precedencedata conversionsaccepting input from the user© 2004 Pearson Addison-Wesley. All rights reserved 2-3/29OutlineCharacter StringsVariables and AssignmentPrimitive Data TypesExpressionsData ConversionInteractive Programs© 2004 Pearson Addison-Wesley. All rights reserved 2-4/29Character Strings•A string of characters can be represented as a string literal by putting double quotes around the text:•Examples:"This is a string literal.""123 Main Street""X"•Every character string or simply ‘string’ is an object in Java, defined by the String classBecause a string is an object, it will have ‘properties’ and ‘methods’ associated with it.Because this is already defined in Java for us, we ‘inherit’ the properties and methods provided to us.Again, JAVA has defined a String class within the language! So when we create objects of type String, we are supplied with many properties and methods for us! What a deal!!!•Every string literal represents a String object© 2004 Pearson Addison-Wesley. All rights reserved 2-5/29The println Method•In the Lincoln program from Chapter 1, we invoked the println method in an object to print a character string•The method, println, inside the System.out object will send output to the monitor / screen System.out.println ("Whatever you are, be a good one.");objectmethodnameinformation provided to the method(parameters)Note: the syntax requires parentheses and a String. Format: objectName.methodName …. ending with a semicolon. ALL statements in Java end in a semicolon!© 2004 Pearson Addison-Wesley. All rights reserved 2-6/29The print Method•The System.out object provides another service (method) as well.•The print method is similar to the println method, except that it does not advance to the next line once the String is displayed.•Therefore anything printed after a print statement will appear on the same line•See Countdown.java© 2004 Pearson Addison-Wesley. All rights reserved 2-7/29String Concatenation•The string concatenation operator (+) is used to append one string to the end of another"Peanut butter " + "and jelly"•It can also be used to append a number to a string•A string literal cannot be broken across two lines in a program•See Facts.java •Specific example coming up…© 2004 Pearson Addison-Wesley. All rights reserved 2-8/29String Concatenation•The + operator is also used for arithmetic addition•The function it performs depends on the type of the information on which it operates If both operands are strings, or if one is a string and one is a number, it performs string concatenation•If both operands are numeric, it adds themThis is called ‘operator overloading.’•The + operator is evaluated left to right (associativity)• but parentheses can be used to force the order (later)•See Addition.java •But first, let‘s look at some code:© 2004 Pearson Addison-Wesley. All rights reserved 2-9/29•//********************************************************************•// Facts.java Author: Lewis/Loftus•//•// Demonstrates the use of the string concatenation operator and the•// automatic conversion of an integer to a string.•//********************************************************************•public class Facts•{• //-----------------------------------------------------------------• // Prints various facts.• //-----------------------------------------------------------------• public static void main (String[] args)• {• // Strings can be concatenated into one long string• System.out.println ("We present the following facts for your "• + "extracurricular edification:");• System.out.println ();• // A string can contain numeric digits• System.out.println ("Letters in the Hawaiian alphabet: 12");• // A numeric value can be concatenated to a string• System.out.println ("Dialing code for Antarctica: " + 672);• System.out.println ("Year in which Leonardo da Vinci invented "• + "the parachute: " + 1515);• System.out.println ("Speed of ketchup: " + 40 + " km per year");• } // end main•} // end class Facts•Everything prints out in a straight line. See page 61 in textbook….© 2004 Pearson Addison-Wesley. All rights reserved 2-10/29Overloading Operators•Operators can be ‘overloaded’•Have different meanings depending on context.•“+” can mean concatenation or addition depending….•Remember the + operator ‘associates’ left to right AND•Remember parentheses always override the ‘normal’ hierarchical evaluation (later…)© 2004 Pearson Addison-Wesley. All rights reserved 2-11/29Escape Sequences•What if we wanted to print a the quote character?•The following line would confuse the compiler because it would interpret the second quote as the end of the stringSystem.out.println ("I said "Hello" to you.");•An escape sequence is a series of characters that represents a special character•An escape sequence begins with a backslash character (\) which indicates that the character(s) that follow should be treated in a special waySystem.out.println ("I said \"Hello\" to you.");© 2004 Pearson Addison-Wesley. All rights reserved 2-12/29Escape Sequences•Some Java escape sequences:•Make sure you understand these, especially \n, \t, \” and maybe a couple of others…•See Roses.java (page 64)Escape Sequence\b\t\n\r\"\'\\Meaningbackspacetabnewlinecarriage returndouble quotesingle quotebackslash© 2004 Pearson Addison-Wesley. All rights reserved 2-13/29Public Directory for Roses.java•//********************************************************************•// Roses.java Author: Lewis/Loftus•//•// Demonstrates the use of escape


View Full Document

UNF COP 2551 - Data and Expressions

Download Data and Expressions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Data and Expressions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data and Expressions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?