Upload
yitta
View
27
Download
0
Embed Size (px)
DESCRIPTION
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) - PowerPoint PPT Presentation
Citation preview
Decisions - if and switch
Chapter Seven
NO YES
Is A >B
F T
Program
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)text fieldsrandom numbers
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;
//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) ; }}
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
AND OR NOT OPERATORS
AND = && TWO AMPERSANDSOR = || TWO PIPE SYMBOLSNOT = ! (EXCLAMATION POINT)
ANDA
B
OUTA B OUT
F F FF T FT F F
T T T
OR Gate
OUT A B OUT
F T TT F T
T T T
F F F
NOT Gate
INOUT
IN OUT
F T
T F
Relational operators
= = Equal to!= Not Equal< = Less that equal to> = Greater than equal to< Less than> Greater than
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 initalizedpublic void init ( ) {die1 = new Scrollbar (Scrollbar.HORIZONTAL,1 ,1,1,6);add(die1);} // NOTE die2 = almost same
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( ); } }
Options to the Dice gameIf (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);
Nested if statements
If ((age > 6 )if (age < 16) g.drawString(“Junior rate”, 50, 50);elseg.drawString(“adult rate “ , 50, 50);
// could be done with two separate if //statements
Tom and Jerry
//A story of two scrollbars// the usual importspublic class TomAndJerry extends Applet
implements AdjustmentListener {private Scrollbar tom, jerry;private int tomValue = 0,
jerryValue = 0;
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}
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);}
Public void adjustmentValueChanged (AdjustmentEvent event) {tomValue = tom.getValue( );jerryValue = jerry.getValue( );repaint ( );
} }
Using Buttons //usual importspublic 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);}
Public void actionPerformed(ActionEvent event) {count++;repaint( );
}public void paint (Graphics g) {
g.drawString (“Number of button presses is “ + count, 10,50);
} }
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 ( ); }
If you had more than two buttons
You would and more if statements or if - else statements
Boolean variables
boolean finished ; // can hold only true or falsefinished = true;if (finished) g.drawString (“Good bye” , 10,10);// OR if(finished = = true)
ButtonOuch program -uses booleans//import usual stuffpublic 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); }
public void paint (Graphics g) {if (clickedYet)
g.drawString(“Ouch “, 100, 50); }public void actionPerformed(ActionEvent event)
{clickedYet = true;repaint ( );
} }
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 stuffpublic class Dice2 extends Applet implements
ActionListener {private Button throwDice;private boolean throw = false;
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) {
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);
elseg.drawString( “ Sorry “ , 20, 60) ;
} } }
New Program “Little and Large”
//Purpose two buttons one makes a Circle larger, the other smaller
//The usual import stuffpublic class LittleAndLarge extends Applet
implements ActionListener {
“Holy OOP’s program Batman to the Internet”
http://www.cms.shu.ac.uk/java/
for the rest of chapter Seven