Lab 9 -- Iteration

The purpose of this laboratory is to give you practice with iteration via tail recursion, while loops, and for loops. You will use iteration tables to show the sequence of values assigned to parameters and variables which are updated and/or used to meet the invariant in your method loops.

Iteration using Lists

pencil-and-paper problems
We saw in class that iterations could be expressed as tail recursions, while loops, and for loops. Each of the following parts contains a method that uses one of these forms of iteration. For each part, draw an iteration table and write two equivalent methods that use the other two forms of iteration. Assume we have a list L1 = [1,2,3,4].

Part a -- weightedSum
Draw the iteration table for 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);
  }
}

Part b -- partialSum
Draw the iteration table for partialSum(L1) given the for loop implementation below, and write the tail-recursive implementation (partialSum and partialSumTail) and while loop (partialSumWhile) implementations.

public static IntList partialSumFor (IntList L) {
  int sum = 0;
  IntList result = empty();
  for (; !isEmpty(L); L=tail(L)) {
    sum = sum + head(L);
    result = postpend(result, sum);
  }
  return result;
}

Part c -- isMember
Draw the iteration table for isMember(3,L1) and for isMember(5,L1) given the while loop implementation below, and write the tail-recursive implementation (isMember) and for loop (isMemberFor) implementations.

public static boolean isMember (int n, IntList L) {
  while (!isEmpty(L) && (head(L)!=n)) {
    L = tail(L);
  }
  return !isEmpty(L); // only true if n is in L
}

Iteration in PictureWorld -- row

To begin this problem, download the lab9_programs folder from the CS111 download folder. Open the project file Row.proj in the Row folder, and then open Row.java. In this task, you will be fleshing out skeletons for various methods defined in the RowWorld class within Row.java. The Test folder has an example of working code.

// 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(patch(Color.red), 4). Rewrite the method in three ways:

Iteration in TurtleWorld

To begin this problem, download the lab9_programs folder from the CS111 download folder. Open the project file Polygons.proj in the Polygons folder, and then 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.

1. Polygons

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 three strategies for expressing iteration:

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. 
	}

Draw an iteration table for polygon(5,75)

Next, flesh out the bodies of the polygonTail(), polygonWhile() and polygonFor() methods. As suggested by their names, the polygonWhile() method should express the polygon-drawing iteration via a while loop, and the polygonFor() method should express this iteration via a for loop.

You can test your methods by selecting the appropriate checkbox in the Parameter window before pressing the Run button in the Turtle window.

2. Polygon Flowers

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, while loops and for loops. First, though, draw an iteration table for flower(6,4,60). Then, fill out the skeletons for flowerTail(), flowerWhile(), and flowerFor() in the 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.

3. Nested for loops

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.