


























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!



























Friday, Sep. 14, 2007
Methods 4-
Methods 4-
One stair of a fixed size
public class StairWorld extends BuggleWorld { ...
public void run() { StairBuggle stepper = new StairBuggle(); stepper.buildStair(); } }
class StairBuggle extends Buggle { public void buildStair() { this.left(); this.forward(4); this.right(); this.forward(4); } }
Methods 4-
One stair, and we choose the size
public class StairWorld extends BuggleWorld { ...
public void run() { StairBuggle stepper = new StairBuggle(); stepper.buildStair(5); } }
class StairBuggle extends Buggle { public void buildStair(int stairSize) { this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1); } }
Methods 4-
Begin with the first statement in run()
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); stepper.buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
Methods 4-
Create a StairBuggle and go to the second statement
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); stepper.buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
stepper
Methods 4-
Request that the first step be built
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
stepper
Methods 4-
Create an execution frame for buildStair(5)
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1);
this
.buildStair(5)
stairSize 5
stepper
Methods 4-
Evaluate the receiver, this, in the second statement
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1);
this
.buildStair(5)
stairSize 5
stepper
Methods 4-
Evaluate the argument expression stairSize - 1
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
.left(); .forward(stairSize-1); this.right(); this.forward(stairSize-1);
this
.buildStair(5)
stairSize 5
stepper
Methods 4-
Evaluate the argument subexpression stairSize
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
.left(); .forward(stairSize-1); this.right(); this.forward(stairSize-1);
this
.buildStair(5)
stairSize 5
stepper
Methods 4-
Evaluate the argument expression 5 - 1
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
.left(); .forward(5-1); this.right(); this.forward(stairSize-1);
this
.buildStair(5)
stairSize 5
stepper
Methods 4-
Turn right and start the fourth statement
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
.left(); .forward(4); .right(); this.forward(stairSize-1);
this
.buildStair(5)
stairSize 5
stepper
Methods 4-
Done with the first step!
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
.left(); .forward(4); .right(); .forward(4);
this
.buildStair(5)
stairSize 5
stepper
Methods 4-
Request that the second step be built
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); stepper.buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
stepper
Methods 4-
Create an execution frame for buildStair(4)
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); .buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1);
this
.buildStair(4)
stairSize 4 stepper
Methods 4-
Turn right
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); .buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
.left(); .forward(3); .right(); this.forward(stairSize-1);
this
.buildStair(4)
stairSize 4
SB SB SB
stepper
Methods 4-
Draw the horizontal to finish the second stair
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); .buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
.left(); .forward(3); .right(); .forward(3);
this
.buildStair(4)
stairSize 4
SB SB SB SB
stepper
Methods 4-
Request that the third step be built
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); .buildStair(4); stepper.buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
stepper
Methods 4-
Create an execution frame for buildStair(3)
Object Land
Execution Land
StairWorld
StairBuggle stepper = new StairBuggle(); .buildStair(5); .buildStair(4); .buildStair(3);
this
.run()
position
heading
color
brushDown true
StairBuggle
red
this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1);
this
.buildStair(3)
stairSize 3 stepper
Methods 4-
A void method
/ tells the StairBuggle to draw a stair /
public void buildStep(int stairSize) { this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1); }
reserved word void says method does not return a value
Methods 4-
A fruitful method
/ tells the StairBuggle to draw a stair and to return the StairBuggle’s position after completing the stair. /
public buildStep(int stairSize) { this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1);
}
Methods 4-
A fruitful method
/ tells the StairBuggle to draw a stair and to return the StairBuggle’s position after completing the stair. /
public Location buildStep(int stairSize) { this.left(); this.forward(stairSize-1); this.right(); this.forward(stairSize-1); return this.getPosition(); }
Methods 4-
Jack and Jill go up the hill
Methods 4-
Suppose run() is invoked for an instance of StairWorld
Object Land
Execution Land
StairWorld
StairBuggle jack = new StairBuggle(); StairBuggle jill = new StairBuggle(); jill.setColor(Color.blue); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3));
this
.run()
Methods 4-
Jack is born
Object Land
Execution Land
StairWorld
StairBuggle jack = new StairBuggle(); StairBuggle jill = new StairBuggle(); jill.setColor(Color.blue); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3));
this
.run()
jack
position
heading
color
brushDown true
StairBuggle
red
Methods 4-
Jill is Born (rooming with Jack)
Object Land
Execution Land
StairWorld
StairBuggle jack = new StairBuggle(); StairBuggle jill = new StairBuggle(); jill.setColor(Color.blue); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3));
this
.run()
jack
position
heading
color
brushDown true
StairBuggle
red
position
heading
color
brushDown true
StairBuggle
red
jill
Methods 4-
Jill feels blue
Object Land
Execution Land
StairWorld
StairBuggle jack = new StairBuggle(); StairBuggle jill = new StairBuggle(); .setColor(Color.blue); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3)); jill.setPosition(jack.buildStair(3)); jack.setPosition(jill.buildStair(3));
this
.run()
jack
position
heading
color
brushDown true
StairBuggle
red
position
heading
color
brushDown true
StairBuggle
blue
jill