CS 101 (Fall 2002)
Lab 4: Persian Recursion (Design Solution)

Lab Assigned Design Due
(In class)
10 AM
Implement
(In Lab)
Demo
(In Lab)
Lab Due
(In class)
Friday
10 AM
30 Sep 4 Oct 1-2 Oct 8-9 Oct 11 Oct

[[[ Download PC zip ]]]

Zip includes:


Solution APIs for this lab

Crayon.java
This class is immutable -- once a color is constructed, you can't change it. This class was provided to you, so its methods are complete. You need not do any work in this class, but you must understand how to use it.
Constructors
Crayon(int colorNumber)
Constructs the color associated with the supplied colorNumber. That integer must be between 0 and 15, inclusively; if not, an error is thrown.
Accessors
Color getColor()
This method returns the java.awt.Color represented by this object.
PersianRug.java
Constructors
PersianRug(CS101Canvas canvas, int size)
Constructs a Persian Rug of the supplied size. No check is performed to make sure the rug will fit on the canvas. Because just one size is supplied, the rug is necessarily square.
Accessors
int linesDrawn()
This method returns how many lines have been drawn. It is valid to call this method anytime after construction, but don't expect big numbers to be returned if the rug isn't drawn yet (see below).
int smallestLine()
This method returns the length of the smallest line drawn in constructing the rug. If no lines are drawn yet, then an error is thrown.
int numUsed(int color)
This method returns the number of lines drawn of the specified color. The supplied integer must be in the range of 0 to 15, inclusively; otherwise, an error is thrown. It is valid to call this method any time after construction.
Mutators
public void weave(int stopAt)
This method causes the rug to be drawn. Weaving (drawing the rug) continues until a squre of the specified size (stopAt) is reached.
Other
private void recurDraw(int c1x, int c1y, int c2x, int c2y, int k1, int k2, int k3, int k4)
This method actually does the recursive part of the drawing. It is an internal method to PersianRug. The integers (c1x,c1y) and (c2x,c2y) represent the upper-left and lower-right corners of the rectangle that will now be subdivided by recurDraw. The integers k1,k2,k3,k4 are the four colors, one for each side of the rectangle about to be subdivided. The following picture may make this clearer.

     (c1x, c1y)          color int k1
              |----------------------------| 
              |                            |
              |                            |   color int k2
 color int k4 |                            |
              |                            |
              |----------------------------| (c2x,c2y)
                         color int k3

Why is stopAt not a part of the API for this method? What does this imply about thei class's instance variables?


Possible functions for picking a color for the cross-members

When subdividing, you have to pick one color that is used for both of the cross members. Let's suppose you are dividing rectangle R into four subrectangles, r1, r2, r3, and r4. The color you pick for drawing the two lines that make r1, r2, r3, and r4 is some function of the colors of R's four sides.

What does this tell you about the recursive method that will do the drawing? It needs the information passed in about the rectangle's sides' colors.

Let's suppose that these four colors are

int k1, k2, k3, k4;
What you need is a function with API
int computeColor(int k1, int k2, int k3, int k4)
In your demo, you will be asked to show the following functions, each with the above API (you can change the name of the function if you want to keep them all in your PersianRug.javafile).

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 14:46:22 CDT 25 June 2002 by Ron K. Cytron