Robot

Karel – Chapter 5
Conditionally Executing Instructions
Note: Original slides provided by www.apComputerScience.com
and modified for Mr. Smith’s AP Computer Science A class
1
CH5 – Conditional Statements
Version 1:
Boolean expressions
return true or false
if ( <some boolean expression> )
{
<some instruction list> For now: these
are method
}
invocations (see
next slide)
2
if statement
“predicates”
frontIsClear()
if (
{
…
}
)
nextToABeeper()
either true or
false
nextToARobot()
facingNorth()
facingSouth()
Robot
facingEast()
facingWest()
Let’s check
the API to see
our options
anyBeepersInBeeperBag()
3
Robot Class
public class Robot extends UrRobot
{
public boolean frontIsClear() {…}
Now I
a
public boolean nextToABeeper() {…}have
brain!
public boolean nextToARobot() {…}
etc…
} again, you don’t
write this class
4
Examples from a client program
if ( karel.frontIsClear() )
{
karel.move(); // no danger of hitting wall
}
The client has to refer to the object it is acting on
if ( karel.anyBeepersInBeeperBag() )
{
karel.putBeeper(); // no danger of error
}
5
Examples from an object class
if ( frontIsClear() )
{
move(); // no danger of hitting wall
}
if (anyBeepersInBeeperBag() )
{
putBeeper();
// no danger of error
}
6
Extending Robot
public class SmartBot extends Robot
{
public boolean beeperIsToLeft() {…}
public boolean twoBeepersOrMoreOnCorner() {…}
public void faceEast() {…}
}
Draw the Inheritance
Hierarchy
7
Creating a boolean method
public boolean methodName()
{
The method must return either true or false
// other instructions could go here
if ( <some boolean expression> )
{
return true;
}
Returns true and ends the method
return false;
}
Returns false and ends the method
8
public boolean beeperIsToLeft()
MUST put world
{
back in initial
turnLeft();
situation that it was
in BEFORE the
move();
method was invoked
if ( nextToABeeper() )
{
turnLeft(); turnLeft(); move(); turnLeft();
return true;
}
turnLeft(); turnLeft(); move(); turnLeft();
return false;
}
9
SmartBot
Create a SmartBot class that extends Robot
You write these methods:
beeperIsToLeft()
(see
previous slide)
beeperIsToRight()
(this method will return true if at least one beeper is on corner right of robot)
twoBeepersOrMoreOnCorner()
(this method will return true if at least two beepers are on corner)
Note: you may have to nest if statements – look at page 118 in Karel textbook
faceEast(), faceNorth(), faceSouth(), faceWest()
(these methods will force the robot to turn in the indicated direction)
Note: Look at page 112 in Karel textbook
Also use the SmartBotTester to test SmartBot and
these new methods. You should download and use the
world file named smartbotWorld.txt.
10
Paying Attention?
• For the last several slides, we’ve been using a
new robot class. By now you’ve probably figured
out that our Inheritance Structure looks like this:
UrRobot
Robot
What annoying thing (should
have) happened to you while
coding the last few examples?
SmartBot
BetterRobot
Yep, you wrote (or wanted to)
turnAround() and maybe even
turnRight() AGAIN! ANNOYING!
Solution(s)?
12
What if there is only one
statement in the THEN clause?
if ( frontIsClear())
{
move();
}
is the same as …..
if ( frontIsClear())
move();
if ( frontIsClear())
{
move();
turnLeft();
}
is NOT the same as …..
if ( frontIsClear())
move();
turnLeft();
13
Nested IF
(IF statement inside an IF statement)
if ( frontIsClear())
{
move();
if ( nextToABeeper())
{
pickBeeper();
}
}
14
public boolean exactlyOneBeeperOnCorner()
{
if (nextToABeeper())
{
pickBeeper();
if (nextToABeeper())
{
putBeeper();
return false;
}
putBeeper();
return true;
}
return false;
}
15
Check the lines of code that would execute for each scenario
0
1
2
Beepers
Beeper
Beepers
public boolean exactlyOneBeeperOnCorner()
{
if (nextToABeeper())
{
pickBeeper();
if (nextToABeeper())
{
putBeeper();
return false;
}
putBeeper();
return true;
}
return false;
}
16
Check the lines of code that would execute for each scenario
0
1
2
Beepers
Beeper
Beepers
l
l
l
l
l
l
l
l
l
l
l
l
l
l
pickBeeper();
l
l
if (nextToABeeper())
l
l
{
public boolean exactlyOneBeeperOnCorner()
{
if (nextToABeeper())
{
l
putBeeper();
l
return false;
l
}
l
putBeeper();
l
return true;
l
}
l
return false;
l
l
l
}
17
Boolean Operators
• Java uses same boolean operators as C++
(&&, ||, !)
• && means AND
• || means OR
• ! means NOT
• Example:
if (! frontIsClear() || facingSouth())
{
turnLeft();
}
move();
18
if (frontIsClear() && nextToABeeper())
frontIsClear()
true
true
false
false
nextToABeeper()
true
false
true
false
result
true
false
false
false
if (frontIsClear() || nextToABeeper())
frontIsClear()
true
true
false
false
nextToABeeper()
true
false
true
false
result
true
true
true
false
19
IF - ELSE
Version 2:
if ( <boolean expression> )
{
<statements>
}
else
{
<statements – somewhat different>
}
20
IF - ELSE
Example:
if ( beeperIsToLeft() )
{
turnLeft();
move();
pickBeeper();
}
else
{
move();
}
21
Practice Using && and ||
Write this method which could be put in SmartBot
/*
returns true if there is at least
one beeper on both sides of bot,
false otherwise
*/
public boolean beeperOnLeftAndRight()
{
}
22
Look at ex. 9 in the book (pages 132-133)
Initial Situation
End Situation
Create a single followWallRight() method to handle each of these situations.
Hint: Before coding, look at the four situations and see what is the same and different for each.
Start with the initial situation for each robot. How could you use an if statement to determine if
they are in a specific situation?
This can be done with an if statement that includes nested if statements.
23
IF – ELSE Simplifications
simplify:
if ( frontIsClear() )
{
return true;
}
else
{
return false;
}
24
IF – ELSE Simplifications
simplify:
if ( frontIsClear() )
{
return true;
}
else
{
return false;
}
One option:
if ( frontIsClear() )
{
return true;
}
return false;
Or even better:
return frontIsClear();
25
Simplify
if ( ! leftIsBlocked() )
{
return true;
}
else
{
return false;
}
26
Possible Simplifications
if ( ! leftIsBlocked() )
{
return true;
}
else
{
return false;
}
One option:
if ( leftIsBlocked() )
{
return false;
}
return true;
Or even better:
return ! leftIsBlocked();
27
Simplify – bottom factoring
if ( facingSouth() )
{
turnLeft();
move();
}
else
{
turnRight();
move();
}
if ( facingSouth() )
{
turnLeft();
}
else
{
turnRight();
}
move();
28
Simplify – top factoring
if ( beeperOnLeft() )
{
move();
turnLeft();
}
else
{
move();
turnRight();
}
29
Top factoring does not work here
if ( beeperOnLeft() )
{
move();
turnLeft();
}
else
{
move();
turnRight();
}
move();
if ( beeperOnLeft() )
{
turnLeft();
}
else
{
turnRight();
}
Moves to new corner before
checking for beeper
30
However, top factoring does work here
if ( nextToABeeper() )
{
turnLeft();
pickBeeper();
}
else
{
turnLeft();
move();
}
turnLeft();
if (nextToABeeper() )
{
pickBeeper();
}
else
{
move();
}
turnLeft() does not affect
whether robot is next to a
beeper
31
Being redundant again and
again and againha ha
if ( facingNorth() )
{
move();
pickTwoBeepers();
if (facingNorth())
{
turnLeft();
}
}
This if statement is redundant
32
Here is better code
(unless one of the instructions can cause a change
in direction)
if ( facingNorth() )
{
move();
pickTwoBeepers();
if (facingNorth())
{
turnLeft();
}
}
if ( facingNorth() )
{
move();
pickTwoBeepers();
turnLeft();
}
33