1、Chapter 2 Data and Expressions,Java Software Solutions Foundations of Program Design Seventh Edition,John Lewis William Loftus,Data and Expressions,Lets explore some other fundamental programming concepts Chapter 2 focuses on: character strings primitive data the declaration and use of variables exp
2、ressions and operator precedence data conversions accepting input from the user Java applets introduction to graphics,Outline,Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes,Character Strings,A string li
3、teral is represented 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,The println Method,In the Lincoln program from Chapter 1,
4、 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 output,System.out.println (“Whatever you are, be a good one.“);,The print Method,The System.out object provides another service as well The print method
5、 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,/* / Countdown.java Author: Lewis/Loftus / / Demonstrates the difference between print and println. /*public class Co
6、untdown /-/ Prints two lines of output representing a rocket countdown./-public static void main (String args)System.out.print (“Three. “);System.out.print (“Two. “);System.out.print (“One. “);System.out.print (“Zero. “);System.out.println (“Liftoff!“); / appears on first output lineSystem.out.print
7、ln (“Houston, we have a problem.“); ,/* / Countdown.java Author: Lewis/Loftus / / Demonstrates the difference between print and println. /*public class Countdown /-/ Prints two lines of output representing a rocket countdown./-public static void main (String args)System.out.print (“Three. “);System.
8、out.print (“Two. “);System.out.print (“One. “);System.out.print (“Zero. “);System.out.println (“Liftoff!“); / appears on first output lineSystem.out.println (“Houston, we have a problem.“); ,Output Three. Two. One. Zero. Liftoff! Houston, we have a problem.,String Concatenation,The string concatenat
9、ion 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,/* / Facts.java Author: Lewis/Loftus / / Demonstrates the use of the st
10、ring 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 stringSystem.out.println (“We present the following facts for your “+ “extracurricula
11、r edification:“);System.out.println ();/ A string can contain numeric digitsSystem.out.println (“Letters in the Hawaiian alphabet: 12“);continue,continue/ A numeric value can be concatenated to a stringSystem.out.println (“Dialing code for Antarctica: “ + 672);System.out.println (“Year in which Leon
12、ardo da Vinci invented “+ “the parachute: “ + 1515);System.out.println (“Speed of ketchup: “ + 40 + “ km per year“); ,continue/ A numeric value can be concatenated to a stringSystem.out.println (“Dialing code for Antarctica: “ + 672);System.out.println (“Year in which Leonardo da Vinci invented “+ “
13、the parachute: “ + 1515);System.out.println (“Speed of ketchup: “ + 40 + “ km per year“); ,Output We present the following facts for your extracurricular edification:Letters in the Hawaiian alphabet: 12 Dialing code for Antarctica: 672 Year in which Leonardo da Vinci invented the parachute: 1515 Spe
14、ed of ketchup: 40 km per year,String 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 I
15、f both operands are numeric, it adds them The + operator is evaluated left to right, but parentheses can be used to force the order See Addition.java,/* / Addition.java Author: Lewis/Loftus / / Demonstrates the difference between the addition and string / concatenation operators. /*public class Addi
16、tion /-/ Concatenates and adds two numbers and prints the results./-public static void main (String args)System.out.println (“24 and 45 concatenated: “ + 24 + 45);System.out.println (“24 and 45 added: “ + (24 + 45); ,/* / Addition.java Author: Lewis/Loftus / / Demonstrates the difference between the
17、 addition and string / concatenation operators. /*public class Addition /-/ Concatenates and adds two numbers and prints the results./-public static void main (String args)System.out.println (“24 and 45 concatenated: “ + 24 + 45);System.out.println (“24 and 45 added: “ + (24 + 45); ,Output 24 and 45
18、 concatenated: 2445 24 and 45 added: 69,Quick Check,What output is produced by the following?,System.out.println (“X: “ + 25); System.out.println (“Y: “ + (15 + 50); System.out.println (“Z: “ + 300 + 50);,Quick Check,What output is produced by the following?,System.out.println (“X: “ + 25); System.o
19、ut.println (“Y: “ + (15 + 50); System.out.println (“Z: “ + 300 + 50);,X: 25 Y: 65 Z: 30050,Escape Sequences,What if we wanted to print 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 “H
20、ello“ to you.“);An escape sequence is a series of characters that represents a special character An escape sequence begins with a backslash character ()System.out.println (“I said “Hello“ to you.“);,Escape Sequences,Some Java escape sequences:,See Roses.java,/* / Roses.java Author: Lewis/Loftus / /
21、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,ntViolets are blue,n“ +“Sugar is sweet,ntBut I have “commitment issues“,nt“ +“So Id rather just be friendsntAt this
22、point in our “ +“relationship.“); ,/* / 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,ntViolets are blue,n“ +“Sugar is sweet,n
23、tBut I have “commitment issues“,nt“ +“So Id rather just be friendsntAt this point in our “ +“relationship.“); ,Output Roses are red,Violets are blue, Sugar is sweet,But I have “commitment issues“,So Id rather just be friendsAt this point in our relationship.,Quick Check,Write a single println statem
24、ent that produces the following output:,“Thank you all for coming to my home tonight,“ he said mysteriously.,Quick Check,Write a single println statement that produces the following output:,“Thank you all for coming to my home tonight,“ he said mysteriously.,System.out.println (“Thank you all for “
25、+“coming to my homentonight,“ he said “ +“mysteriously.“);,Outline,Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes,Variables,A variable is a name for a location in memory that holds a value A variable de
26、claration specifies the variables name and the type of information that it will hold,int total; int count, temp, result;,Multiple variables can be created in one declaration,Variable Initialization,A variable can be given an initial value in the declaration,int sum = 0; int base = 32, max = 149;,Whe
27、n a variable is referenced in a program, its current value is used See PianoKeys.java,/* / PianoKeys.java Author: Lewis/Loftus / / Demonstrates the declaration, initialization, and use of an / integer variable. /*public class PianoKeys /-/ Prints the number of keys on a piano./-public static void ma
28、in (String args)int keys = 88;System.out.println (“A piano has “ + keys + “ keys.“); ,/* / PianoKeys.java Author: Lewis/Loftus / / Demonstrates the declaration, initialization, and use of an / integer variable. /*public class PianoKeys /-/ Prints the number of keys on a piano./-public static void ma
29、in (String args)int keys = 88;System.out.println (“A piano has “ + keys + “ keys.“); ,Output A piano has 88 keys.,Assignment,An assignment statement changes the value of a variable The assignment operator is the = sign,total = 55;,The value that was in total is overwritten You can only assign a valu
30、e to a variable that is consistent with the variables declared type See Geometry.java,/* / Geometry.java Author: Lewis/Loftus / / Demonstrates the use of an assignment statement to change the / value stored in a variable. /*public class Geometry /-/ Prints the number of sides of several geometric sh
31、apes./-public static void main (String args)int sides = 7; / declaration with initializationSystem.out.println (“A heptagon has “ + sides + “ sides.“);sides = 10; / assignment statementSystem.out.println (“A decagon has “ + sides + “ sides.“);sides = 12;System.out.println (“A dodecagon has “ + sides
32、 + “ sides.“); ,/* / Geometry.java Author: Lewis/Loftus / / Demonstrates the use of an assignment statement to change the / value stored in a variable. /*public class Geometry /-/ Prints the number of sides of several geometric shapes./-public static void main (String args)int sides = 7; / declarati
33、on with initializationSystem.out.println (“A heptagon has “ + sides + “ sides.“);sides = 10; / assignment statementSystem.out.println (“A decagon has “ + sides + “ sides.“);sides = 12;System.out.println (“A dodecagon has “ + sides + “ sides.“); ,Output A heptagon has 7 sides. A decagon has 10 sides.
34、 a dodecagon has 12 sides.,Constants,A constant is an identifier that is similar to a variable except that it holds the same value during its entire existence As the name implies, it is constant, not variable The compiler will issue an error if you try to change the value of a constant In Java, we u
35、se the final modifier to declare a constant final int MIN_HEIGHT = 69;,Constants,Constants are useful for three important reasons First, they give meaning to otherwise unclear literal values Example: MAX_LOAD means more than the literal 250 Second, they facilitate program maintenance If a constant i
36、s used in multiple places, its value need only be set in one place Third, they formally establish that a value should not change, avoiding inadvertent errors by other programmers,Outline,Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs
37、Graphics Applets Drawing Shapes,Primitive Data,There are eight primitive data types in Java Four of them represent integers: byte, short, int, long Two of them represent floating point numbers: float, double One of them represents characters: char And one of them represents boolean values: boolean,N
38、umeric Primitive Data,The difference between the numeric primitive types is their size and the values they can store:,Characters,A char variable stores a single character Character literals are delimited by single quotes: a X 7 $ , n Example declarations: char topGrade = A; char terminator = ;, sepa
39、rator = ; Note the difference between a primitive character variable, which holds only one character, and a String object, which can hold multiple characters,Character Sets,A character set is an ordered list of characters, with each character corresponding to a unique number A char variable in Java can store any character from the Unicode character set The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters It is an international character set, containing symbols and characters from many world languages,