Graphic by Keith Ohlfs

CS111, Wellesley College, Fall 1998

Problem Set 9

Due: Wednesday, November 18 by 4:00 p.m.

Note the Wednesday Due Date!!!

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


Reading Assignment:

Take-Home Exam Announcement

CS111 Exam 2 will be a take-home exam that will be handed out on Wednesday, November 18, and will be due at 4:00 p.m. Wednesday, November 25. You will have no other assignments during this period of time. The take-home exam will be very much like a problem set, except that (1) you may not collaborate with anyone else on the exam and (2) it counts for more points toward your final grade than a problem set does.

About this Problem Set

The purpose of this problem set is to give you practice with iteration via tail recursion, while loops, and for loops.

There are 2 pieces to this problem set: the laboratory assignment and the homework assignment. There is no pre-lab assignment for this problem set. You are required to do both pieces. We recommend that you do the laboratory problem during your lab section and the homework problems after completing the lab assignment.

All code for this problem set can be found in the ps9_programs folder, which you can download from the CS111 download folder.

How to turn in this Problem Set

Laboratory Problem: Save the modified PolygonWorld.java and FlowerWorld.java files in the Polygons folder. Upload the entire folder to your ps9 drop folder. Turn in a hardcopy of the PolygonWorld.java and FlowerWorld.java files.

Homework Problem:

Save the modified NestedFrames.java file in the NestedFrames folder. Upload the entire folder to your ps9 drop folder. Turn in a hardcopy of the NestedFrames.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 Stanzi's office (E106, directly across from E101).

Reminders


Laboratory Problem: Polygons

Task 1. Polygons

In this lab 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:

To begin this problem, download the ps9_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.

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. Your first goal is to fill in the missing body of polygonTail().

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

Next, flesh out the bodies of the 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 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. 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.

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. Fill out the skeleton for flowerNestedFor(). Test out your method by executing FlowerWorld.html and selecting the checkbox corresponding to the flowerNestedFor method.

Submission Details: For your softcopy submission, turn in your final version of the Polygons folder to your PS9 drop folder. For your hardcopy submission, turn in your final versions of PolygonWorld.java and FlowerWorld.java.


Homework Problem 1: Nested Frames

In this problem you will use iteration in to draw two-colored nested frame patterns like those shown below:

Each of the four patterns pattern consists of concentric rectangular frames that have the same thickness and alternate between two colors. In the above example, each target has 10 nested frames, each of whose thicknesses is 1/20 of the dimensions allocated to the pattern.

In Picture World, such patterns can be drawn as a sequence of concentric filled rectangles, where the rectangles are draw from the outside in. You have been provided with the following method for creating a centered rectangular picture:

public Picture centeredRect (double fraction, Color c)
Returns a rectangle filled with color c that is centered in the picture canvas in which it is drawn. The width and height of the rectangle are each the given fraction of the enclosing picture canvas's width and height. The fraction argument must be between 0 and 1.

For example, using this method, here is a recursive implementation of the nested pattern:

	public Picture nestedRec(int levels, double thickness, Color c1, Color c2) {
		if (levels == 0) {
			return empty();
		} else {
			return overlay(nestedRec(levels - 1, thickness, c2, c1),
			               centeredRect(levels*thickness, c1));
		}
	}

Here, levels is the number of nested frames, thickness is the thickness of each frame edge, c1 is the outermost color, and c2 is the color that alternates with c1. The recursion accumulates a final picture that consists of levels centered rectangles overlayed on top of one another. Note how each level of the recursion decrements levels by 1 and swaps c1 and c2 (so that c2 is the outermost color in the nested subpicture). This strategy is not a tail recursive one, since there is still a pending overlay operation to be performed after the recursion returns.

In the rest of this problem, you will expore three iterative strategies for expressing the nested box pattern. You will be fleshing out the following skeletons in the file NestedFrames.java within the folder NestedFrames:


public Picture nestedIter(int n, double thickness, Color c1, Color c2) { // This method contains the initial call to nestedTail, which does all the work. return nestedTail(n, thickness, c1, c2, empty()); } public Picture nestedTail(int n, double thickness, Color c1, Color c2, Picture ans) { // nestedTail should be a tail recursive method that returns a Picture // that is the nested frames pattern.   // Replace the following stub by a correct definition. return empty(); } public Picture nestedWhile(int n, double thickness, Color c1, Color c2) { // Use a while loop to accumulate and return a Picture // that is the nested frames pattern.   // Replace the following stub by a correct definition. return empty();   } public Picture nestedFor(int n, double thickness, Color c1, Color c2) { // Use a for loop to accumulate and return a Picture // that is the nested frames pattern.   // Replace the following stub by a correct definition. return empty(); }

Your task is to flesh out all three methods so that they have the same behavior as nestedRec. You can test your methods by executing the NestedFrames.html applet. This will give you a window like that pictured at the beginning of this problem. By selecting an integer in the righmost choice box, you can change the nesting level of the patterns. Your code does not have to take care of calculating the thickness; this calculation is already performed by the testing enviroment.

The patterns are arranged as follows inside the NestedFrames Applet window:

In your definitions, pay attention to the following notes:

Submission Details: For your softcopy submission, turn in your final version of the NestedFrames folder to your PS9 drop folder. For your hardcopy submission, turn in your final version of NestedFrames.java.