









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!










bagelsToWall(); tree(4,20,40,0.7); cornerPush(4,di);
Recursive Pictures: upPush() upPush(4,di) upPush(5,hex)
Designing upPush() with an Invocation Tree Let’s draw an invocation tree for upPush(3,di):
Defining rightPush() in Java
Recursive Pictures: cornerPush() cornerPush(4,di) cornerPush(5,hex)
Defining cornerPush() in Java
DistanceWorld Diana buggle wants to find the number of steps to the wall.
position heading
color red brushDown false The First Invocation
DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}
this (^) DB
position heading
color red brushDown false Making the Problem Smaller
DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}
this (^) DB
if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} position heading
color red brushDown false Smaller Again
DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}
this (^) DB this (^) DB
if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} position heading
color red brushDown false Even Smaller
DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}
this (^) DB this (^) DB if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} this (^) DB
if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} position heading
color red brushDown false Saving the Answer
DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}
this (^) DB this (^) DB if (isFacingWall()){ return 0; } else { forward(); int ans = 1 ; backward(); return ans;} this (^) DB
ans^1
if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} position heading
color red brushDown false Backing Up
DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}
this (^) DB this (^) DB if (isFacingWall()){ return 0; } else { forward(); int ans = 1 ; backward(); return ans;} this (^) DB
ans 1
if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;} position heading
color red brushDown false About to Return from the Penultimate Case
DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}
this (^) DB this (^) DB if (isFacingWall()){ return 0; } else { forward(); int ans = 1 ; backward(); return 1 ;} this (^) DB
ans^1
if (isFacingWall()){ return 0; } else { forward(); int ans = 1+ 1 ; backward(); return ans;} position heading
color red brushDown false The Story Continues
DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 1+stepsToWall(); backward(); return ans;}
this (^) DB this (^) DB
position heading
color red brushDown false The Final Return
DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println(diana.stepsToWall()); this diana Object Land Execution Land if (isFacingWall()){ return 0; } else { forward(); int ans = 3 ; backward(); return 3 ;}
this (^) DB
ans^3
position heading
color red brushDown false The Answer can be Displayed
DistanceBuggle diana = new DistanceBuggle(); Diana.setPosition(4,2); diana.brushUp(); System.out.println( 3 ); this diana Object Land Execution Land DW DB
Which of the Following Work? public int stepsToWall2() { if (isFacingWall()) { return 0; } else { forward(); int subSteps = stepsToWall2(); backward(); return 1+subSteps; } } public int stepsToWall4() { int n = 0; if (!isFacingWall()) { forward(); n = n + 1; stepsToWall4(); backward(); } return n; } public int stepsToWall3() { int n = 0; if (!isFacingWall()) { forward(); n = 1+stepsToWall3(); backward(); } return n; } public int n = 0; // extra instance variable public int stepsToWall5() { if (!isFacingWall()) { forward(); n = n + 1; stepsToWall5(); backward(); } return n; }
BagelCountingWorld
public int bagelsInFront(); Returns the number of bagels between this buggle and the wall it is facing, excluding any bagel that might be under it. Maintains position, heading, color, and brush state of buggle as invariants. public int bagelsUnderAndInFront(); Returns the number of bagels between this buggle and the wall it is facing, including any bagel that might be under it. Maintains position, heading, color, and brush state of buggle as invariants. Both solutions are similar to stepsToWall(). Here’s a useful helper method: