CS111, Wellesley College, Fall 2006

Problem Set 5

Due on Friday 27 October at the start of class

Reading

About this Problem Set

The purpose of this problem set is to give you experience with recursion. Task 1 is a pencil-and-paper problem in which you will draw an execution diagram (a JEM) for a recursive buggle program. In Task 2, you will use recursion in TurtleWorld to generate a self-similar picture. In Task 3, you will use recursion in BuggleWorld to produce quilt designs with bagels. In Task 4, you will use fruitful recursion in PictureWorld to generate a recursively structured quilt. The code for Tasks 2, 3, and 4 is available in the ps05_programs folder in the cs111 download directory on the cs server.

We have included working examples of the solutions to the programming problems in the test folder that you download with the ps05_programs folder. It is a good idea to run these programs to get a feel for what we are asking you to do. Your solution should provide the exact same results as our solution (except as noted in the individual problem descriptions below).

This is a bigger assignment than usual (four Tasks), but you have a longer time than usual to complete it (a week and a half). We strongly recommend that you complete Tasks 1 and 2 by Friday, Oct. 20, and then do Tasks 3 and 4 the following week.

With the introduction of conditionals and recursion, programming becomes much more challenging than on your first few assignments. It is a good idea to think carefully about how to solve your problems before you sit down at a computer and start writing code. It has been our experience that attempting to solve these sorts of problems while sitting at the computer is a recipe for disaster, because it can easily turn into a frustrating trial-and-error nightmare. Instead, we suggest that you form study groups and talk about solution strategies with your classmates before attempting to write any code. Once you have settled upon a strategy, we suggest that you write your Java code first using pencil and paper and simulate it in your head to check for its correctness. Only as a last step should you type in your code and test it on the computer.

Notes, hints, and suggestions for Tasks 2 and 3 are given on a separate page. We want you to have freedom to think about the problem in your own way. Reading the hints page is not required. It's main purpose is to help you think about the problem if you don't know how to get started.

How to turn in this Problem Set

You are required to turn in both a hardcopy and a softcopy. For general guidelines on problem set submission, including how to submit a softcopy and how to check if you softcopy submission was successful, click here. Please make sure to keep a copy of your work, either on your own computer, or in your private directory (or, to play it safe, both).

Hardcopy Submission

Your hardcopy packet should consist of:
  1. The cover page;
  2. Your JEM drawing for Task 1;
  3. Your modified SierpinskiWorld.java file from Task 2;
  4. Your modified BagelQuiltWorld.java file from Task 3.
  5. Your modified SierpinskiQuiltWorld.java file from Task 4;
Staple these together, and hand them in at the start of class on the due date given above.

Softcopy Submission

Save the modified SierpinskiWorld.java, BagelQuiltWorld.java, and SierpinskiQuiltWorld.java files in the ps05_programs folder. Submit the entire ps05_programs folder to your drop folder on the cs111 server.


Task 1: Java Execution Model for a Recursive Method

The following recursive method is defined for the Recurser subclass of Buggle:

public void spiral (int n)
{
     if (n > 0) {
          forward(n);
          left();
          spiral(n - 1);
          right();
          backward(n);
     }
}

Consider the following run() method in the RecursionWorld subclass of BuggleWorld:

public void run () 
{
     Recurser rita = new Recurser();
     rita.spiral(3);
}

For this task you are to draw a Java Execution Model diagram that models the execution of run() invoked on an instance (call it RW) of RecursionWorld with a 5×5 grid. Your JEM diagram should consist of three parts:

  1. An Execution Land that shows all five of the execution frames created by invoking the run() method on RW. Your diagram should depict the point in time when the invocation of run() returns. Although Java can discard an execution frame when control returns from it, you should not discard any frames when drawing your diagram.
  2. An Object Land that shows the initial RecursionWorld object along with any other objects created or manipulated as a result of the exectution of the run() method. Your drawing should show the state of Object Land after the completion of the execution of the run() method.

  3. A Grid Land that shows the state of the 5×5 grid after the completion of execution of the run() method.

Task 2: Sierpinski's Gasket

Sierpinski's gasket is a self-similar triangular entity discovered in 1916 by Polish mathematician Waclaw Sierpinski (1882-1969). In this problem we will investigate an approach for approximating Sierpinski's gasket.

