Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)

Similar presentations


Presentation on theme: "1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)"— Presentation transcript:

1

2 1 Chapter 5 Karel J Robot

3 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)

4 3 Chapter 5 IF if ( ) { … } frontIsClear(); nextToABeeper(); nextToARobot(); facingNorth(); facingSouth(); facingEast(); facingWest(); anyBeepersInBeeperBag(); Robot “predicates” either T or F Note: nextTo means on the same corner (not a block away)

5 4 Chapter 5 Robot Class Then: public class ___ extends UrRobot Now: public class ___ extends Robot The Robot Class is provided by karel. It extends UrRobot Now I have a brain!

6 5 Chapter 5 Robot Class public class Robot extends UrRobot { public boolean frontIsClear() {…} public boolean nextToABeeper() {…} public boolean nextToARobot() {…} etc… } Now I have a brain! Note: boolean, not void. Returns boolean values (true or false) Remember, you don’t write the Robot class

7 6 Chapter 5 Examples if (frontIsClear() ) { move();// no danger of hitting wall } if ( anyBeepersInBeeperBag() ) { putBeeper();// no danger of error }

8 7 Chapter 5 public boolean beeperIsToLeft() { turnLeft(); move(); if ( nextToABeeper() ) { turnLeft(); turnLeft(); move(); turnLeft(); return true; } turnLeft(); turnLeft(); move(); turnLeft(); return false; } MUST put world back in initial situation that it was in BEFORE the method was invoked Pg 107

9 8 Chapter 5 Boolean Operators Karel (Java) Robot (!, || &&) –if (! frontIsClear()) { turnLeft(); } move(); ! = Not || = Or && = And Pg 120-121

10 9 Chapter 5 Boolean Operators Karel (Java) Robot (!, || &&) –if (frontIsClear() && (nextToABeeper()) { pickBeeper(); turnLeft(); } move(); ! = Not || = Or && = And Pg 120-121

11 10 Chapter 5 Boolean Operators Karel (Java) Robot (!, || &&) –if (frontIsClear() || (nextToABeeper()) { turnLeft(); } move(); ! = Not || = Or && = And Pg 120-121

12 11 Chapter 5 IF - ELSE Flavor 2: if ( ) { } else { }

13 12 Chapter 5 IF – ELSE Simplifications simplify: if ( frontIsClear() ) { return true; } else { return false; } simplify: if ( frontIsClear() ) { return true; } return false;

14 13 Chapter 5 Simplify – bottom factoring if ( facingSouth() ) { turnLeft(); move(); } else { turnRight(); move(); } if ( facingSouth() ) { turnLeft(); } else { turnRight(); } move(); move();move();move();

15 14 Chapter 5 Simplify – top factoring if ( beeperOnLeft() ) { move(); turnLeft(); } else { move(); turnRight(); } move(); if ( beeperOnLeft() ) { turnLeft(); } else { turnRight(); }

16 15 Chapter 5 no need to check for the same thing twice! if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) { turnLeft(); } if ( facingNorth() ) { move(); pickTwoBeepers(); turnLeft(); }

17 16 Chapter 5


Download ppt "1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)"

Similar presentations


Ads by Google