Download presentation
Presentation is loading. Please wait.
1
CH5 – Conditional Statements
Flavor 1: if ( <some boolean expression> ) { <some instruction list> } For now: these are method invokations (see next slide) Chapter 5 - IF
2
Let’s check the API to see our options
IF “predicates” either Tor F frontIsClear() nextToABeeper() nextToARobot() facingNorth() facingSouth() facingEast() facingWest() anyBeepersInBeeperBag() if ( ) { … } Robot Let’s check the API to see our options Chapter 5 - IF
3
again, you don’t write this class
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 Chapter 5 - IF
4
Examples if ( karel.frontIsClear() ) // in driver {
karel.move(); // no danger of hitting wall } if ( karel.anyBeepersInBeeperBag() ) // in driver { karel.putBeeper(); // no danger of error } Chapter 5 - IF
5
Draw the Inheritance Hierarchy
Extending Robot public class SmartBot extends Robot { public boolean beeperIsToLeft() {…} public boolean twoBeepersOnCornerOrMore() {…} public void faceEast() {…} } Draw the Inheritance Hierarchy Chapter 5 - IF
6
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 Chapter 5 - IF
7
you write twoBeepersOnCornerOrMore() it belongs to the SmartBot class note: you may have to nest if statements Chapter 5 - IF
8
you write faceEast() it belongs to the SmartBot class
Chapter 5 - IF
9
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 What annoying thing (should have) happened to you while coding the last few examples? UrRobot Robot SmartBot Yep, you wrote (or wanted to) turnRight() and maybe even turnAround() AGAIN! ANNOYING! Solution(s)? Chapter 5 - IF
10
Boolean Operators Karel (Java) Robot (&&, ||, !) if (! frontIsClear())
{ turnLeft(); } move(); // always going to move True turnLeft and move False move Ex) if (facingEast() || nextToABeeper()) if (!frontIsClear() && nextToARobot()) Chapter 5 - IF
11
&& and || (no, I didn’t stutter)
Write this method /** true if there is at least * one beeper on both sides of bot, * false otherwise */ public boolean isBeepOnLeftAndBeepOnRight() 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. Chapter 5 - IF
12
IF - ELSE Flavor 2: if ( <boolean expression> ) {
<statements> } else // do this instead <statements – somewhat different> Chapter 5 - IF
13
IF – ELSE Simplifications
simplify: if ( frontIsClear() ) { return true; } else return false; Chapter 5 - IF
14
Simplify if ( ! leftIsBlocked() ) { return true; } else return false;
Chapter 5 - IF
15
Simplify if ( leftIsBlocked() ) { return false; } else return true;
Chapter 5 - IF
16
Simplify – bottom factoring
move(); move(); if ( facingSouth() ) { turnLeft(); move(); } else turnRight(); if ( facingSouth() ) { turnLeft(); } else turnRight(); move(); move(); move(); move(); Chapter 5 - IF
17
Simplify – top factoring
if ( beeperOnLeft() ) { move(); turnLeft(); } else turnRight(); Chapter 5 - IF
18
being redundant again and again and againha ha
if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) turnLeft(); } Chapter 5 - IF
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.