Graphic by Keith Ohlfs

CS111, Wellesley College, Fall 1997

Problem Set 5:
Recursion

Note Extension:
Due: Saturday, November 8, 6pm

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

In this problem set, you will explore two problems that involve recursion.


Problem 1: Bagel Bush

A bagel bush is a recursive figure defined by the function BagelBush(levels, size), where

Below are some examples of some bagel bushes in a 16x16 grid. In all cases, there is a North-pointing buggle at position (8,1).

BagelBush(0,8)

BagelBush(1,8)

BagelBush(2,8)

BagelBush(3,8)

BagelBush(4,8)
or BagelBush(5,8)
or BagelBush(6,8), etc.

BagelBush(3,4)
or BagelBush(4,4)
or BagelBush(5,4), etc.

The fact that bagel bushes are drawn on a discrete grid means that resolution can be lost. Assume that all fractional numbers are rounded down to the nearest integer, and that many parts of the tree can share the same grid cell. For example, as illustrated above, BagelBush(n,8) is the same for all n >= 4. In order to get better resolution, it is necessary to use a grid with more cells. For example, here is BagelBush(5,16):

To begin this problem, download the BagelBush folder from the CS111 download folder on Nike. Rename the BagelBush folder username_BagelBush, where username is your Nike username. You do not need to rename any other files within the BagelBush folder.

The BagelBush class creates an n x n grid, and places a Northward-pointing buggle named bertha at position (n/2, 1), where n is a variable whose value you can edit (by default it is 16). Bertha is sent the message bush(levels, size), where levels and size are numbers you can edit; by default they are 4 and 8, respectively.

Bertha is a member of the BagelBusher subclass of Buggles, which responds to the following method:

public void bush (int levels, int size)
  Use this buggle to draw the bagel bush defined by BagelBush(levels, size).

Your goal is to flesh out the bush skeleton so that it correctly draws the bagel bushes illustrated in the pictures above.

Notes/suggestions:

When you are done with this problem, submit the entire username_BagelBush folder to your ps5 drop folder on Nike.


Problem 2: Quilts Revisited

Consider the following quilt patterns:

On problem set 3, you wrote methods that produced the quilts shown above. (The "exploding" pattern in the upper right corner has been slightly modified to simplify this problem.) Each quilt required multiple methods that usually differed only in the number of patches they drew (e.g. 16x16, 8x8, 4x4, etc).

In this problem, you will use the power of recursion to define, for each quilt pattern, a single method that draws that quilt pattern. In a few cases, you will be provided with some auxiliary methods to help draw the pattern.

To begin this problem, download both the Patchwork folder and the Quilts folder from the CS111 download folder on Nike. Rename the Quilts folder username_Quilts, where username is your Nike username. You do not need to rename any other files within the Quilts folder. You do not need to rename the Patchwork folder.

Study the files Patchwork.java and PatchworkRecursive.java within the Patchwork folder. Patchwork.java contains the patchwork-drawing code presented in class during Lecture 9 (September 30, 1997). PatchworkRecursive.java contains recursive versions of the patchwork-drawing code presented in class during Lecture 17 (October 30, 1997). Note how recursion greatly simplifies the patchwork-drawing code.

Next study the file Quilts.java within the Quilts folder. This file contains the solutions to Problem Set 3 (modulo the minor change to the Explode pattern).

Finally, examine the file QuiltsRecursive.java. This file contains stubs for the following four method definitions:

     public void drawStripes (int n, Graphics g, Color c1, Color c2, 
	                          int side, int x, int y)
	  Draw horizontal stripes in nxn patches within a square at (x,y) with length side.
	  The stripes alternate between colors c1 and c2. 
	  All stripes are n patches wide. The height of the stripes in patches decreases 
	  in the sequence n/2, n/4, n/8, etc. 
 
     public void drawExplode (int n, Graphics g, int side, int x, int y)
	  Draw an "exploding" pattern using nxn patches in a square at (x,y) with length side.
	  The upper left quadrant is a solid color, while the other three quadrants are
	  exploding patterns. Use the intToColor method to determine the color of the
	  solid square for a given n.
 
	public void drawWeave (int n, Graphics g, Color c1, Color c2, Color c3, 
	                         int side, int x, int y)
	  Draw a weave pattern with straw colors c1 and c2 and background color c3
	  within the nxn patches of a square at (x,y) with length side. 
	  Use the drawWeave2x2a and drawWeave2x2b methods to draw the primitive
	  2x2 weave patterns.
 
	public void drawDiagonals (int n, Graphics g, Color c1, Color c2, Color c3, 
	                             int side, int x, int y)
	  Draw lower-left to upper-right diagonals in  within nxn patches in a 
	  square at (x,y) with length side. The main diagonal of the square should
	  be color c1, and from right to left the colors should alternate in the
	  sequence c1, c2, c3, c1, c2, c3, etc.  Note that the recursive pattern 
	  differs when n is 1 mod 3 (i.e. (n % 3) == 1)  and when n is 2 mod 3
	  (i.e. (n % 3) == 2). 
 

You are to flesh out the definitions of these above four methods so that the following paint method draws the four quilts in the picture at the beginning of this problem.

	public void paint(Graphics g){
	  this.drawStripes(16, g, Color.yellow, Color.red, 160, 0, 0);
	  this.drawExplode(16, g, 160, 200, 0);
	  this.drawWeave(16, g, Color.yellow, Color.black, Color.white, 160, 0, 200);	
        this.drawDiagonals(16, g, Color.red, Color.blue, Color.yellow, 160, 200, 200);
	}
 

You should define each of the four methods recursively. Think carefully about the base case(s) and general case(s) for each method.

Notes/hints:

	public void drawSolid (int n, Graphics g, Color c, int side, int x, int y) {
        Draw nxn patches of color c in the square with length side at  point (x,y).
	  Assume that n is 2a, where a >= 0.

When you are done with this problem, submit the entire username_Quilts folder to your ps5 drop folder on Nike.