CH4 – Conditional Statements

Download Report

Transcript CH4 – Conditional Statements

CH5 – Conditional Statements
Flavor 1:
if ( <some boolean expression> )
{
For now: these are
<some instruction list>
method invokations
(see next slide)
}
Chapter 5 - IF
1
IF
“predicates”
frontIsClear();
if ( )
{
…
}
either Tor F
nextToABeeper();
nextToARobot();
facingNorth();
facingSouth();
Robot
facingEast();
facingWest();
Let’s check the
API to see our
options
anyBeepersInBeeperBag();
Chapter 5 - IF
2
Robot Class
public class Robot extends UrRobot
{
public boolean frontIsClear() {…} Now I
have a
public boolean nextToABeeper() {…}
brain!
public boolean nextToARobot() {…}
etc…
} again, you don’t
write this class
Chapter 5 - IF
3
Examples
if ( karel.frontIsClear() )
{
karel.move(); // no danger of hitting wall
}
if ( karel.anyBeepersInBeeperBag() )
{
karel.putBeeper();
// no danger of error
}
Chapter 5 - IF
4
Extending Robot
public class SmartBot extends Robot
{
public boolean beeperIsToLeft() {…}
public boolean twoBeepersOnCornerOrMore() {…}
public void faceEast() {…}
}
Draw the Inheritance
Hierarchy
Chapter 5 - IF
5
public boolean beeperIsToLeft()
{
turnLeft();
move();
if ( nextToABeeper() )
{
MUST put world
back in initial
situation that it
was in BEFORE
the method was
invoked
turnLeft(); turnLeft(); move(); turnLeft();
return true;
}
turnLeft(); turnLeft(); move(); turnLeft();
return false;
}
Chapter 5 - IF
6
you write
twoBeepersOnCornerOrMore()
it belongs to the SmartBot class
note: you may have to nest if
statements
Chapter 5 - IF
7
you write
faceEast()
it belongs to the SmartBot class
Chapter 5 - IF
8
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
What annoying thing (should
have) happened to you while
coding the last few examples?
Robot
SmartBot
Yep, you wrote (or wanted to)
turnRight() and maybe even
turnAround() AGAIN! ANNOYING!
Solution(s)?
Chapter 5 - IF
9
Boolean Operators
• Same as C++ (&&, ||, !)
• Karel (Java) Robot (&&, ||, !)
– if (! frontIsClear())
{
turnLeft();
}
move();
Chapter 5 - IF
10
&& 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.
Chapter 5 - IF
11
IF - ELSE
Flavor 2:
if ( <boolean expression> )
{
<statements>
}
else
{
<statements – somewhat different>
}
Chapter 5 - IF
12
IF – ELSE Simplifications
simplify:
if ( frontIsClear() )
{
return true;
}
else
{
return false;
}
Chapter 5 - IF
13
Simplify
if ( ! leftIsBlocked() )
{
return true;
}
else
{
return false;
}
Chapter 5 - IF
14
Simplify
if ( leftIsBlocked() )
{
return false;
}
else
{
return true;
}
Chapter 5 - IF
15
Simplify – bottom factoring
if ( facingSouth() )
{
turnLeft();
move();
}
else
{
turnRight();
move();
}
if ( facingSouth() )
{
turnLeft();
}
else
{
turnRight();
}
move();
Chapter 5 - IF
16
Simplify – top factoring
if ( beeperOnLeft() )
{
move();
turnLeft();
}
else
{
move();
turnRight();
}
Chapter 5 - IF
17
being redundant again and again
and againha ha
if ( facingNorth() )
{
move();
pickTwoBeepers();
if (facingNorth())
{
turnLeft();
}
}
Chapter 5 - IF
18