Chapter 2
Chapter 2
Chapter 2
Elementary Programming
Motivations
2
// 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);
}
}
animation
// Assign a radius
radius = 20; allocate memory
for area
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
animation
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius "
+
radius + " is " + area);
}
}
animation
// Display results
System.out.println("The area for the circle of radius "
+
radius + " is " + area);
}
}
animation
// Compute area
print a message to the
console
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Notes
10
The plus sign (+) has two meanings: one for addition
and the other for concatenating (combining) strings.
The plus sign (+) in lines 13–14 is called a string
concatenation operator. It combines two strings into
one.
Reading Input from the Console
11
• you can use the Scanner class to create an object to read input from
System.in, as follows:
1. Create a Scanner object
Scanner input = new Scanner(System.in);
2. Use the methods next(), nextByte(), nextShort(), nextInt(),
nextLong(), nextFloat(), nextDouble(), or nextBoolean() to
obtain to a string, byte, short, int, long, float, double, or
boolean value.
For example,
System.out.print("Enter a double value: ");
Scanner input = new Scanner(System.in);
double d = input.nextDouble();
Reading Input from the Console
12
int x; // Declare x to be an
// integer variable;
double radius; // Declare radius to
// be a double variable;
char a; // Declare a to be a
// character variable;
Assignment Statements
18
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;
area = radius * radius * 3.14159; // Compute area
Declaring and Initializing in One Step
19
int x = 1;
double d = 1.4;
Named Constants
20
Class names:
Capitalize the first letter of each word in the name. For
example, the class name ComputeArea.
Constants:
Capitalize all letters in constants, and use underscores to
connect words. For example, the constant PI and
MAX_VALUE
Numerical Data Types
23
Java uses four types for integers: byte, short, int, and long.
Java uses two types for floating-point numbers: float and double.
Numeric Operators
24
Integer Division
25
+, -, *, /, and %
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
System.out.println(Math.pow(2, 3));
// Displays 8.0
System.out.println(Math.pow(4, 0.5));
// Displays 2.0
System.out.println(Math.pow(2.5, 2));
// Displays 6.25
System.out.println(Math.pow(2.5, -2));
// Displays 0.16
Number Literals
31
int i = 34;
long x = 1000000;
double d = 5.0;
Integer Literals
32
System.out.println
3 4 x 10( y 5)(a b c ) 4 9 x
9( )
5 x x y
is translated to
3 + 4 * 4 + 5 * (4 + 3) - 1
(1) inside parentheses first
3 + 4 * 4 + 5 * 7 – 1
(2) multiplication
3 + 16 + 5 * 7 – 1
(3) multiplication
3 + 16 + 35 – 1
(4) addition
19 + 35 – 1
(5) addition
54 - 1
(6) subtraction
53
Problem: Converting Temperatures
38
41
Shortcut Assignment Operators
42
count = count + 1;
the preceding statement can be written as: count += 1;
The += is called the addition assignment operator. Table 2.4
shows other augmented assignment operators.
Increment and Decrement Operators
43
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
Conversion Rules
47
Implementation
Testing
Implementation
Testing
Part of the analysis entails modeling
the system’s behavior. The model is Deployment
System
Design
Implementation
Testing
Implementation
Testing
System
Design
Implementation
Testing
Requirement
Specification
Deployment makes the project
available for use.
System
Analysis
System
Design
Implementation
Testing
Requirement
Maintenance is concerned with
Specification
changing and improving the
System product.
Analysis
System
Design
Implementation
Testing
char ch = 'a';
System.out.println(++ch);
b
Unicode Format
63
a =97
ASCII Character Set, cont.
68
A=\u0041
Casting between char and Numeric Types
69
• A char can be cast into any numeric type, and vice versa
int i = 'a'; // Same as int i = (int)'a';
char c = 97; // Same as char c = (char)97;
Casting Example Comments
The next() method reads a string that ends with a whitespace character
JOptionPane Input
80
5 import javax.swing.JOptionPane;
– Declaration
• firstNumber and secondNumber are variables
– Variables
• Location in memory that stores a value
– Declare with name and data type before use
• firstNumber and secondNumber are of data type
String (package java.lang)
– Hold strings
• Variable name: any valid identifier
• Declarations end with semicolons ;
String firstNumber, secondNumber;
– Method Integer.parseInt
• Converts String argument into an integer (type int)
– Class Integer in java.lang
• Integer returned by Integer.parseInt is assigned to
variable number1 (line 27)
– Remember that number1 was declared as type int
• Line 28 similar