1 karel – chapter 5 conditionally executing instructions note: original slides provided by and...
TRANSCRIPT
![Page 1: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/1.jpg)
1
Karel – Chapter 5Conditionally Executing
Instructions
Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A class
![Page 2: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/2.jpg)
2
CH5 – Conditional Statements
Version 1:
if ( <some boolean expression> ){
<some instruction list>}
For now: these are method invocations (see next slide)
Boolean expressions return true or false
![Page 3: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/3.jpg)
3
if statement
if ( ){ … }
frontIsClear()
nextToABeeper()
nextToARobot()
facingNorth()
facingSouth()
facingEast()
facingWest()
anyBeepersInBeeperBag()
Robot
“predicates”
either true or false
Let’s check the API to see our options
![Page 4: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/4.jpg)
4
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
![Page 5: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/5.jpg)
5
Examples from a client program
if ( karel.frontIsClear() ){
karel.move(); // no danger of hitting wall
}
if ( karel.anyBeepersInBeeperBag() ){ karel.putBeeper(); // no danger of error}
The client has to refer to the object it is acting on
![Page 6: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/6.jpg)
6
Examples from an object class
if ( frontIsClear() ){
move(); // no danger of hitting wall}
if (anyBeepersInBeeperBag() ){ putBeeper(); // no danger of error}
![Page 7: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/7.jpg)
7
Extending Robot
public class SmartBot extends Robot{
public boolean beeperIsToLeft() {…}
public boolean twoBeepersOrMoreOnCorner() {…}public void faceEast() {…}
}Draw the
Inheritance Hierarchy
![Page 8: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/8.jpg)
8
Creating a boolean method
public boolean methodName(){ // other instructions could go here if ( <some boolean expression> ) {
return true; }
return false; }
The method must return either true or false
Returns true and ends the method
Returns false and ends the method
![Page 9: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/9.jpg)
9
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
![Page 10: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/10.jpg)
10
SmartBotCreate 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.
![Page 11: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/11.jpg)
12
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
SmartBot
What annoying thing (should have) happened to you while coding the last
few examples?Yep, you wrote (or wanted to) turnAround() and maybe even turnRight() AGAIN! ANNOYING!
Solution(s)?
BetterRobot
![Page 12: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/12.jpg)
13
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();
![Page 13: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/13.jpg)
14
Nested IF(IF statement inside an IF statement)
if ( frontIsClear()){
move(); if ( nextToABeeper()) { pickBeeper(); }
}
![Page 14: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/14.jpg)
15
public boolean exactlyOneBeeperOnCorner() {
if (nextToABeeper()){
pickBeeper();if (nextToABeeper()){
putBeeper();return false;
}putBeeper();return true;
}return false;
}
![Page 15: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/15.jpg)
16
0Beepers
1Beeper
2Beepers
public boolean exactlyOneBeeperOnCorner()
{
if (nextToABeeper())
{
pickBeeper();
if (nextToABeeper())
{
putBeeper();
return false;
}
putBeeper();
return true;
}
return false;
}
Check the lines of code that would execute for each scenario
![Page 16: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/16.jpg)
17
0Beepers
1Beeper
2Beepers
public boolean exactlyOneBeeperOnCorner()
{
if (nextToABeeper())
{
pickBeeper();
if (nextToABeeper())
{
putBeeper();
return false;
}
putBeeper();
return true;
}
return false;
}
Check the lines of code that would execute for each scenario
![Page 17: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/17.jpg)
18
Boolean Operators
• Java uses same boolean operators as C++
(&&, ||, !)• && means AND• || means OR• ! means NOT
• Example: if (! frontIsClear() || facingSouth())
{ turnLeft();
}move();
![Page 18: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/18.jpg)
19
if (frontIsClear() && nextToABeeper())
frontIsClear() nextToABeeper()true truetrue falsefalse truefalse false
if (frontIsClear() || nextToABeeper())
frontIsClear() nextToABeeper()true truetrue falsefalse truefalse false
resulttruefalsefalsefalse
resulttruetruetruefalse
![Page 19: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/19.jpg)
20
IF - ELSEVersion 2:if ( <boolean expression> ){
<statements>}else{
<statements – somewhat different>}
![Page 20: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/20.jpg)
21
IF - ELSEExample:if ( beeperIsToLeft() ){
turnLeft(); move(); pickBeeper();}else{
move();}
![Page 21: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/21.jpg)
22
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() {
}
![Page 22: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/22.jpg)
23
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.
Initial Situation End Situation
Look at ex. 9 in the book (pages 132-133)
![Page 23: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/23.jpg)
24
IF – ELSE Simplifications
simplify:if ( frontIsClear() ){
return true;}else{
return false;}
![Page 24: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/24.jpg)
25
IF – ELSE Simplifications
simplify:if ( frontIsClear() ){
return true;}else{
return false;}
One option:if ( frontIsClear() ){
return true;}return false;
Or even better:return frontIsClear();
![Page 25: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/25.jpg)
26
Simplify
if ( ! leftIsBlocked() ){
return true;}else{
return false;}
![Page 26: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/26.jpg)
27
Possible Simplifications
if ( ! leftIsBlocked() )
{return true;
}else{
return false;}
One option:if ( leftIsBlocked() ){
return false;}return true;
Or even better:return ! leftIsBlocked();
![Page 27: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/27.jpg)
28
Simplify – bottom factoring
if ( facingSouth() )
{turnLeft();move();
}
else{
turnRight();move();
}
if ( facingSouth() )
{
turnLeft();
}
else
{
turnRight();
}
move();
move();
move();
move();
move();
move();
![Page 28: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/28.jpg)
29
Simplify – top factoringif ( beeperOnLeft() ){
move();turnLeft();
}else{
move();turnRight();
}
![Page 29: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/29.jpg)
30
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
![Page 30: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/30.jpg)
31
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
turnLeft();
![Page 31: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/31.jpg)
32
Being redundant again and again and againha ha
if ( facingNorth() ){
move(); pickTwoBeepers(); if (facingNorth()) {
turnLeft(); }
}
This if statement is redundant
![Page 32: 1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer](https://reader035.vdocuments.site/reader035/viewer/2022070402/56649f265503460f94c3cfc0/html5/thumbnails/32.jpg)
33
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();
}