Let sierpinski(levels, side) be the notation for a Sierpinski gasket with levels levels and side length side. Then a recursive mathematical definition of sierpinski() is:

The pictures below depict sierpinski(levels, side) for levels = 1 through 5. These are approximations to the "true" self-similar Sierpinksi gasket, which is the limit of sierpinski(levels, side) as levels approaches infinity.

sierpinski(1,100)

sierpinski(2,100)

sierpinski(3,100)

sierpinski(4,100)

sierpinski(5,100)

In this problem you will define a Java class with a method that uses a turtle to draw Sierpinski's gasket. To begin this problem, look in the SierpinskiWorld folder from the ps05_programs folder. The file SierpinskiWorld.java contains a subclass of TurtleWorld called SierpinskiWorld. The run() method it defines uses a SierpinskiMaker to draw a gasket. The SierpinskiMaker is positioned toward the lower left hand corner of the screen facing east. A gasket whose lower left-hand corner is positioned at this starting point will be automatically centered in the TurtleWorld window.

You must add the definition of the SierpinskiMaker class to this file. SierpinskiMaker is a subclass of Turtle that defines a void method called sierpinski() that draws Sierpinski's gasket given two parameters:

Write your sierpinski() method so that it draws the specified Sierpinski gasket and maintains the turtle's position, heading, and color as invariants. Your method should use recursion. You may define any auxiliary methods that you deem helpful.

Recall that the Turtle drawing primitives include the following:

public void fd (double n)
Move the turtle forward n steps.

public void bd (double n)
Move the turtle backward n steps.

public void lt (double angle)
Turn the turtle to the left angle degrees.

public void rt (double angle)
Turn the turtle to the right angle degrees. 

public void pu ()
Raise the turtle's pen up.

public void pd ()
Lower the turtle's pen down.
Additionally, there are also versions of fd(), bd(), lt(), and rt() that take int parameters, so you can invoke these methods with either an integer or double floating-point value.

You should not need to use any other Turtle primitives other than those listed above. In fact, many solutions use only a subset of the primitives listed above.

Test your definition by specifying levels and side in the parameter window and then clicking on the Run button in the TurtleWorld window. The Reset button will clear the screen. Good parameter values are in the ranges [0 ... 8] for levels and [100 ... 400] for side.

The test subfolder of the ps05_programs folder contains a working version of SierpinskiWorld.html that you can play with. Your solution should produce the same pictures as this test applet. It's worth noting that there are many possible ways to correctly decompose the problem, and the test applet uses only one possible approach. Thus, while you must produce the same designs as the test applet, you need not produce the designs in the same way as the test applet.

Notes/Hints/Suggestions


Task 3: Recursive Buggle Quilts

Quilt sales at the Buggle Bagel Rug Company have been flat recently. The market researchers at the company have determined that one factor accounting for the sluggish sales is the limited number of quilt sizes and quilt patterns offered by the company. In contrast, the recursively structured quilt patterns of their main competitor, the PictureWorld Quilt Company, have been selling like hotcakes.

To help improve sales, Quinton Buggle, the chief quilt designer at Buggle Bagel Rug, has developed a recursive bagel quilt pattern that can be used on a rug of any size. Here are examples of his pattern on square rugs with side lengths 16, 17, 18, and 19:

side = 16
side = 17
side = 18
side = 19

The fundamental repeated unit in these quilts is a triangle of bagels of size n, where n measures the number of bagels on a side. Here is such a triangle for n = 8:

Given a square rug with dimensions m × m, bagel triangles of size (m/2) are placed at the corners, and then the pattern is repeated in the smaller square bounded by the outer bagels in the center of the rug. Here (m/2) means integer division, as performed by Java. If m is odd, the result is truncated down to the nearest integer. For example, both 6/2 and 7/2 yield 3. Note that if m is even, all border cells of the square are covered with bagels, but if m is odd, the middle cell of each side of the square will not contain a bagel. Quinton has decided that the smallest square that should contain bagels is a 2×2 square. So the pattern is empty when applied to a 1×1 square.

In this problem, you must define the QuiltBuggle class in the file BagelQuiltWorld.java. Your class should define the following method:

