![]() Graphic by Keith Ohlfs |
|
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.
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
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:
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.
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.
|
|
|
|
|
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.
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.