Graphic by Keith Ohlfs

CS111, Wellesley College, Fall 1997

PreLab #7
[CS111 Home Page] [Syllabus] [Students] [Lecture Notes] [Assignments] [Programs] [Documentation] [Software Installation] [FAQ] [CS Dept.] [CWIS]

CS111 PreLab #7: Recursion

Due Date: Before Lab, October 27 or 28

In lecture you were introduced to the concept of recursion in which a method calls itself in order to solve successively smaller versions of the same problem. You saw a recursive method to move a buggle forward some arbitrary number of steps. This method looked like this:

public void fd(int n){
 if (n == 0) {
  //Do Nothing
 } else {
  forward();
  fd(n-1);
 }
}

In this prelab, we will use recursion to rewrite the ObstacleRunner class that we wrote for lab 6. If all went well in lab 6, your ObstacleRunner should have taken 48 consecutive steps to run the obstacle course, leaving a trail that looks like this:

To accomplish this, you had to write numerous methods, such as forward48(), forward16(), forward8(), etc. Recursion will allow you to write your ObstacleRunner class that uses just two short methods to run the obstacle course.

1. Download the Buggles folder from the Nike Download directory.

2. Open the Buggles.proj file.

3. Add Obstacles.java and Obstacles.html to the Buggles.proj window. (Either by choosing Add Files from the Project Menu or by dragging them into the window. Make sure to hold down the "apple" key to drag in the Obstacles.html file).

4. Change the two methods in the ObstacleRunner class as follows:

  1. Change the "move()" method so that it tests whether the buggle is facing a wall and if it is facing a wall, the buggle turns left and moves forward; otherwise it moves forward. (this is just like the method you wrote for lab6).
  2. Change the "runObstacles" method so that it moves the Buggle the number of steps given by the integer n. Use recursion, like that in the program given above. Make sure the buggle checks for walls with each step!
    public void runObstacles(int n)
  3. Make ruth run an obstacle course that is 48 steps long, by calling ruth.runObstacles(48);
  4. Ruth should draw the pattern shown above.

Turning in PreLab

When you are finished. Save the three files (Obstacles.java, Obstacles.html and Obstacles.class) in a folder named "username_preLab7", where username is your username. Do not include the Buggleworld.proj file. Upload this folder into the lab7 drop folder on Nike.