public void quilt(int side)
Assume that the buggle starts in the lower left-hand corner of a square whose side length is side, facing parallel to the bottom side. Drops bagels to form quinton's pattern within this square, and returns to its initial position and heading.
Your quilt method should work for any integer; it should do nothing for side lengths ≤1. It should be defined using recursion. You will need to define numerous auxiliary methods, many of which themselves will be recursive.

You can test your code by running the applet from the BagelQuiltWorld.html file. In addition to the usual BuggleWorld window, this applet displays a parameter window that allows you to change the side length of the BuggleWorld grid. Pressing the Reset button causes the grid to have the side length specified in the parameter window.

The test subfolder of the ps05_programs folder contains a working version of BagelQuiltWorld that you can play with. Your solution should produce the same quilt designs as this test applet. It's worth noting that there are many possible ways to correctly decompose the problem, and the test applet uses only one possible approach. Thus, while you must produce the same designs as the test applet, you need not produce the designs in the same way as the test applet.

Notes/Hints/Suggestions


Task 4: Sierpinski Quilts

Background

Gilda Quilter of the Built-a-Quilt company has decided to incorporate Sierpinski gasket designs into her latest line of quilts. When ordering one of her new quilts, a customer must specify two colors and a number that indicates the "level" of the Sierpinski gasket designs used in her quilts. Here are some pictures of her Sierpinski quilt designs for the colors black and cyan and level numbers ranging from 1 to 4:

sierpinskiQuilt(black,cyan,1) sierpinskiQuilt(black,cyan,2)
sierpinskiQuilt(black,cyan,3) sierpinskiQuilt(black,cyan,4)

Tragically, Gilda is an artist with no programming background (where was media arts when she went to college?), so she has to draw all her designs by hand. Since this is extremely error prone and tedious, the Built-a-Quilt company has decided to hire a Java programmer who can express Gilda's designs in PictureWorld. Impressed by your work on the previous Problem Set 5 problems, Built-a-Quilt hires you to implement the following method:

  public Picture sierpinskiQuilt (Color c1, Color c2, int n)
  Assume that n >= 1. Returns one of Gilda's Sierpinski quilts that is 
  constructed out of n-level Sierpinski gaskets colored with c1 and c2.

When you carefully study Gilda's quilt patterns, you see that there are two auxiliary methods that would greatly simplify your task:

  1. The first auxiliary method is one that makes sierpinki gasket pictures in Picture World using two colors and a level number:
      public Picture sierpinski (Color c1, Color c2, int n)
      Assume that n >= 1. Returns a picture of an n level sierpinski gasket whose 
      "body" has color c1 and whose "holes" have color c2. The resulting picture 
      should be a "lower left" triangle -- that is, a triangle two sides of which 
      are the left and bottom edges of the frame in which it is displayed.
    
    For example:

    sierpinski(black,cyan,1) sierpinski(black,cyan,2)
    sierpinski(black,cyan,3) sierpinski(black,cyan,4)

  2. The second auxiliary method is one that makes an interesting picture out of any given "lower left" triangle:
      public Picture triangles (Picture tri, int n)
      Suppose that tri is a "lower left" triangle and n >= 1. 
      Returns a picture whose lower left triangle is filled with tri and whose
      upper right triangle is an n-level Sierpinski gasket whose "body" is white
      and whose "holes" are filled with copies of tri. 
    

    For example, suppose that rgTri is the following lower-left triangle:

    rgTri
  3. Then here are some examples of triangles() called with rgTri and various level numbers:

    triangles(rgTri,1) triangles(rgTri,2)
    triangles(rgTri,3) triangles(rgTri,4)

Your Task

Your task is to define the sierpinski(), triangles(), and sierpinskiQuilt() methods in the file SierpinskiQuiltWorld.java in the SierpinskiQuilt folder. In addition to the usual methods of the PictureWorld contract, you may also use the following methods:
  public Picture triangle (Color c)
  Returns a "lower left" triangle filled with color c.

  public Picture rotations (Picture p)
  Returns a picture consisting of four copies of p rotated
  around the center of the picture. 

You may find it helpful to define additional auxiliary methods in order to complete your task.