CS111 Lab 5

Wednesday, October 18, 2000

Objectives

 

Part 1 -- JEM Diagrams for Recursive methods

Below are the declarations for two classes: a ThrowerWorld class that is a subclass of BuggleWorld and a ThrowerBuggle class that is a subclass of Buggle.

// a BuggleWorld in which Buggles throw Bagels
   public class ThrowerWorld extends BuggleWorld {
  public void run () {
    ThrowerBuggle tara = new ThrowerBuggle();
    tara.throwBagel(3);
  }
}
    
class ThrowerBuggle extends Buggle {
  public void throwBagel (int distance) {
    if (distance == 0) {
      dropBagel();
    } else {
      forward();
      throwBagel(distance - 1);
      backward();
    }
  }
}
    

Task 1a) Draw the result (in a ThrowerWorld grid) of asking tara to throw bagels at distances of 0, 1, 2, 3, 4, and 5. Use a different grid for each distance. The size of the grid doesn't matter (just make it large enough so the buggle doesn't run into walls). When given recursive code, it is usually a good idea to draw on paper a couple of instances of running the code beginning with the easiest case and working up.

Task 1b) Draw a Java Execution Model diagram that shows all of the execution frames created by invoking the run() method on an instance of the ThrowerWorld class. Your diagram should depict the point in time when the invocation of run() returns. Although Java can discard an execution frame when control returns from it, you should not discard any frames when drawing your diagram.

 

Part 2: Exercises

Most of the programming problems for this lab take place in extensions of TurtleWorld. The code is in the lab5_programs folder within the /usr/users/cs111/download directory on nike.wellesley.edu. The problems are arranged in order of difficulty with the easiest problem first. The last problem is a BuggleWorld recursion problem.

  1. BoomerangWorld
  2. EiffelWorld
  3. InvertedTrianglesWorld
  4. NestedWindowWorld
  5. NestedPolygonWorld
  6. ConcentricRugWorld

Working samples of the above worlds are available in the Test folder which is included in the lab5_programs folder.