CS111 Lab 6

Wednesday, March 5, 2003

Plan of the lab:

Some helpful material: Thinking about Recursion

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, and 3. 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 -- Brief introduction to TurtleWorld

Turtle drawing primitives include the following:
   public void fd (double n)
   Move the turtle forward n steps. 
   
   public void bd (double n); 
   Move the turtle backward n steps.
   
      public void lt (double angle);
   Turn the turtle to the left angle degrees.  
   
   public void rt (double angle);
   Turn the turtle to the right angle degrees. 
   
   public void pu (); 
   Raise the turtle's pen up.
   
      public void pd (); 
   Lower the turtle's pen down.
Additionally, there are also versions of fd, bd, lt, and rt that take int parameters, so you can invoke these methods with either an integer or double floating-point value.

You should not need to use any other Turtle primitives other than those listed above. In fact, many solutions use only a subset of the primitives listed above.

Test your definition by specifying levels and side in the parameter window and then clicking on the Run button in the TurtleWorld window. The Reset button will clear the screen. Good parameter values are in the ranges [0 ... 8] for levels and [100 ... 400] for side. If your program hangs, you may need to "force quit" it by depressing the option, apple, and escape keys all at the same time. If that doesn't help, restart the computer by pressing the little button with triangle on the machine itself (under your desk).

Part 3 -- Exercises

Most of the programming problems for this lab take place in extensions of TurtleWorld. The code is in the lab6_programs folder in the cs111d account on cs.wellesley.edu. The problems are arranged in order of difficulty with the easiest problem first. The last problem is a BuggleWorld recursion problem. Note that the exercises are challenging, so don't be disappointed if you don't get to the last ones.

  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 lab6_programs folder.