CS 101 (Spring 2000)
Lab 4: Persian Recursion

Lab Assigned
Design Due
(In class)

10 AM
Implement
(In Lab)
Demo
(In Lab)
Lab Due
(In class)
Friday
10 AM
8 Feb 11 Feb 15-16 Feb 22-23 Feb 25 Feb

Click here for the class-sponsored design

Overview:

Recursive programs are useful when a large problem can be cast as smaller instances of itself. Recursive programs tend to be short compared with the job that they do. In this lab, you will first design an API and then implement classes that have the effect of drawing a Persian rug on the screen.

Goals:

By the end of this lab, you should This lab contains two parts, design and implementation, due as shown at the beginning of this document. The Monday after your design is due, a sample design will be handed out in class. We may choose one of your designs for this honor; or, we may write one up on our own. Remember, there is no right or wrong design, but each design may have its own strengths and weaknesses.


Before starting:

[[[ Download PC zip ]]]
Zip includes:

Recall that the following lines should appear at the top of any files that use the terminal or canvas classes.

import terminal.*;
import canvas.*;

Problem description: Persian Recursion

The idea for this lab appeared in the article "Persian Recursion", by Anne M. Burns, Mathematics Magazine, volume 70, 1997, pages 196-199.
Design and implement the class PersianRug.java that does the following.


Design Strategy

  1. Complete a design cover sheet.
  2. You are given the code for Crayon.java. As an exercise, write the API for this class and turn it in as part of your design.
  3. Define your PersianRug class. Give its API and describe what all of its methods do. Normally, private methods would not appear in an API, but for this assignment consider them as well.

To find out how to draw colored lines, take a look at the Line class. Look for a constructor that accepts the line's coordinates as well as a color. (If we had given you an API for Line this would be easy!)

The effect of this lab is to make a picture that looks like a Persian rug. To see this effect, run the sample solution.

For the purpose of this lab, it is helpful to regard color as an integer. This is a kind of abstraction that allows us to compute one color as the function of other colors. You are supplied with the class Crayon.java, which lets you provide an integer and get a Color. As part of the design component of this lab, you are asked to write the API for Crayon.java.

You will have to invent a method for this class that takes four colors (the colors of a square's sides) and computes what color the middle (subdividing) lines should be. Let c1, c2, c3, and c4 represent the (int) color of each of the four sides of a square. If the initial square's sides are color 0, then a bad function would be:

  (c1 + c2 + c3 + c4)
because the result would always be 0. For the design don't worry about the particular function---worry only about its type signature. However, for the implementation you will experiment with a few functions:
  1. Find a function that always draws the same color
  2. Find a function that always alternates between two colors
  3. Find a function that uses all the colors

Place code in your class to count how many times your recursive method is called from within the class. At the end of the computation, print out this number (with a nice message) using Terminal.println.


What is an API?

An API is an Application Programming Interface. It tells somebody what they need to know to use a particular class. As an example of an API, take a look at what you were provided for Rect in Lab 2. The API should include the following: For each constructor or method, you should specify its A good test of your API is to show it to somebody (maybe somebody in cs101, a TA, or the instructor), and ask that person if the API tells him or her what is needed to use your class.

Looking forward to implementation

( class-sponsored design)
  1. Implement the Persian Rug problem.
  2. Complete a code cover sheet.
  3. Provide printouts of any files you created or modified for this assignment.
  4. Provide transcripts of your rug-drawing exercises and their statistics (as computed by your program, not by you!).


Last modified 23:01:15 CST 14 February 2000 by Ron K. Cytron