3/16: do/while, break & continue

Download Report

Transcript 3/16: do/while, break & continue

10/19: do/while, Logical Operators
• the revised SwitchTest.java
• notes on JApplets
• the do / while repetition structure
• break
• continue
• Logical operators
Midterm Grades
Midterm
Exam
Scores
grades across the semester
ALL
60
MID
A
B(+)
C(+)
D
F
Total
49
22
10
8
8
97
Q2
50
Q1
40
30
20
10
Q1
Q2
MID
0
A
B
C
ALL
D
F
Revised SwitchTest – pt. 1
//SwitchTest.java ( page 170 ) – Drawing colored shapes
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.*;
public class SwitchTestWithColor extends JApplet {
int choiceColor, choice;
Revised SwitchTest – pt. 2
public void init()
{
String inputColor;
inputColor = JOptionPane.showInputDialog (
"Enter 1 to draw in red\n" +
"Enter 2 to draw in blue\n" +
"Enter 3 to draw in green" );
choiceColor = Integer.parseInt ( inputColor );
Revised SwitchTest – pt. 3
String input;
input = JOptionPane.showInputDialog (
"Enter 1 to draw lines\n" +
"Enter 2 to draw hollow rectangles\n" +
"Enter 3 to draw filled rectangles\n" +
"Enter 4 to draw hollow ovals\n" +
"Enter 5 to draw filled ovals" );
choice = Integer.parseInt ( input );
}
Revised SwitchTest – pt. 4
public void paint ( Graphics g )
{
switch ( choiceColor ) {
case 1:
g.setColor( Color.red );
break;
case 2:
g.setColor( Color.blue );
break;
case 3:
g.setColor( Color.green );
break;
default:
JOptionPane.showMessageDialog( null ,
"Invalid value entered" );
Revised SwitchTest – pt. 5
for ( int i = 0 ; i < 10 ; i++ ) {
switch ( choice ) {
case 1:
g.drawLine ( 10 , 10 , 250 , 10 + i * 10 ); break;
case 2:
g.drawRect ( 10+i*10 , 10+i*10 , 50+i*10 , 50+i*10 ); break;
(cases 3 & 4 not shown)
case 5:
g.fillOval ( 10+i*10 , 10+i*10 , 50+i*10 , 50+i*10 ); break;
default:
JOptionPane.showMessageDialog(null,"Invalid value entered");
} //end switch for choice
Revised SwitchTest – pt. 6
} //end for loop
} //end paint method
} //end class SwitchTestWithColor
Notes on Applets
• Applets generally have two methods: the init
method and the paint method.
– the init method usually contains items for setting
things up, asking for input from the user, etc.
– the paint method usually contains items that are about
drawing ( drawString, drawLine, setColor, etc. ) in the
Applet window.
• It is a good idea to put Input Dialog boxes in the
init method, but not absolutely necessary.
the do/while repetition structure
• This structure varies from the while structure by
checking the condition AFTER the body instead
of BEFORE.
• Therefore, the do/while loop will always be
executed at least one time.
• do {
statement1;
statement2;
} while ( condition );
do/while: implications
• Always gets one time through the structure.
• The counter is generally incremented before the
condition is checked: watch out for surprises.
• Note the semicolon after the while statement.
int a = 10;
do {
g.drawString ( “go” , a , a );
a += 5 ;
} while ( a <= 40 );
the break statement
• the break statement causes immediate exit from
the control structure. Already saw it here:
switch ( pizzaSlice ) {
case 1:
System.out.println ( “have more” );
break;
case 2:
System.out.println ( “just right” );
break;
default:
System.out.println ( “well?” );
}
the break statement: other uses
int x = 1;
while ( x < 5 ) {
y = x * 3;
if ( y == 9 ) {
System.out.print ( “ I like nine ” );
break;
}
System.out.print ( y );
x++;
}
prints out 36 I like nine in the MSDOS window.
the continue statement
• similar to the break statement, but has important
differences:
– the continue statement will stop JUST that iteration
of the loop control structure and return to the
beginning of the loop.
– the continue statement will not leave the control
structure entirely.
the continue statement: different.
int x = 1;
while ( x < 5 ) {
y = x * 3;
x++;
if ( y == 9 ) {
System.out.print ( “ I like nine ” );
continue;
}
System.out.print ( y );
}
prints out 36 I like nine 12 in the MSDOS window.
1st program: DoWhileTest p.175
•
After getting the program to run, modify the
program to :
1. Allow user input for how many circles to draw.
(Include an init method. Put an InputDialog
box in it for the user input.)
2. Change the oval’s color for every second oval. You
pick which colors to use.
(use an “if” statement with a modulus
comparison to decide what color to draw
the next oval.)
Add in “how many circles”
//Fig. 5.9: DoWhileTest.java
import
import
import
import
java.awt.Graphics;
java.awt.Color;
javax.swing.JApplet;
javax.swing.JOptionPane;
//brought in to change color
//brought in for Input box
public class DoWhileTest extends JApplet {
int repeat = 0; //used in both methods so declared outside both
public void init ()
{
repeat = Integer.parseInt ( JOptionPane.showInputDialog
( "How many circles?" ) );
}
Add in color switching
public void paint ( Graphics g )
{
int counter = 1;
do {
switch ( counter % 2 ) {
case 0:
g.setColor ( Color.red ); break;
case 1:
g.setColor ( Color.green ); break;
}
g.drawOval ( 110 - counter * 10 , 110 counter * 20 , counter * 20 );
++counter;
} while ( counter <= repeat );
}
}
//put ‘break’s at
//end of prev. line
//to save space
counter * 10 ,
Logical Operators
• To evaluate more than one condition, we can use
logical operators
• EX: if you have a ticket AND are at least 17, you
can see an R-rated movie.
• EX: if it is after five o’clock OR I’m tired, I’ll
leave work.
• EX: if the car is NOT out of gas, I’ll drive.
Logical operators
• &&
•&
• ||
•|
•^
•!
logical AND
both must be true
boolean logical AND
both must be true
logical OR
one must be true
boolean logical inclusive OR one must be true
boolean logical exclusive OR only one can be true
logical NOT
reverses evaluation
Logical operators: && and &
• &&
AND
both must be true
•&
AND
both must be true*
Truth table for && and & operators:
1st condition
false
false
true
true
2nd condition
false
true
false
true
RESULT
false
false
false
true
Logical operators: && and &
• &&
AND
both must be true
•&
AND
both must be true*
• The & operator differs in that both conditions are
evaluated/processed every time. The &&
provides a “short-circuit” if the 1st condition isn’t
true. Why it matters: EX: if you include an
increment operator:
gender == 1 & ++age = 1
Logical operators: || and |
• ||
OR
at least one must be true
•|
OR
at least one must be true*
Truth table for || and | operators:
1st condition
false
false
true
true
2nd condition
false
true
false
true
RESULT
false
true
true
true
Logical operators: || and |
• ||
OR
at least one must be true
•|
OR
at least one must be true*
• The | operator differs in that both conditions are
evaluated/processed every time. The || provides
a “short-circuit” if the first condition IS true.
Why it matters: EX: if you include an increment
operator:
gender == 1 | ++age = 1
Logical operators: ^
•^
ONLY OR
only one must be true
Truth table for the ^ operator:
1st condition
false
false
true
true
2nd condition
false
true
false
true
RESULT
false
true
true
false
Logical operators: !
•!
NOT
reverses evaluation
Unary operator (not binary)
Truth table for the ! operator:
condition RESULT
false
true
true
false
Logical operators: examples
( 3 > 4 && 8 > 5 )
( false && true )
( false )
results in
results in
• one way these operators can be used: as an
alternative to nested if statements.
2nd Program of the day
• Create a program that will print out a table of 3
randomly generated double-type numbers from 0
to 100, then print out the largest of the 3 numbers
in the MS-DOS window.
• Generate the three numbers by using the
Math.random() method:
EX: x = Math.random() * 100 ;
will generate a random double-type from 0 – 99.
• Use logical operators to decide which number is
the largest.