![]() |
Lab 10
|
The purpose of this lab is to give you practice with iteration via tail recursion and while loops. You will use iteration tables to show the sequence of values assigned to parameters and variables that are updated and/or used to meet the invariant in your method loops.
Today's plan:
In class, you saw that tail recursion, while
loops, and for
loops are all equivalent in power.
Which mechanism you choose depends on the particular program and is
often a matter of taste.
For the problems below, draw an iteration table for the given
method and then write equivalent methods using the other forms of
iteration. Assume we have a list
L1 = [3,1,2,4]
.
weightedSum()
weightedSum(L1)
given the tail-recursive implementation below, and write the
while
loop (weightedSumWhile
) and
for
loop (weightedSumFor
)
implementations.
public static int weightedSum (IntList L) { return weightedSumTail(L, 1, 0); } public static int weightedSumTail (IntList L, int weight, int total) { if (isEmpty(L)) { return total; } else { return weightedSumTail(tail(L), weight+1, (weight*head(L)) + total); } }
isMember()
isMemberWhile(2,L1)
and
for isMemberWhile(5,L1)
given the
while
loop implementation below, and write the
tail-recursive implementation (isMember()
) and the
for
loop implementation (isMemberFor()
).
public static boolean isMemberWhile(int n, IntList L) { while (!isEmpty(L) && (head(L)!=n)) { L = tail(L); } return !isEmpty(L); // only true if n is in L }
To begin this problem, download the lab10_programs
folder from the cs111d
account. In DrJava, open
RowWorld.java
in the Row
folder. In this
task, you will be fleshing out skeletons for various methods defined
in the RowWorld
class. The Test
folder contains a test
version so you can see what your applet should produce.
row(p,n)
produces a picture with p
arranged
in n
equally spaced columns (ie a row with
n
elements).
For example,
row(smile(Color.black,Color.yellow),1) |
row(smile(Color.black,Color.yellow),4) |
![]() |
![]() |
// This method returns a picture with p arranged in numberItems
// equally spaced columns (ie a row with numberItems elements).
public Picture row (Picture p, int numberItems) {
if (numberItems<=0) { // base case: a row with no elements is empty
return empty();
} else { // general case: a row is a picture with a row
// of one fewer number of elements to its right
return beside(p, row(p, numberItems-1), 1.0/numberItems);
}
}
Draw an iteration table for row(smile(Color.black,Color.yellow),
4)
. Rewrite the method in three ways:
while
loopfor
loop
Look in the Polygons
subdirectory of the
lab10_programs
directory, and open
PolygonWorld.java
. In this task, you will be fleshing
out skeletons for various methods defined in the
PolygonMaker
class within
PolygonWorld.java
. The test
folder has an
example of working code.
In this problem, you will program turtles to draw regular polygons. A regular polygon has sides of equal length and angles of equal length, as shown in the following images:
We will draw these polygons using the three strategies for expressing iteration:
while
loop.for
loop.A turtle can draw a polygon by repeatedly drawing a side and then
turning by an angle of (360.0/sides)
,
until it has drawn the specified number of sides. One strategy for
encoding this iteration is to use tail recursion. Below is the
skeleton for such a such a strategy. The polygon()
method invokes the tail recursive polygonTail()
method with the appropriate initial parameters for the state
variables of the iteration.
public void polygon (int sides, int length) { double angle = 360.0/(double)sides; polygonTail(sides, length, angle); } public void polygonTail(int numSides, int length, double angle){ // Flesh out this body. }
Flesh out the bodies of the polygonTail()
,
polygonWhile()
and polygonFor()
methods.
You can test your methods by selecting the appropriate checkbox in
the Parameter window before pressing the Run
button in the
Turtle window.
Now that you can draw a polygon, you can use this method to draw a
polygon flower. A polygon flower is defined by the number of petals
and the number of sides of each petal. Each petal is a regular
polygon, and the petals are rotated with respect to one another. The
angle of rotation is equal to (360.0/petals)
. Some sample
flowers are as follows:
As with the polygon, we will write methods to draw these flowers using tail recursion and while loops. Fill out the skeletons for:
flowerTail()
flowerWhile()
flowerFor()
FlowerWorld.java
file. You should
make use of one of the polygon methods you wrote in the last section
(Note that FlowerMaker
extends PolygonMaker
).
Test out your methods by executing FlowerWorld.html
and selecting the checkbox corresponding to the method you want to
test.
Now, just for practice, we will write a method to draw these
flowers using a nested for
loop. For this
method, do not make use of the Polygon methods you wrote in
the last section. Draw an iteration table for
flowerNestedFor(6,4,60)
and fill out the skeleton for
flowerNestedFor()
.
Test out your method by executing FlowerWorld.html and selecting the checkbox corresponding to the flowerNestedFor method.