Graphic by Keith Ohlfs |
More Recursion and Iteration
|
In this problem set, you will explore three problems that involve recursion and iteration. The problems are significantly simpler than those on previous assignments.
As discussed in class, an Invocation Tree is an abbreviated version of a Jave Execution Model. It contains a node for each method called during the execution of a program. Each node contains the name of the method, the values of its parameters, and (where applicable) its result. There is an edge connecting each "parent" node to the "children" nodes for the method invocations encountered directly within the body of the parent. The children nodes are all shown at the same vertical level, arranged from left to right in the order of their execution.
For example, consider a recursive definition of the Fibonacci function:
public int fib (int n) { if (n < 2) { return n; } else { return fib(n-1) + fib(n-2); } }
Below is an invocation tree for the invocation fib(6). Each node has the form fib(n):r, where n is the parameter of the invocation of fib and r is the result returned by the invocation.
In this problem, you will draw an invocation tree for the invocation of a method that computes an element of Pascal's triangle. Pascal's triangle is a triangular arrangement of numbers whose outer edges consist of 1s and each of whose inner elements is the sum of the two numbers immediately above it to its right and left:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1
Let P(r,i) indicate the value of the ith element in the rth row of Pascal's triangle, where rows are numbered from top to bottom starting with 0, and elements in a row are numbered from left to right starting with 0. For example, P(4,0) = 1, P(4,1) = 4, and P(4,2) = 6.
Here is a recursive method that computes the value of P(r,i):
public int P (int r, int i) { if ((i == 0) || (i == r)) { return 1; } else { return P(r - 1, i - 1) + P(r - 1, i); } }
For this problem, you are to draw a complete invocation tree for the invocation P(6,4). Each node of your tree should have the form P(r,i):a, where r is the row number, i is the element number within a row, and a is the answer returned by the invocation. Be sure to draw your nodes small enough so that they all fit on a single piece of paper. All children of the same parent should appear at the same vertical level, as in the fib example above.
You should turn in an actual sheet of paper for this problem rather than an electronic submission. Turn it in during class on Thursday, November 13, or slip it under Lyn's office door (SCI E106) any time before 6pm on Thursday, November 16. You may draw it in pencil (or pen), or use a drawing program, whichever you find more convenient. Your drawing should be neat, but please do not go overboard -- spend as little time on this problem as possible!
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 CS111 download folder on Nike. Rename the SierpinskiWorld folder username_SierpinskiWorld, where username is your Nike username. You do not need to rename any other files within the SierpinskiWorld 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 proper subset of the primitives listed above.
Turn in you final version of username_SierpinskiWorld to your PS6 drop folder.
In this problem you will use iteration to draw two-colored ringed "targets" like those shown below:
Each target consists of concentric rings that have the same thickness and alternate between two colors. In the above example, each target has 5 rings with a thicknes of 20 pixels. Such targets can be drawn as a sequence of concentric filled circles, where the circles are draw from the outside in.
To begin this problem,, download the Target folder from the CS111 download folder on Nike. Rename the Target folder username_Target, where username is your Nike username. You do not need to rename any other files within the Target folder.
The Target.java file contains all the code that implements the user interface shown above except for three methods that are responsible for drawing the targets:
public void drawTargetIter(int rings, int thickness, Color c1, Color c2, int x, int y) public void drawTargetWhile(int rings, int thickness, Color c1, Color c2, int x, int y) public void drawTargetFor(int rings, int thickness, Color c1, Color c2, int x, int y)
All three methods has the same specification: draw rings concentric rings with thickness thickness centered at point (x, y) in the usual Java graphics coodinate system. The outermost color should be c1, and the colors should alternate between c2 and c1 in the rest of the target. drawTargetIter draws the leftmost target in the window; drawTargetWhile draws the center target ; and drawTargetFor draws the right target.
Your task is to define these three methods. All three methods are supposed to implement the specification using iteration to draw a sequence of concentric filled circles from the outside in. The only difference between the three methods is how they should implement iteration:
Notes:
while (test-expression) { body-statements }
and a Java for loop has the syntax
for (init-statement; test-expression; update-statement) { body-statements }
A Java for loop is equivalent to the following while loop:
init-statement; while (test-expression) { body-statements; update-statement; }
public void fillCircle(Color c, int radius, int x, int y) Draw a circle with color c and radius radius centered at point (x,y).
This is the only drawing primitive you should use in this problem. Note that you do not pass any Graphics object to fillCircle -- it figures out which Graphics object to use on its own.
Turn in you final version of username_Target to your PS6 drop folder.