Graphic by Keith Ohlfs |
|
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:
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.