Professional Documents
Culture Documents
Selection Statements: 4 Ed Chapter 5 1
Selection Statements: 4 Ed Chapter 5 1
Selection Statements
int testScore;
else
This
Thisstatement
statementisis
JOptionPane.showMessageDialog(null, executed
executedififthe
thetestScore
testScore
isis70 or higher.
70 or higher.
"You did pass" );
<then block>
else
Boolean
BooleanExpression
Expression
<else block>
if ( testScore < 70 )
Then
ThenBlock
Block JOptionPane.showMessageDialog(null,
"You did not pass" );
else
Else
ElseBlock
Block JOptionPane.showMessageDialog(null,
"You did pass " );
false testScore
true
testScore<<
70
70??
JOptionPane.
JOptionPane. JOptionPane.
JOptionPane.
showMessageDialog
showMessageDialog showMessageDialog
showMessageDialog
(null,
(null,"You
"Youdid
didpass");
pass"); (null,
(null,"You
"Youdid
didnot
notpass");
pass");
testScore < 80
testScore * 2 >= 350
30 < w / (h * h)
x + y != 2 * (a + b)
2 * Math.PI * radius <= 359.99
if ( <boolean expression> )
{
…
} Style
Style22
else
{
…
}
if ( <boolean expression> )
<then block>
Boolean
BooleanExpression
Expression
if ( testScore >= 95 )
Then
ThenBlock
Block JOptionPane.showMessageDialog(null,
"You are an honor student");
testScore
true
testScore>=
>=
95?
95?
JOptionPane.
JOptionPane.
showMessageDialog
showMessageDialog(null,
(null,
false "You are an honor student");
"You are an honor student");
true inner if
false testScore
testScore>=
>=70
70
??
messageBox.show
messageBox.show
false studentAge
studentAge<<10
10
true
("You
("Youdid
didnot
not ??
pass");
pass");
messageBox.show
messageBox.show
messageBox.show
messageBox.show ("You
("Youdid
didaagreat
great
("You
("Youdid
didpass");
pass"); job");
job");
if (x < y) A
A Both A
A and B
B means…
if (x < z)
System.out.print("Hello"); if (x < y) {
else if (x < z) {
System.out.print("Good bye"); System.out.print("Hello");
} else {
System.out.print("Good
bye");
}
if (x < y) B
B }
if (x < z)
System.out.print("Hello");
else
System.out.print("Good bye");
P Q P && Q P || Q !P
false false false false true
false true false true true
true false false true false
true true true true false
if (isValid(30)) {
…
Can be used as } else {
…
}
if (str1 == str2) {
System.out.println("They are equal");
} else {
System.out.println("They are not equal");
}
if (str1 == str2) {
System.out.println("They are equal");
} else {
System.out.println("They are not equal");
}
if (str1.equals(str2)) {
System.out.println("They are equal");
} else {
System.out.println("They are not equal");
}
switch (gradeLevel) {
This
Thisstatement
statement
isisexecuted
executedifif
case 1: System.out.print("Go to the Gymnasium"); the
thegradeLevel
gradeLevel
break; isisequal
equaltoto1.1.
true
NN ==
==
11?? xx==10;
10;
switch ( N ) {
case 1: x = 10; false
true
case 2: x = 20; NN ==
==
22?? xx==20;
20;
case 3: x = 30;
} false
true
NN ==
== xx==30;
30;
33??
false
true
NN ==
==
switch ( N ) { 11?? xx==10;
10;
case 1: x = 10;
false break;
break;
break; true
NN ==
==
case 2: x = 20; 22?? xx==20;
20;
break;
false break;
break;
case 3: x = 30; true
NN ==
==
break; 33?? xx==30;
30;
}
false break;
break;
case 10:
case 9:
case 8: System.out.print("Master");
break;
case 7:
case 6: System.out.print("Journeyman");
break;
case 5:
case 4: System.out.print("Apprentice");
break;
• Problem statement:
Write an application that simulates a screensaver
by drawing various geometric shapes in different
colors. The user has an option of choosing a type
(ellipse or rectangle), color, and movement
(stationary, smooth, or random).
• Tasks:
– Get the shape the user wants to draw.
– Get the color of the chosen shape.
– Get the type of movement the user wants to use.
– Start the drawing.
Ch5DrawShape JOptionPane
standard class
DrawingBoard DrawableShape
Program source file is too big to list here. From now on, we ask
you to view the source files using your Java IDE.
Directory:
Directory: Chapter5/Step1
Chapter5/Step1
Source
Source Files:
Files: Ch5DrawShape.java
Ch5DrawShape.java
Directory:
Directory: Chapter5/Step2
Chapter5/Step2
Source
Source Files:
Files: Ch5DrawShape.java
Ch5DrawShape.java
DrawableShape.java
DrawableShape.java
Directory:
Directory: Chapter5/Step3
Chapter5/Step3
Source
Source Files:
Files: Ch5DrawShape.java
Ch5DrawShape.java
DrawableShape.java
DrawableShape.java
Directory:
Directory: Chapter5/Step4
Chapter5/Step4
Source
Source Files:
Files: Ch5DrawShape.java
Ch5DrawShape.java
DrawableShape.java
DrawableShape.java
Directory:
Directory: Chapter5/Step5
Chapter5/Step5
Source
Source Files:
Files: Ch5DrawShape.java
Ch5DrawShape.java
DrawableShape.java
DrawableShape.java
• Possible Extensions
– Morphing the object shape
– Changing the object color
– Drawing multiple objects
– Drawing scrolling text