DOC PREVIEW
UNF COP 2551 - Lecture Notes

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 8Slide 9Overloading OperatorsEscape SequencesSlide 12Public Directory for Roses.javaOutput from Roses:Slide 15VariablesVariable InitializationGeometry example…Page 69AssignmentConstantsSlide 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 ProgramsGraphicsAppletsDrawing Shapes© 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 is an object in Java, defined by the String class•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 to print a character string•The System.out object represents a destination (the monitor screen) to which we can send outputSystem.out.println ("Whatever you are, be a good one.");objectmethodnameinformation provided to the method(parameters)© 2004 Pearson Addison-Wesley. All rights reserved 2-6/29The print Method•The System.out object provides another service as well•The print method is similar to the println method, except that it does not advance to the next line•Therefore anything printed after a print statement will appear on the same line•See Countdown.java (page 61)© 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 (page 63)© 2004 Pearson Addison-Wesley. All rights reserved 2-8/29String Concatenation•The + operator is also used for arithmetic addition•The function that 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•The + operator is evaluated left to right (associaivity)• but parentheses can be used to force the order•See Addition.java (page 64) •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 63 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 66)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 sequences.•//********************************************************************•public class Roses•{• //-----------------------------------------------------------------• // Prints a poem (of sorts) on multiple lines.• //-----------------------------------------------------------------• public static void main (String[] args)• {• System.out.println ("Roses are red,\n\tViolets are blue,\n" +• "Sugar is sweet,\n\tBut I have \"commitment issues\",\n\t" +• "So I'd rather just be friends\n\tAt this point in our " +• "relationship.");• } // end main•} // end class Roses© 2004 Pearson


View Full Document

UNF COP 2551 - Lecture Notes

Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?