CS 101 (Spring 2000)
Lab 4: Persian Recursion (Design Solution)
| || 8 ||Feb
|| 11 ||Feb
|| 15-16 ||Feb
|| 22-23 ||Feb
|| 25 ||Feb
[[[ Download PC zip ]]]
Lab4.java (don't modify)
You will have to modify this to call your
appropriately. Also, you'll have to add testing code.
where you implementation takes place.
Solution APIs for this lab
- 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
- Constructs the color associated with the supplied
colorNumber. That integer must be between 0 and 15,
inclusively; if not, an error is thrown.
- This method returns the
java.awt.Color represented by this object.
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.
- 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).
- This method returns the length of the smallest line drawn
in constructing the rug. If no lines are drawn yet, then an error is
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.
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 (
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
c1y) and (
c2y) represent the upper-left and lower-right
corners of the rectangle that will now be subdivided by
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 | |
color int k3
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'
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
- A function that always returns the same color.
- A function that returns two colors -- not necessarily in any
- A function that always alternates between two colors (of your
choice). For example, If the
previous color chosen was red, then the next color might be blue. But
then the next color would have to be red, and so on.
- A function that nicely spreads out the colors. You can tell this
by the statistics you compute at the end of the rug-drawing.
Looking forward to implementation
( class-sponsored design>)
- Implement the Persian Rug problem.
- Complete a code cover sheet.
- Provide printouts of any files you created or modified for this
- Provide transcripts of your rug-drawing exercises and their
statistics (as computed by your program, not by you!).
Last modified 14:13:18 CST 15 February 2000
by Ron K. Cytron