










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











WallHuggerWorld
Straight-line code
forward();
forward();
left();
forward();
Cruelty to Buggles
Straight-line code
forward();
forward();
left();
forward();
Conditional statements*
class WallHugger extends Buggle {
// Move forward one step // unless facing a wall, // in which case turn left. public void followWall() {
if (isFacingWall()) { left(); } else { forward(); } } }
*In general, write: if (
Repeating ourselves
class WallHugger extends Buggle { public void followWall() { if (isFacingWall()) { left(); } else { forward(); } } }
public class WallHuggerWorld extends BuggleWorld { public void run () { WallHugger peterParker = new WallHugger(); peterParker.setPosition(new Location(5,3)); peterParker.followWall(); peterParker.followWall(); … } }
Repeating ourselves more succinctly
Capturing the pattern
WallHuggerWorld
WallHugger peterParker = peterParker.setPosition(new Location(5,3)); peterParker.tick4();
Object Land
Execution Land
A spider bites Peter
this
color red
brushDown true
WallHugger WH
heading EAST
position (1,1)
peterParker WH
WallHuggerWorld
WallHugger peterParker = .setPosition( ); peterParker.tick4();
Object Land
Execution Land
Peter jumps
this
color red
brushDown true
WallHugger WH
heading EAST
position
peterParker WH
WallHuggerWorld
WallHugger peterParker = .setPosition( ); .tick4();
Object Land
Execution Land
And the clock starts ticking...
this
color red
brushDown true
WallHugger WH
heading EAST
position
peterParker WH
this
tick2(); tick2();
WallHuggerWorld
WallHugger peterParker = .setPosition( ); .tick4();
Object Land
Execution Land
tick4() invokes tick2()
this
color red
brushDown true
WallHugger WH
heading EAST
position
peterParker WH
this
tick2(); tick2();
tick(); tick();
this
WallHuggerWorld
WallHugger peterParker = .setPosition( ); .tick4();
Object Land
Execution Land
The boolean expression is evaluated
this
color red
brushDown true
WallHugger WH
heading EAST
position
peterParker WH
this
tick2(); tick2();
tick(); tick();
this
followWall();
this WH
this
if (false) { left(); } else { forward(); }
WallHuggerWorld
WallHugger peterParker = .setPosition( ); .tick4();
Object Land
Execution Land
The “else” clause is executed
this
color red
brushDown true
WallHugger WH
heading EAST
position
peterParker WH
this
tick2(); tick2();
tick(); tick();
this
followWall();
this WH
this
if (false) { left(); } else { forward(); }
WallHuggerWorld
WallHugger peterParker = .setPosition( ); .tick4();
Object Land
Execution Land
And spidey moves forward
this
color red
brushDown true
WallHugger WH
heading EAST
position
peterParker WH
this
tick2(); tick2();
tick(); tick();
this
followWall();
this WH
this
if (false) { left(); } else { forward(); }
WallHuggerWorld
WallHugger peterParker = .setPosition( ); .tick4();
Object Land
Execution Land
followWall() goes away
this
color red
brushDown true
WallHugger WH
heading EAST
position
peterParker WH
this
tick2(); tick2();
tick(); tick();
this
followWall();
this WH
WallHuggerWorld
WallHugger peterParker = .setPosition( ); .tick4();
Object Land
Execution Land
And so does another followWall()
this
color red
brushDown true
WallHugger WH
heading EAST
position
peterParker WH
this
tick2(); tick2();
tick(); tick();
this
followWall();
this WH
this
if (isFacingWall()) { left(); } else { forward(); }
WallHuggerWorld
WallHugger peterParker = .setPosition( ); .tick4();
Object Land
Execution Land
The “if” clause is executed
this
color red
brushDown true
WallHugger WH
heading NORTH
position
peterParker WH
this
tick2(); tick2();
tick(); tick();
this
followWall();
this WH
this
if (true) { left(); } else { forward(); }
WallHuggerWorld
WallHugger peterParker = .setPosition( ); .tick4();
Object Land
Execution Land
Until eventually, tick4() finishes execution
this
color red
brushDown true
WallHugger WH
heading NORTH
position
peterParker WH
Bagels, bagels everywhere
BuggleWorld
RandomBagel World
WallHugger World
public class HungryWallHuggerWorld
extends RandomBagelWorld {
public void run () { HungryWallHugger peterParker = new HungryWallHugger(); peterParker.setPosition(new Location(5,3)); peterParker.tick128(); }
}
Put another way*
class HungryWallHugger extends TickBuggle { // If over a bagel, eat it. // Otherwise, turn left if // facing a wall, or move // forward if not facing a wall. public void followWall() {
if (isOverBagel()) { pickupBagel(); } else if (isFacingWall()) { left(); } else { forward(); } } }
*Multi-way conditionals.
Multi-way conditional idiom
true false
test1()
true false
test2()
test3()
false
. . .
. . . . . .
true
. . .
Eat and run
left();
isFacingWall()
forward();
true false
isOverBagel()
true false
pickUpBagel();
It’s okay to leave off the “else”
left();
isFacingWall()
forward();
true false
isOverBagel()
true false
pickUpBagel();