



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: LAB:Comp Program & Prob Solv; Subject: Computer Science; University: Wellesley College; Term: Fall 2007;
Typology: Study notes
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















bagelForward(3)
Write a method that teaches a buggle to leave behind a trail
of bagels of a given length. Assume brushUp().
We could solve …
BagelWorld
BagelBuggle betty = new BagelBuggle(); betty.brushUp(); betty.bagelForward(3);
Object Land
Execution
Land
JEM demonstrating bagelForward method
this
BagelWorld
BagelBuggle betty = ; betty.brushUp(); betty.bagelForward(3);
Object Land
Execution
Land
A buggle named betty is constructed
this
betty BB
color red
brushDown true
BagelBuggle BB
heading EAST
position (1,1)
BagelWorld
BagelBuggle betty = ; .brushUp(); betty.bagelForward(3);
Object Land
Execution
Land
betty raises her brush
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (1,1)
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
The message bagelForward(3) is sent to betty
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (1,1)
this
if (n==0) { } else { dropBagel(); forward(); bagelForward(n-1); }
BB1 n 3
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
Drop a bagel and move forward
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (2,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(n-1); }
BB1 n 3
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
Evaluate the method’s argument
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (2,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(2); }
BB1 n 3
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
Execute the method bagelForward(2)
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (2,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(2); }
BB1 n 3
this
if (n==0) { } else { dropBagel(); forward(); bagelForward(n-1); }
BB1 n 2
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
Evaluate the boolean expression and execute the else clause
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (2,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(2); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelForward(n-1); }
BB1 n 2
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
Execute the method bagelForward(1)
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (3,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(2); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelForward(1); }
BB1 n 2
this
if (n==0) { } else { dropBagel(); forward(); bagelForward(n-1); }
BB1 n 1
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
Evaluate the boolean expression and execute the else clause
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (3,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(2); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelForward(1); }
BB1 n 2
this
if (false) { } else { dropBagel(); forward(); bagelForward(n-1); }
BB1 n 1
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
Drop a bagel and move forward
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (4,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(2); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelForward(1); }
BB1 n 2
this
if (false) { } else { dropBagel(); forward(); bagelForward(n-1); }
BB1 n 1
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
Evaluate the method’s argument
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (4,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(2); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelForward(1); }
BB1 n 2
this
if (false) { } else { dropBagel(); forward(); bagelForward(0); }
BB1 n 1
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
The bagelForward(0) execution frame finishes
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (4,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(2); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelForward(1); }
BB1 n 2
this
if (false) { } else { dropBagel(); forward(); bagelForward(0); }
BB1 n 1
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
The bagelForward(1) execution frame finishes
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (4,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(2); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelForward(1); }
BB1 n 2
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
The bagelForward(2) execution frame finishes
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (4,1)
this
if (false) { } else { dropBagel(); forward(); bagelForward(2); }
BB1 n 3
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelForward(3);
Object Land
Execution
Land
The bagelForward(3) execution frame finishes
this
betty BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (4,1)
bagelLine(int n)
BagelWorld
BagelBuggle betty = new BagelBuggle(); betty.brushUp(); betty.bagelLine(3);
Object Land
Execution
Land
JEM demonstrating bagelLine method
this
BagelWorld
BagelBuggle betty = ; betty.brushUp(); betty.bagelLine(3);
Object Land
Execution
Land
A buggle named betty is constructed
this
betty BB
BB
color red
brushDown true
BagelBuggle BB
heading EAST
position (1,1)
BagelWorld
BagelBuggle betty = ; .brushUp(); betty.bagelLine(3);
Object Land
Execution
Land
betty raises her brush
this
betty BB
BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (1,1)
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelLine(3);
Object Land
Execution
Land
Evaluate the boolean expression, drop a bagel, move forward, and
execute bagelLine(1)
this
betty BB
BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (3,1)
this
if (false) { } else { dropBagel(); forward(); bagelLine(2); backward(); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelLine(1); backward(); }
BB1 n 2
this
if (n==0) { } else { dropBagel(); forward(); bagelLine(n-1); backward(); }
BB1 n 1
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelLine(3);
Object Land
Execution
Land
Evaluate the boolean expression, drop a bagel, move forward, and
execute bagelLine(0)
this
betty BB
BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (4,1)
this
if (false) { } else { dropBagel(); forward(); bagelLine(2); backward(); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelLine(1); backward(); }
BB1 n 2
this
if (false) { } else { dropBagel(); forward(); bagelLine(0); backward(); }
BB1 n 1
this
if (n==0) { } else { dropBagel(); forward(); bagelLine(n-1); backward(); }
BB1 n 0
if (true) { } else { dropBagel(); forward(); bagelLine(n-1); backward(); }
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelLine(3);
Object Land
Execution
Land
Evaluate the boolean expression and execute the then clause
this
betty BB
BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (4,1)
this
if (false) { } else { dropBagel(); forward(); bagelLine(2); backward(); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelLine(1); backward(); }
BB1 n 2
this
if (false) { } else { dropBagel(); forward(); bagelLine(0); backward(); }
BB1 n 1
this
BB1 n 0
BagelWorld
BagelBuggle betty = ; .brushUp(); .bagelLine(3);
Object Land
Execution
Land
The bagelLine(0) execution frame finishes
this
betty BB
BB
color red
brushDown false
BagelBuggle BB
heading EAST
position (4,1)
this
if (false) { } else { dropBagel(); forward(); bagelLine(2); backward(); }
BB1 n 3
this
if (false) { } else { dropBagel(); forward(); bagelLine(1); backward(); }
BB1 n 2
this
if (false) { } else { dropBagel(); forward(); bagelLine(0); backward(); }
BB1 n 1