CS111, Wellesley College, 2007

Lab 8

Wedn, Oct 24, 2007

Recurses, Executed Again!

Total checkmarks:

Today:


Unfurling a recursive method -- Boo!


public int boo (int n) {
    if (n<=2) {
      return n;
    } else {
      return n + boo(n/2) + boo(n/3);
    }
}

Run the method boo above by hand, to compute the following:

Note that integer division in Java yields integers, so, for example [10/3 is 3 (not 3.33) and 5/3 is 1 (not 1.66)].

Now let's write a Java application starting from an empty file (our very first one!) to implement that method, and run it in DrJava to verify our results.

How Many Steps To Wall??

Recall from the lecture on fruitful recursion, the stepsToWall() fruitful method that you studied. Here is that definition, again:


public int stepsToWall() () {
    if (isFacingWall()) {
      return 0;
    } else {
      forward();
      int steps = 1 + stepsToWall();
      backward();
      return steps;
    }
}

Study the following method definitions for solving the same problem, i.e. counting the steps to wall. Which ones do you think would work, and which would not? Feel free to work with a friend on this one!


public int stepsToWall2() () {
     int steps = 0;
     if (!isFacingWall()) {
        forward();
        steps = 1 + stepsToWall2();
        backward();
     }
     return steps;
}

public int stepsToWall3() () {
    int steps = 0;
    if (!isFacingWall()) {
      forward();
      steps = steps + 1;
      stepsToWall3();
      backward();
    } 
      return steps;
}


public int stepsToWall4() () {
    if (isFacingWall()) {
      int steps = 0;
      return steps;
    } else {
      forward();
      steps = 1 + stepsToWall4();
      backward();
      return steps;
    }
}

public int stepsToWall5() () {
    if (isFacingWall()) {
      return 0;
    } else {
      int steps = 1;
      forward();
      steps = steps + stepsToWall5();
      backward();
      return steps;
    }
}


HungryWorld

Look in the HungryWorld folder inside lab7_programs.

Harry is a hungry, but very eccentric buggle. He always starts out in the middle of the first row of a grid facing NORTHwith his brush up (can you hear his stomach rumbling?). The grid cells are randomly populated with a user-specified number of bagels, but no bagels are in the column Harry starts out in. The buggle moves row by row from the bottom to the top, and performs the following action at each row:


Initial grid configuration


Final grid configuration

Your solution must several methods to take care of the several sub-tasks in this problem. Note that it may be easiest to define these methods in the reverse order than that given in order to test each method one at a time.

Here are the method headers for the methods I used in my solution:
public void eatAllRows();
This method applies the eatOneRow() method as the buggle works her way from the bottom row to the top-most one.

public void eatOneRow();
This method implements the row-eating behavior as described above, for one row.

public int countBagelsAhead();
This method counts the bagels ahead of the buggle (which is located in the middle of the rwo) and until the wall. Calling this method should not change the state of the buggle.

public void eatBagelsAhead();
This method implements the bagel-eating behavor of this kind of buggle: Eat the bagels ahead of the buggle, and leave a red square where a buglle used to be.


Recursion in PictureWorld: Patchwork

Look in the Patchwork folder inside the lab8_programs you downloaded. We're going to draw some interesting patterns. Have a look at the behavior of the program in the Test subfolder, by running the file Patchwork.html in your browser. Your job is to modify the code in Patchwork.java to duplicate this behavior you see in this applet. Be sure your code works for zero levels.

Be careful! The code can be very simple, but the pattern is diabolical! Getting the colors to come out right is a bit tricky.