Chapter 2 Primitive Data Types and Operations rerequisites for Part I Basic computer skills such as using Windows Internet Explorer and Microsoft Word Chapter 1 Introduction to Computers, Programs and Java hapter 2 Primitive Data Types and Operations hapter 3 Control Statements 事实不可扭曲,意见大可自由 下hpr4Mtod C P. Scott Chapter 5 arra Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 2 Primitive Data Types and Operations Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Control Statements Chapter 5 Arrays Chapter 4 Methods Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Prerequisites for Part I 事实不可扭曲,意见大可自由 —— C.P.Scott
Objectives o To write Java programs to per form simple calculations(8 2.2) o To use identifiers to name variables, constants, methods, and classes (8 2.3) ● To use variables to store data(§24-2.5) o To program with assignment statements and assignment expressions ( 82.5) o To use constants to store permanent data($ 2.6) To declare Java primitive data types: byte short, int, long, float, double, char, and olean(§27-2.10) o To use Java operators to write expressions(s 2.7-2.10) o To know the rules governing operand evaluation order, operator precedence, and operator associativity(8211-2.12) o To represent a string using the String type($ 2.13) o To obtain input using the JOption Pane input dialog boxes($2.14) ● To obtain input from console(§2.16 Optional o To format output using JDK 1.5 printf(s 2.17) o To become familiar with Java documentation, programming style, and naming conventions(§218) o To distinguish syntax errors, runtime errors, and logic errors(82.19 o To debug logic errors lias. 2 t2g)on to Java Programming, revised by Dai-kaiyu
Liang, Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives ⚫ To write Java programs to perform simple calculations (§2.2). ⚫ To use identifiers to name variables, constants, methods, and classes (§2.3). ⚫ To use variables to store data (§2.4-2.5). ⚫ To program with assignment statements and assignment expressions (§2.5). ⚫ To use constants to store permanent data (§2.6). ⚫ To declare Java primitive data types: byte, short, int, long, float, double, char, and boolean (§2.7 – 2.10). ⚫ To use Java operators to write expressions (§2.7 – 2.10). ⚫ To know the rules governing operand evaluation order, operator precedence, and operator associativity (§2.11 – 2.12). ⚫ To represent a string using the String type. (§2.13) ⚫ To obtain input using the JOptionPane input dialog boxes (§2.14). ⚫ To obtain input from console (§2.16 Optional). ⚫ To format output using JDK 1.5 printf (§2.17). ⚫ To become familiar with Java documentation, programming style, and naming conventions (§2.18). ⚫ To distinguish syntax errors, runtime errors, and logic errors (§2.19). ⚫ To debug logic errors (§2.20)
Introducing Programming with an ( E Example Example 2.1 Computing the area of a circle Program =Algorithm Data structure ComputeRed Run Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 3 Introducing Programming with an Example Example 2.1 Computing the Area of a Circle ComputeArea Run Program = Algorithm + Data structure
Trace a Program Execution public class Compute Area i allocate memory /* Main method or radius public static void main( Stringl args)( double radius radius noⅴalue double area / Assign a radius radius= 20 / Compute area area= radius radius 3.14159 / Display results System. out printIn("The area for the circle of radius"+ radius+"is"+ area) Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 4 Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } radius no value allocate memory for radius
Trace a Program Execution public class Compute Area i /* Main method * memory public static void main( Stringl args)( double radius radius no value double area area no value / Assign a radius radius= 20 allocate memory / Compute area for area area= radius radius 3.14159 / Display results System. out printIn("The area for the circle of radius"+ radius+"is"+ area) Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 5 Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } radius no value memory area no value allocate memory for area
Trace a Program Execution public class Compute Area i assign 20 to radius /* Main method * public static void main( Stringl args)( double radius. radius 20 double area area no value / Assign a radius adIts / Compute area area= radius radius 3.14159 / Display results System. out printIn("The area for the circle of radius"+ radius+"is"+ area) Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 6 Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } radius 20 area no value assign 20 to radius
Trace a Program Execution public class Compute Area i /* Main method * memory public static void main( Stringl args)( double radius. radius 20 double area area 1256.636 / Assign a radius radius= 20 / Compute area compute area and assign it to variable area area=radius radius *3. 14159 / Display results System. out printIn("The area for the circle of radius"+ radius+"is"+ area) Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 7 Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } radius 20 memory area 1256.636 compute area and assign it to variable area
Trace a Program Execution public class Compute Area i /* Main method * memory public static void main( Stringl args)( double radius. radius 20 double area area 1256.636 / Assign a radius radius= 20 / Compute area area= radius radius 3.14159 print a message to the le / Display results System. out. printIn("The area for the circle of radius radius+ A string constant should not cross Ines Liang, Introduction to Java Programming, revised by dai-kaiyu
Liang, Introduction to Java Programming,revised by Dai-kaiyu 8 Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } } radius 20 memory area 1256.636 print a message to the console A string constant should not cross lines
Identifiers o An identifier is a sequence of characters that consist of letters, digits, underscores ( and dollar signs(s o An identifier must start with a letter, an underscore (,or a dollar sign (S). It cannot start with a digit O An identifier cannot be a reserved word o An identifier can be of any lengt o Are used for naming variables, constants, methods, classes and packages Liang, Introduction to Java Programming, revised by dai-kaiyu
Liang, Introduction to Java Programming,revised by Dai-kaiyu 9 Identifiers ⚫ An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). ⚫ An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. An identifier cannot be a reserved word. ⚫ An identifier can be of any length. ⚫ Are used for naming variables, constants, methods, classes, and packages
Java Keywords abstract boolean bre ak byte case catch char class continue default double e⊥se extends false final finally float 士。卫 if implements import instanceof int interface long native new null package private protected public 卫 e turn sho卫t static super switch synchronized this throw throws transient true try void volatile wn⊥e Key woras that are ye served but not used by jaa Cons t goto Rg. 4.2 Ja va keywords. Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 10