1 / 30

Decisions - if and switch

Decisions - if and switch. Chapter Seven. Program. T. F. Is A >B. NO. YES. Rest of Pgm. Lots of places where decisions are made in “real world” and in computer programs if statements test boolean data (true - false) Also in this chapter: buttons (action buttons on web pages)

yitta
Download Presentation

Decisions - if and switch

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Decisions - if and switch Chapter Seven

  2. Program T F Is A >B NO YES Rest of Pgm

  3. Lots of places where decisions are made in “real world” and in computer programs if statements test boolean data (true - false) Also in this chapter: buttons (action buttons on web pages) text fields random numbers

  4. The Voting Checker Program // Enter your age and Program tells is you can vote //import all the usual stuff (by now you know //most all of this) //import - .Applet, awt.*; awt.event.*; public class Voting extends Applet implements AdjustmentListener { private Scrollbar bar; private int age = 0;

  5. //skipping some of the usual stuff to concentrate on NEW stuff … init( ) { /* a method that produces a Horizontal scrollbar and displays it */ public void paint (Graphics g) { if (age > 17 ) g.drawString ( “ You may vote “, 50, 50); g.drawString ( “Age is “ + age , 50, 70) ; }}

  6. Public void adjustmentValueChanged ( AdjustmentEvent event) { …\\ Not going to write this out as it is same as that which was in other programs with scrollbars NOTE indentation NICE but not necessary it is easier to follow if done correctly It is Recommended

  7. AND OR NOT OPERATORS • AND = && TWO AMPERSANDS • OR = || TWO PIPE SYMBOLS • NOT = ! (EXCLAMATION POINT) A B OUT A AND OUT F F F B F T F T F F T T T

  8. OR Gate A B OUT OUT F F F F T T T F T T T T

  9. NOT Gate IN OUT IN OUT F T T F

  10. Relational operators = = Equal to != Not Equal < = Less that equal to > = Greater than equal to < Less than > Greater than

  11. A Dice Game Program // import the usual stuff awt.*; Applet; event.* public class Dice1 extends Applet implements AdjustmentListener { private Scrollbar die1, die2; private int value1 = 1, value2 = 1; //NOTE // not only declared but initalized public void init ( ) { die1 = new Scrollbar (Scrollbar.HORIZONTAL,1 ,1,1,6); add(die1);} // NOTE die2 = almost same

  12. Public void paint (Graphics g) { int total; total = value1 + value2; g.drawString(“ total is “ + total, 50, 50); if (total = = 6) g.drawString(“ you have won!” , 50 , 60 ); } public void adjustmentValueChanged(AdjustmentEvent event) { value1 = die1.getValue( ); value2 = die2.getValue( ); repaint( ); } }

  13. Options to the Dice game If (total = = 6) g.drawString(“ you have won! “ , 50 , 50); else g.drawString(“you have lost!” 50, 50); // OR another option if (die1 = = die2) g.drawString(“you won”,50,50); // OR another if ((total = =2) || (total = = 7)) g.drawString(“You have won”,50, 50);

  14. Nested if statements If ((age > 6 ) if (age < 16) g.drawString(“Junior rate”, 50, 50); else g.drawString(“adult rate “ , 50, 50); // could be done with two separate if //statements

  15. Tom and Jerry //A story of two scrollbars // the usual imports public class TomAndJerry extends Applet implements AdjustmentListener { private Scrollbar tom, jerry; private int tomValue = 0, jerryValue = 0;

  16. Public void init ( ) { Label toms = new Label (“Tom:”); add(toms); tom = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0, 100); add(tom); tom.addAdjustmentListener(this); //NOTE SAME stuff (lines of code) for jerry }

  17. Public void paint (Graphics g) { g.drawString(“Tom” ,5, 70); g.drawString(“Jerry” ,5, 85); g.fillRect(40, 60, tomValue, 10); g.fillRect(40, 75, jerryValue, 10); if (tomValue = = jerryValue) g.drawString(“They are equal “, 50, 50); else if (tomValue > jerryValue) drawString(“Tom is bigger “, 50, 50); else g.drawString(“Jerry is bigger”, 50, 50);}

  18. Public void adjustmentValueChanged (AdjustmentEvent event) { tomValue = tom.getValue( ); jerryValue = jerry.getValue( ); repaint ( ); } }

  19. Using Buttons //usual imports public class ButtonCount extends Applet implements ActionListener { private Button tester; private int count = 0; public void init ( ) { tester = new Button (“Press Here”); add(tester); tester.addActionListener(this);}

  20. Public void actionPerformed(ActionEvent event) { count++; repaint( ); } public void paint (Graphics g) { g.drawString (“Number of button presses is “ + count, 10,50); } }

  21. What if you had more that one button? // Modifications //after “implements ActionListener // add “comma “ , AdjustmentListener //before the { //AND ADD if(event.getSource ( ) = = truckButton) truckCount++; if (event.getSource( ) = = carButton) carCount++; repaint ( ); }

  22. If you had more than two buttons You would and more if statements or if - else statements

  23. Boolean variables boolean finished ; // can hold only true or false finished = true; if (finished) g.drawString (“Good bye” , 10,10); // OR if(finished = = true)

  24. ButtonOuch program -uses booleans //import usual stuff public class ButtonOuch extends Applet implements ActionListener { private Button tester; private boolean clickedYet = false; public void init ( ) { tester = new Button(“Press here”); add(tester); tester.addActionListner(this); }

  25. public void paint (Graphics g) { if (clickedYet) g.drawString(“Ouch “, 100, 50); } public void actionPerformed(ActionEvent event) { clickedYet = true; repaint ( ); } }

  26. Random Number Program (pseudo) // die = (int) (Math.random( ) * 6 ) + 1; // I thought that Math Library would have to be imported but not so… //import the usual stuff public class Dice2 extends Applet implements ActionListener { private Button throwDice; private boolean throw = false;

  27. public void init ( ) { throwDice = new Button(“Throw”); add(throwDice); throwDice.addActionListener(this); } public void actionPerformed(ActionEvent event) { thrown = true; repaint ( ); } public void paint (Graphics g) { int die1, die2; if(thrown) {

  28. die1 = (int) (Math.random( ) * 6 ) + 1; die2 = (int) (Math.random( ) * 6 ) + 1; g.drawString(“The dice are “ + die1 + “ and “ + dice2, 20, 40); if (die1 = = die2 ) g.drawString(“ a win “ , 20, 60); else g.drawString( “ Sorry “ , 20, 60) ; } } }

  29. New Program “Little and Large” //Purpose two buttons one makes a Circle larger, the other smaller //The usual import stuff public class LittleAndLarge extends Applet implements ActionListener {

  30. “Holy OOP’s program Batman to the Internet” http://www.cms.shu.ac.uk/java/ for the rest of chapter Seven

More Related