30
Decisions - if and switch Chapter Seven

Decisions - if and switch

  • 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

Page 1: Decisions - if  and switch

Decisions - if and switch

Chapter Seven

Page 2: Decisions - if  and switch

NO YES

Is A >B

F T

Program

Rest of Pgm

Page 3: Decisions - if  and switch

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

Page 4: Decisions - if  and switch

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;

Page 5: Decisions - if  and switch

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

Page 6: Decisions - if  and switch

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

Page 7: Decisions - if  and switch

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

Page 8: Decisions - if  and switch

OR Gate

OUT A B OUT

F T TT F T

T T T

F F F

Page 9: Decisions - if  and switch

NOT Gate

INOUT

IN OUT

F T

T F

Page 10: Decisions - if  and switch

Relational operators

= = Equal to!= Not Equal< = Less that equal to> = Greater than equal to< Less than> Greater than

Page 11: Decisions - if  and switch

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

Page 12: Decisions - if  and switch

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

Page 13: Decisions - if  and switch

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

Page 14: Decisions - if  and switch

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

Page 15: Decisions - if  and switch

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;

Page 16: Decisions - if  and switch

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}

Page 17: Decisions - if  and switch

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

Page 18: Decisions - if  and switch

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

} }

Page 19: Decisions - if  and switch

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

Page 20: Decisions - if  and switch

Public void actionPerformed(ActionEvent event) {count++;repaint( );

}public void paint (Graphics g) {

g.drawString (“Number of button presses is “ + count, 10,50);

} }

Page 21: Decisions - if  and switch

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

Page 22: Decisions - if  and switch

If you had more than two buttons

You would and more if statements or if - else statements

Page 23: Decisions - if  and switch

Boolean variables

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

Page 24: Decisions - if  and switch

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

Page 25: Decisions - if  and switch

public void paint (Graphics g) {if (clickedYet)

g.drawString(“Ouch “, 100, 50); }public void actionPerformed(ActionEvent event)

{clickedYet = true;repaint ( );

} }

Page 26: Decisions - if  and switch

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;

Page 27: Decisions - if  and switch

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) {

Page 28: Decisions - if  and switch

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

} } }

Page 29: Decisions - if  and switch

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 {

Page 30: Decisions - if  and switch

“Holy OOP’s program Batman to the Internet”

http://www.cms.shu.ac.uk/java/

for the rest of chapter Seven