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