3/16: do/while, break & continue

Download Report

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

3/4: do/while, break & continue
• about the midterm
• the revised SwitchTest.java
• notes on JApplets
• the do / while repetition structure
• break
• continue
• today’s program
The Midterm Exam
•
•
•
•
Mean: 80%
Median: 84%
High score: 101% (2 people)
Low score: 41%
Revised SwitchTest – pt. 1
//SwitchTest.java ( page 211 ) – 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 )
{
super.paint ( 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 , “Bad value." );
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.
• Put Input Dialog boxes in the init method.
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.
today’s program: DoWhileTest p.216
•
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.)