Professional Documents
Culture Documents
42
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;
43
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = 'A'; // Assign 'A' to a;
44
• int x = 1;
• double d = 1.4;
45
Name Range Storage Size
Positive range:
4.9E-324 to 1.7976931348623157E+308
46
Name Meaning Example Result
+ Addition 34 + 1 35
% Remainder 20 % 3 2
47
+, -, *, /, and %
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
48
• Remainder is very useful in programming.
• For example, an even number % 2 is always 0 and an odd number
% 2 is always 1.
• So you can use this property to determine whether a number is
even or odd.
• Suppose today is Saturday and you and your friends are going
to meet in 10 days. What day is in 10 days? You can find that
day is Tuesday using the following expression:
Saturday is the 6th day in a week
A week has 7 days
(6 + 10) % 7 is 2
The 2nd day in a week is Tuesday
After 10 days
49
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
50
3 4 x 10( y 5)(a b c) 4 9 x
9( )
5 x x y
is translated to
51
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
52
Write a program that converts a Fahrenheit degree to
Celsius using the formula:
celsius ( 95 )( fahrenheit 32)
53
Operator Example Equivalent
+= i += 8 i = i + 8
-= f -= 8.0 f = f - 8.0
*= i *= 8 i = i * 8
/= i /= 8 i = i / 8
%= i %= 8 i = i % 8
54
Operator Name Description
++var preincrement The expression (++var) increments var by 1 and evaluates
to the new value in var after the increment.
var++ postincrement The expression (var++) evaluates to the original value
in var and increments var by 1.
--var predecrement The expression (--var) decrements var by 1 and evaluates
to the new value in var after the decrement.
var-- postdecrement The expression (var--) evaluates to the original value
in var and decrements var by 1.
55
Examples:
int num = 6;
num ++;
The value of variable num is increased by 1 to be 7
int m= 10;
int n;
n = ++ m;
Both m and n are set to 11
int x = 5 ;
int y ;
y = x ++;
x is set to 6 and y is 5
int i = 10; Same effect as
int newNum = 10 * i++; int newNum = 10 * i;
i = i + 1;
57
Consider the following statements:
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
58
When performing a binary operation involving two
operands of different types, Java automatically
converts the operand based on the following rules:
59
The char type only represents one character. To represent a string
of characters, use the data type called String. For example,
60
// Three strings are concatenated
String message = "Welcome " + "to " + "Java";
61
Program title:
Computes the area of the circle
62
allocate memory
public class ComputeArea { for radius
/** Main method */
public static void main(String[] args) {
double radius; radius no value
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);
}
}
63
public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
double radius; radius no value
double area; area no value
// 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);
}
}
64
public class ComputeArea { assign 20 to radius
/** Main method */
public static void main(String[] args) {
double radius; radius 20
double area;
area no value
// 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);
}
}
65
public class ComputeArea {
/** Main method */ memory
public static void main(String[] args) {
double radius; radius 20
double area;
area 1256.636
// Assign a radius
radius = 20;
compute area and assign
// Compute area it to variable area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
66
public class ComputeArea {
/** Main method */ memory
public static void main(String[] 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
console
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
67
Two ways of obtaining input by using import classes.
• Show an internal information dialog with the message, ' Welcome to Java!","':
JOptionPane.showMessageDialog(null,"Welcome to Java!","Display
Message",JOptionPane.INFORMATION_MESSAGE);
• Show an information panel with the options yes/no and message ‘Please
choose one':
JOptionPane.showConfirmDialog(null,“Please choose one",“Choose
one",JOptionPane.YES_NO_OPTION);
• Show a warning dialog with the options OK, CANCEL, title 'Warning', and
message 'Click OK to continue':
Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue",
"Warning",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,null, options,
options[0]);
• Show a dialog asking the user to type in a String:
String inputValue = JOptionPane.showInputDialog("Please input a value");
73
import javax.swing.JOptionPane;
79
Program title:
Payroll(Console)
80
import java.util.Scanner; // Needed for the Scanner class
86
• 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
87
The input returned from the input dialog box is a string. If
you enter a numeric value such as 123, it returns “123”.
To obtain the input as a number, you have to convert a
string into a number.
89
In Java, an augmented expression of the form x1 op= x2 is
implemented as x1 = (T)(x1 op x2), where T is the type for
x1. Therefore, the following code is correct.
int sum = 0;
sum += 4.5; // sum becomes 4 after this statement
90