Graphic by Keith Ohlfs

CS111, Wellesley College, Fall 1997

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

CS111 Lab #7: Recursion

Due Date: Thursday, October 30, 6:00 p.m.

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 other programs, it is advangageous to be able to repeat a method some undetermined number of times, and only end upon some test being satisfied. For example, if the Buggle were looking for a bagel, the above method could be modified so that the buggle only stops when it has found a bagel, as follows:

public void findBagel()
{
     if (isOverBagel()) 
     {
          //Do Nothing
     } 
     else 
     {
          forward();
          findBagel();
     }
}

Note that this kind of programming can be a little tricky. If there is no bagel present, the program will get into an "infinite loop" where the Buggle keeps going forward forever. So you want to make sure your test will be satisfied at some point.

Returning a boolean value from a method

Often, the recursive method may call another method that performs the desired test. This second method must have a way to return a value (either true or false) to the recursive method to signal that the base state has been reached. A method can return a value to the calling method using the return statement:
public boolean example()
{
     boolean testResult = false;
     if (myTest())
     {
          testResult = true;
     } 
     else 
     {
          testResult = false;
     }
     return testResult;
}

In this first exercise, we will use this to create a Hurdler that can run a hurdle race of arbitrary length.

  1. Download the Buggles folder from the CS111 download directory.
  2. Open the project window, and drag in the two files "lowHurdles.java" and "lowHurdles.html".
  3. Modify the runHurdles and the jump methods so that the Buggle runs the hurdles recursively.

    Unlike the prelab, the base state, which causes the Buggle to stop running, will be the detection of the wall at the righthand end of the Buggleworld grid. This wall can be detected within the jump method, by looking for the presence of a wall that is more than one segment high.

    1. Modify the jump method so that it detects the end wall, and returns a value of "true" if it is the case.
    2. Modify the runHurdles method so that the Buggle keeps running hurdles until the jump method returns a value of true (i.e. until the buggle reaches a wall that is at least 2 segments high).
  4. Try out your hurdler with the applet viewer. She should run any combination of hurdles, and stop without complaints when she reaches a wall that is two or more segments high.

Jumping a Hurdle of Arbitrary Height

In our quest to create an ever more versatile hurdler, we will now use recursion to create a hurdler who can jump a hurdle of arbitrary height. We will not be able to use the same trick of detecting a two-story wall for the ending, so instead, we will drop a bagel on the last square for our hurdler to find.
  1. Drag the files "highHurdles.java" and "highHurdles.html" into the Buggles project window.
  2. Open the highHurdles.java file.
  3. Make runHurdles() into a recursive method, that moves forward, jumping each hurdle, until it finds the bagel.
  4. Use recursion to make the buggle jump a hurdle of arbitrary height. While it is easy to make two recursive methods (one for going up, and the other for going back down) to do this, try to make a single recursive method that allows the buggle to go up until it finds the top of the wall and then go back down the other side. We will discuss in lab how to accomplish this.
  5. Use the appletViewer to make the Hurdler Hannah run the hurdles provided in the highHurdles setUp method. Your final applet should look like this:

Food For Thought

Suppose you wanted to create a hurdler to jump high hurdles, but stop when she came to a wall that went all the way to the top of the grid, instead of having to detect the bagel. How would you do this? What would you test for to detect the ending condition? How would you use this test to end your recursion? How would you make sure the Buggle returned to the bottom of the grid at the end of the run? Do not turn this in, but you can test yourself to see how much you've absorbed from this lab by trying this out.

Turning in Lab 7

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