slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Karel – Chapter 5 Conditionally Executing Instructions PowerPoint Presentation
Download Presentation
Karel – Chapter 5 Conditionally Executing Instructions

Loading in 2 Seconds...

play fullscreen
1 / 32

Karel – Chapter 5 Conditionally Executing Instructions - PowerPoint PPT Presentation


  • 608 Views
  • Uploaded on

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. CH5 – Conditional Statements. Boolean expressions return true or false. Version 1:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Karel – Chapter 5 Conditionally Executing Instructions' - paul2


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

Karel – Chapter 5Conditionally Executing Instructions

Note: Original slides provided bywww.apComputerScience.comand modified for Mr. Smith’s AP Computer Science A class

ch5 conditional statements
CH5 – Conditional Statements

Boolean expressions return true or false

Version 1:

if ( <some boolean expression> )

{

<some instruction list>

}

For now: these are method invocations (see next slide)

if statement

“predicates”

either true or false

if statement

frontIsClear()

nextToABeeper()

nextToARobot()

facingNorth()

facingSouth()

facingEast()

facingWest()

anyBeepersInBeeperBag()

if ( )

{

}

Robot

Let’s check the API to see our options

robot 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

examples from a client program
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

}

examples from an object class
Examples from an object class

if ( frontIsClear() )

{

move(); // no danger of hitting wall

}

if (anyBeepersInBeeperBag() )

{

putBeeper(); //no danger of error

}

extending robot
Extending Robot

public class SmartBot extends Robot

{

public boolean beeperIsToLeft() {…}

public boolean twoBeepersOrMoreOnCorner() {…}

public void faceEast() {…}

}

Draw the Inheritance Hierarchy

creating a boolean method
Creating a booleanmethod

The method must return either true or false

public boolean methodName()

{

// other instructions could go here

if ( <some boolean expression> )

{

return true;

}

return false;

}

Returns true and ends the method

Returns false and ends the method

slide9

MUST put world back in initial situation that it was in BEFORE the method was invoked

public booleanbeeperIsToLeft()

{

turnLeft();

move();

if ( nextToABeeper() )

{

turnLeft(); turnLeft(); move(); turnLeft(); return true;

}

turnLeft(); turnLeft(); move(); turnLeft();

return false;

}

smartbot

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.

paying attention
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:

What annoying thing (should have) happened to you while coding the last few examples?

UrRobot

Robot

Yep, you wrote (or wanted to) turnAround() and maybe even turnRight() AGAIN! ANNOYING!

SmartBot

BetterRobot

Solution(s)?

what if there is only one statement in the then clause
What if there is only one statement in the THEN clause?

if ( frontIsClear())

{

move();

turnLeft();

}

is NOT the same as …..

if ( frontIsClear())

move();

turnLeft();

if ( frontIsClear())

{

move();

}

is the same as …..

if ( frontIsClear())

move();

nested if if statement inside an if statement
Nested IF(IF statement inside an IF statement)

if ( frontIsClear())

{

move();

if ( nextToABeeper())

{

pickBeeper();

}

}

slide14

exactlyOneBeeperOnCorner()

publicboolean exactlyOneBeeperOnCorner()

{

if (nextToABeeper())

{

pickBeeper();

if (nextToABeeper())

{

putBeeper();

returnfalse;

}

putBeeper();

returntrue;

}

returnfalse;

}

slide15

if (exactlyOneBeeperOnCorner())

Check the lines of code that would execute for each scenario

slide16

if (exactlyOneBeeperOnCorner())

Check the lines of code that would execute for each scenario

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

l

boolean operators
Boolean Operators
  • Java uses same boolean operators as C++ (&&, ||, !)
  • && means AND
  • || means OR
  • ! means NOT
  • Example:

if (! frontIsClear() || facingSouth())

{

turnLeft();

}

move();

slide18

Truth Tables

if (frontIsClear() && nextToABeeper())

frontIsClear()nextToABeeper()

true true

true false

false true

false false

if (frontIsClear() || nextToABeeper())

frontIsClear()nextToABeeper()

true true

true false

false true

false false

result

true

false

false

false

result

true

true

true

false

if else
IF - ELSE

Version 2:

if ( <boolean expression> )

{

<statements>

}

else

{

<statements – somewhat different>

}

if else1
IF - ELSE

Example:

if ( beeperIsToLeft() )

{

turnLeft();

move();

pickBeeper();

}

else

{

move();

}

practice using and
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()

{

}

slide22

MazeWalker

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.

if else simplifications
IF – ELSE Simplifications

simplify:

if ( frontIsClear() )

{

return true;

}

else

{

return false;

}

if else simplifications1
IF – ELSE Simplifications

One option:

if ( frontIsClear() )

{

return true;

}

return false;

Or even better:

return frontIsClear();

simplify:

if ( frontIsClear() )

{

return true;

}

else

{

return false;

}

simplify
Simplify

if ( ! leftIsBlocked() )

{

return true;

}

else

{

return false;

}

possible simplifications
Possible Simplifications

One option:

if ( leftIsBlocked() )

{

return false;

}

return true;

Or even better:

return ! leftIsBlocked();

if ( ! leftIsBlocked() )

{

return true;

}

else

{

return false;

}

simplify bottom factoring

move();

Simplify – bottom factoring

move();

if ( facingSouth() )

{

turnLeft();

}

else

{

turnRight();

}

if ( facingSouth() )

{

turnLeft();

move();

}

else

{

turnRight();

move();

}

move();

move();

move();

move();

simplify top factoring
Simplify – top factoring

if ( beeperOnLeft() )

{

move();

turnLeft();

}

else

{

move();

turnRight();

}

top factoring does not work here
Top factoringdoes not work here

move();

if ( beeperOnLeft() )

{

turnLeft();

}

else

{

turnRight();

}

if ( beeperOnLeft() )

{

move();

turnLeft();

}

else

{

move();

turnRight();

}

Moves to new corner before checking for beeper

however top factoring does work here

turnLeft();

However, top factoringdoes work here

turnLeft();

if (nextToABeeper() )

{

pickBeeper();

}

else

{

move();

}

if ( nextToABeeper() )

{

turnLeft();

pickBeeper();

}

else

{

turnLeft();

move();

}

turnLeft() does not affect whether robot is next to a beeper

being redundant again and again and again ha ha
Being redundant again and again and againha ha

if ( facingNorth() )

{

move();

pickTwoBeepers();

if (facingNorth())

{

turnLeft();

}

}

This if statement is redundant

here is better code unless one of the instructions can cause a change in direction
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();

}