Graphic by Keith Ohlfs

CS111, Wellesley College, Spring 1998

Problem Set 7

Due: Friday, April 3 by 4:00 p.m.

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

Reading Assignment

About this Problem Set

The purpose of this problem set is to give you experience with recursion.

There are 2 pieces to this problem set: the laboratory assignment and the homework assignment (note - there is no prelaboratory assignment, due to the Spring Break). You are required to do both parts. As usual, we recommend that you work on the laboratory problem during your lab section and the homework problems after completing the lab assignment.

How to turn in this Problem Set

Laboratory Problem: Save the modified HurdleWorld.java and HurdleWorld2.java files in the ps7_Buggles folder. Upload the ps7_Buggles folder to your ps7 drop folder. Turn in a hardcopy of the HurdleWorld.java and HurdleWorld2.java files.

Homework Problem 1: Save the modified SierpinskiWorld.java file in the SierpinskiWorld folder. Upload the entire folder to your ps7 drop folder. Turn in a hardcopy of the SierpinskiWorld.java file.

Homework Problem 2: Save the modified TreeWorld.java file in the ps7_Pictures folder. Upload the entire folder to your ps7 drop folder. Turn in a hardcopy of the TreeWorld.java file.

Turn in only one package of hardcopy materials. Staple your files together with the cover page, and submit your hardcopy package by placing it in the box outside of Jennifer's office (E104, directly across from E101).

Reminders


Laboratory Problem: More Buggle Hurdles - Jumping a Hurdle of Arbitrary Height

The buggles were so excited by their last hurdle jumping exercise, they have decided to go on to bigger and better activities - jumping hurdles of arbitrary height! As before, a number of hurdles are placed at the base of a BuggleWorld grid. A buggle starting at position (1,1) must run across the base of the grid jumping all of the hurdles in its path until it reaches the opposite wall. Just as before, the buggle does not know the placement of the hurdles and does not know the length of the race. Now, however, the buggle also does not know the heights of the hurdles. The hurdles consist of varying heights (walls one or more grid cell high).

We will solve this problem in two stages. Recall that when last jumping hurdles, buggles needed a tick method() to make them repeat their actions. For the first stage, we will use recursion (instead of the tick method()) to create a Hurdler that can run a hurdle race of arbitrary length. In this exercise, the hurdles are still height 1 (jumping hurdles of arbitrary height is the next exercise). To do this:

  1. Download the ps7_Buggles folder from the ps7_programs folder in the CS111 download directory.
  2. Open the project window and the HurdleWorld.java file. This file contains a number of methods responsible for setting up the hurdle race. You do not need to understand these files to successfully complete this lab.
  3. Modify the runHurdles() method so that the Buggle runs the hurdles recursively. The base or exit case, which causes the recursion to stop (i.e., the Buggle to stop running), will be the detection of the wall at the righthand end of the Buggleworld grid. This wall is detected by the atFinishLine() method (written for you and included in HurdleWorld.java), by looking for the presence of a wall that is more than one segment high. We will discuss in lab how to accomplish this.
  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.

As a second exercise, we will use recursion to create a hurdler who can jump a hurdle of arbitrary height. Note that we will not be able to use the same trick of detecting a two-story wall for the finish line because now hurdles are of arbitrary height. Instead, we have decided to reward our buggle - jumping hurdles of arbitrary height is hard work and takes a lot of energy - by a snack at the end of the race! A bagel has been dropped at the finish line (on the last square) for our hurdler detect the finish line and then enjoy.

  1. Open the HurdleWorld2.java file. As with the first exercise, the file contains a number of methods that set up the hurdle race which do not need to a be understood for completing this lab.
  2. View HurdleWorld2.html with the AppletViewer. Note the hurdles of arbitrary height.
  3. Modify the runHurdles() method so that the Buggle runs the hurdles recursively and stops when it finds the bagel. This code should look very similar to your code for the first exercise. The only difference is the condition for the exit case. We will discuss in lab how to accomplish this.
  4. Make the jumpHurdle() method into a recursive method that makes the buggle jump a hurdle of arbitrary height. While it is easy to make two recursive methods to do this (one for going up, and the other for going back down), we will 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 view your Buggle run the hurdles. She should run any combination of hurdles, of any height, and stop without complaints when she reaches the bagel at the finish line.


Homework Problem 1: Sierpinski's Gasket

Sierpinski's gasket is a self-similar triangular entity named after its discoverer. In this problem we will investigate an approach for approximating Sierpinski's gasket.

Let sierpinski(levels, side) be the notation for a Sierpinski gasket with levels levels and side length side. Then a recursive mathematical definition of sierpinski is as follows:

The pictures below depict sierpinski(levels, side) for for levels = 1 through 5. These are approximations to the "true" self-similar Sierpinksi gasket, which is the limit of sierpinski(levels, side) as levels approaches infinity.

sierpinski(1,100)

sierpinski(2,100)

sierpinski(3,100)

sierpinski(4,100)

sierpinski(5,100)

In this problem you will define a Java method that uses a turtle to draw Sierpinski's gasket. To begin this problem, download the SierpinskiWorld folder from the ps7_programs folder in the download folder. The file SierpinskiWorld.java contains a subclass of Turtle named SierpinskiMaker with a stub for the following method:

	public void sierpinski (int levels, int side) {
	 // Draw a sierpinski gasket specified by levels and side.
	  // Maintain the invariant that the turtle's position and heading after
	  // a call to sierpinski are the same as before the call to sierpinski.
            }

Flesh out the definition of the sierpinski method so that it draws the specified Sierpinski gasket and maintains the turtle's position and heading as invariants. Note: SierpinskiWorld.java includes code that creates an instance of SierpinskiMaker and positions it toward the lower left hand corner of the screen facing east. A gasket whose lower left-hand corner is positioned at this starting point will be automatically centered in the TurtleWorld window.

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.

Recall that the Turtle drawing primitives include the following:

public void fd (int n)
Move the turtle forward n steps.
 
public void bd (int n); 
Move the turtle backward n steps.
 
public void lt (int angle);
Turn the turtle to the left angle degrees. 
 
public void rt (int angle);
Turn the turtle to the right angle degrees. 
 
public void pu (int n); 
Raise the turtle's pen up. 
 
public void pd (int n); 
Lower the turtle's pen down.

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.


Homework Problem 2: TreeWorld

In this problem you will explore recursion in PictureWorld. To begin this problem, download the ps7_Pictures folder from the ps7_programs folder in the CS111 download folder. In this folder you will find a file TreeWorld.java, which you are to edit. Specifically, within TreeWorld.java there is a method that has been partially written:

 

public Picture makeTree(Color c, int levels) {

// fill in this stub

}

Your goal is to fill in the body of this method using recursion to create trees. Notice that in the file there are two primitives defined for you, a leaf and a node, shown below:

These two basic primitives can be combined to create trees, much like those drawn in class to describe many fundamental computer science concepts (for example, program structure, method invocations, directory structure). Such trees are shown below, for a varying number of levels. In the figures, tree1 indicates that the tree is composed of one level. Likewise, tree2 is composed of 2 levels, tree3 of 3 levels, tree4 of 4 levels and tree5 of 5 levels.

Notice that in the file TreeWorld.java, the method invocations to create these trees have been included already for you. All that you need to do is fill in the body of the method makeTree() such that it generates the pictures shown above. You will likely find some of the methods of PictureWorld that we have studied already (above, below, beside, overlay, etc...) to be useful. You do not need to define any other auxiliary methods in your solution. You should expect your method body to be quite short. If it is lengthy, you are likely going in the wrong direction and should seek help.