Presentation is loading. Please wait.

Presentation is loading. Please wait.

10/11: do/while, Logical Operators the revised SwitchTest.java notes on JApplets the do / while repetition structure break continue Logical operators.

Similar presentations


Presentation on theme: "10/11: do/while, Logical Operators the revised SwitchTest.java notes on JApplets the do / while repetition structure break continue Logical operators."— Presentation transcript:

1 10/11: do/while, Logical Operators the revised SwitchTest.java notes on JApplets the do / while repetition structure break continue Logical operators

2 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;

3 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 );

4 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 ); }

5 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" );

6 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

7 Revised SwitchTest – pt. 6 } //end for loop } //end paint method } //end class SwitchTestWithColor

8 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.

9 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 );

10 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 );

11 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?” ); }

12 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.

13 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.

14 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.

15 1 st 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.)

16 Add in “how many circles” //Fig. 5.9: DoWhileTest.java import java.awt.Graphics; import java.awt.Color;//brought in to change color import javax.swing.JApplet; import javax.swing.JOptionPane;//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?" ) ); }

17 Add in color switching public void paint ( Graphics g ) { int counter = 1; do { switch ( counter % 2 ) { case 0: g.setColor ( Color.red ); break; //put ‘break’s at case 1: //end of prev. line g.setColor ( Color.green ); break; //to save space } g.drawOval ( 110 - counter * 10, 110 - counter * 10, counter * 20, counter * 20 ); ++counter; } while ( counter <= repeat ); }

18 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.

19 Logical operators && logical ANDboth must be true & boolean logical ANDboth must be true || logical ORone must be true | boolean logical inclusive ORone must be true ^ boolean logical exclusive OR only one can be true ! logical NOTreverses evaluation

20 Logical operators: && and & && ANDboth must be true & ANDboth must be true* Truth table for && and & operators: 1st condition2nd conditionRESULT falsefalsefalse falsetruefalse truefalsefalse truetruetrue

21 Logical operators: && and & && ANDboth must be true & ANDboth 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

22 Logical operators: || and | || ORat least one must be true | ORat least one must be true* Truth table for || and | operators: 1st condition2nd conditionRESULT falsefalsefalse falsetruetrue truefalsetrue truetruetrue

23 Logical operators: || and | || ORat least one must be true | ORat 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

24 Logical operators: ^ ^ ONLY ORonly one must be true Truth table for the ^ operator: 1st condition2nd conditionRESULT falsefalsefalse falsetruetrue truefalsetrue truetruefalse

25 Logical operators: ! ! NOTreverses evaluation Unary operator (not binary) Truth table for the ! operator: conditionRESULT falsetrue truefalse

26 Logical operators: examples ( 3 > 4 && 8 > 5 ) results in ( false && true ) results in ( false ) one way these operators can be used: as an alternative to nested if statements.

27 2 nd 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.


Download ppt "10/11: do/while, Logical Operators the revised SwitchTest.java notes on JApplets the do / while repetition structure break continue Logical operators."

Similar presentations


Ads by Google