Download presentation
Presentation is loading. Please wait.
Published byMatthew Ryan Modified over 9 years ago
2
1 Chapter 5 - IF CH5 – Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
3
2 Chapter 5 - IF IF if ( ) { … } frontIsClear(); nextToABeeper(); nextToARobot(); facingNorth(); facingSouth(); facingEast(); facingWest(); anyBeepersInBeeperBag(); Robot “predicates” either Tor F Let’s check the API to see our options
4
3 Chapter 5 - IF Robot Class public class Robot extends UrRobot { public boolean frontIsClear() {…} public boolean nextToABeeper() {…} public boolean nextToARobot() {…} etc… } Now I have a brain! again, you don’t write this class
5
4 Chapter 5 - IF Examples if ( karel.frontIsClear() ) { karel.move();// no danger of hitting wall } if ( karel.anyBeepersInBeeperBag() ) { karel.putBeeper();// no danger of error }
6
5 Chapter 5 - IF Extending Robot public class SmartBot extends Robot { public boolean beeperIsToLeft() {…} public boolean twoBeepersOnCornerOrMore() {…} public void faceEast() {…} } Draw the Inheritance Hierarchy
7
6 Chapter 5 - IF 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
8
7 Chapter 5 - IF you write twoBeepersOnCornerOrMore() it belongs to the SmartBot class note: you may have to nest if statements
9
8 Chapter 5 - IF you write faceEast() it belongs to the SmartBot class
10
9 Chapter 5 - IF Paying Attention? For the last several slides, I’ve lead you somewhat astray. By now (I’m hoping) someone has pointed out that our Inheritance Structure looks likes this UrRobot Robot SmartBot What annoying thing (should have) happened to you while coding the last few examples? Yep, you wrote (or wanted to) turnRight() and maybe even turnAround() AGAIN! ANNOYING! Solution(s)?
11
10 Chapter 5 - IF Boolean Operators Same as C++ (&&, ||, !) Karel (Java) Robot (&&, ||, !) –if (! frontIsClear()) { turnLeft(); } move();
12
11 Chapter 5 - IF && and || (no, I didn’t stutter) Write this method /** * @returns true if there is at least * one beeper on both sides of bot, * false otherwise */ public boolean isBeepOnLeft_And_BeepOnRight() Assume that the class you’re writing this for has SmartBot as its superclass (the one we just suggested). It may help to look at the inheritance tree again.
13
12 Chapter 5 - IF IF - ELSE Flavor 2: if ( ) { } else { }
14
13 Chapter 5 - IF IF – ELSE Simplifications simplify: if ( frontIsClear() ) { return true; } else { return false; }
15
14 Chapter 5 - IF Simplify if ( ! leftIsBlocked() ) { return true; } else { return false; }
16
15 Chapter 5 - IF Simplify if ( leftIsBlocked() ) { return false; } else { return true; }
17
16 Chapter 5 - IF Simplify – bottom factoring if ( facingSouth() ) { turnLeft(); move(); } else { turnRight(); move(); } if ( facingSouth() ) { turnLeft(); } else { turnRight(); } move(); move();move();move();
18
17 Chapter 5 - IF Simplify – top factoring if ( beeperOnLeft() ) { move(); turnLeft(); } else { move(); turnRight(); }
19
18 Chapter 5 - IF being redundant again and again and again ha ha if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) { turnLeft(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.