Graphic by Keith Ohlfs
CS111, Wellesley College, Fall 2001

Problem Set 6

Due Tuesday, October 30, 2001, at 11pm

[CS111 Home Page] [Syllabus] [Assignments] [Documentation] [FAQ] [CS Dept.] [CWIS]

About this Problem Set

The purpose of this problem set is to give you more experience with recursion and writing recursive methods which return values. The code for this problem set is available in the ps6_programs folder in the cs111 download directory on nike.

How to turn in this Problem Set

Submit the folders Patchwork and HarvestWorld to your drop folder on the cs111 server. Before submitting your work, make sure that all the files have been saved and the projects compile and run as they should. After submitting your work, make sure to doublecheck that all the files have been uploaded correctly. If you need directions on how to submit your work or how to check whether the submission was successful, please click here. Please make sure to keep a copy of your work, either on a zip disk, or in your private directory (or, to play it safe, both).


Homework Problem 1A: Patchwork

Your goal in this problem is to write a recursive method that draws the following Picture in PictureWorld:

This picture is specified by the following method in the file Patchwork.java within the folder Patchwork:

patchwork(6, Color.red, Color.yellow, Color.blue, Color.green);

In this problem, you are to write a recursive definition of the following method:

public Picture patchwork(int levels, Color c1, Color c2, Color c3, Color c4)
Returns a picture of the above pictured staircase pattern nested levels levels deep, using colors c1, c2, c3, and c4

To see the picture created by patchwork(n, Color.red, Color.yellow, Color.blue, Color.green) for values of n between 0 and 6, experiment with the test applet Patchwork.java within the Test subfolder of the Patchwork folder. Select patchQuiltn from the pulldown menu in the applet, where n ranges from 0 to 6.


Homework Problem 1B: boxQuilt

Your goal in this problem is to write a recursive method that draws the following Picture in PictureWorld:

This picture is specified by the following method in the file Patchwork.java within the folder Patchwork:

boxQuilt(3, Color.red, Color.yellow, Color.blue, Color.green);

In this problem, you are to write a recursive definition of the following method:

public Picture boxQuilt(int levels, Color c1, Color c2, Color c3, Color c4)
Returns a picture of the above pictured boxy pattern nested levels levels deep, using colors c1, c2, c3, and c4

To see the picture created by boxQuilt(n, Color.red, Color.yellow, Color.blue, Color.green) for values of n between 0 and 4, experiment with the test applet Patchwork.java within the Test subfolder of the Patchwork folder. Select boxQuiltn from the pulldown menu in the applet, where n ranges from 0 to 4 (be warned that boxQuilt4 takes a long time to draw). Please ignore references to cubeQuilt in the pulldown menu 


Important Notes:

Your final definitions for patchwork and boxQuilt should be very short. You should be able to write each definition in a handful of lines without defining any auxiliary methods of your own. However, you should use the following auxiliary methods, which we have provided for you:

public Picture patch(Color c) 
   Return a picture that consists of a solid patch of color c
    
public Picture fourPics(p1, p2, p3, p4) 
   Return a picture that consists of four pictures p1, p2, p3 and p4, with
       p1 in the upper left corner, p2 in the upper right corner, p3 in the
       lower left corner and p4 in the lower right corner. 
   
   public Picture clockwisen (Picture p) 
    Returns a Picture which is the original Picture p rotated n degrees (n = 90, 180, or 270) in the clockwise direction.   
   
   public Picture fourSame (Picture p) 
           Returns a Picture which contains the original Picture in each of the four corners. 
   
 public Picture rotations (Picture p)
           Returns a Picture which consists of four pictures p1, p2, p3 and p4, with each sub-picture a rotated version of the original Picture.
        p1 is Picture p rotated clockwise270 in the upper left corner, p2 is Picture p in the upper right corner, 
        p3 is Picture p rotated clockwise180 in the lower left corner, and p4 is Picture p rotated clockwise90 in the lower right corner.
   
 public Picture corner (Picture p1, Picture p2) 
           Returns a Picture which consists of p1 in the lower left corner and p2 in the other three corners.
  
    


Homework Problem 2: HarvestWorld

Note: Please read this entire problem carefully. Simply generating the correct final pattern is not enough to get full credit. Each specified method must behave as described below.

Bagels in BuggleWorld grow in fields (you knew that, didn't you?). Twice a year the Harvester buggles go out and harvest bagels so that buggles in BuggleWorld will have bagels to eat and play with throughout the year. Bagels are the most important commodity in BuggleWorld. Therefore, their harvesting procedure is quite complex and elaborate. It is described below:

Each Harvester is assigned a field of bagels to harvest. The buggle does not know the width or height of the bagel field. The buggle starts in the left bottom corner of the field and harvests bagels one row at a time (a row is a vertical column on the BuggleWorld grid). It doesn't matter if the buggle chooses to harvest the closest row first and work down to the end of the field or to walk to the end of the field and harvest rows on the way back to the beginning. Note that there are no bagels planted in the bottom horizontal row of the field. Instead, that is the path that buggles walk on to get from row to row.

Each row is harvested according to the following procedure:

The buggle harvests the bagels in the row by picking them up and counting them. It doesn't matter if the buggle picks up bagels on the way to the end of the row or picks them up coming back from the end.

The buggle stacks the bagels in a row near the path (starting from the second cell up) so that they will be able to dry (dried bagels are the best and keep fresh all year long).

The rest of the field needs to be covered with tarp (black) which preserves the field from bad weather during the non-growing season. The tarp is at the far end of the row (i.e. at the top of the BuggleWorld grid). The buggle needs to go to the end of the row and pull the tarp up to the point where the bagels are stacked. The buggle should count the number of empty spaces left in each row (spaces covered by the tarp).

While the Harvester buggles do all the work, their supervisors want to be able to quickly look at the field and see how it did. It's a bit difficult to see the entire field, so the Harvester buggle must mark each row indicating whether or not the row did well. Bad rows (fewer bagels than blank spaces) are marked red and will get more fertilizer and attention in the next growing season. Good rows (more bagels than blank spaces) are marked green. Fair rows (same number of bagels as blank spaces) are left uncolored. Each row is also tagged with the number of empty spaces (i.e. spaces covered with black tarp). See the pictures below for clarification.

Finally, the Harvester buggle must count the number of bagels harvested in the field and report that to her supervisor.

The following two pictures show the state of a field before and after the buggle has harvested it.


Your task is to write the methods that will make the Harvester buggle do its job. You are free to write any auxiliary methods needed. At a minimum, you must define the following methods for the Harvester class. Each method must perform as specified in the comments below:

Your task is to write the methods that will make the Harvester buggle do its job. You are free to write any auxiliary methods needed. At a minimum, you must define the following methods for the Harvester class. The complete specifications for each method are given in HarvestWorld.java (as usual, you can download the program files from the download directory). Each method must satisfy the specification in the code file. The descriptions below are intended to supplement and clarify the specifications in the code file. Note that all the methods described below must meet the following invariant:
The buggle's state (position, heading, color, and brush state) will not be changed by execution of this method. Assumes the buggle's brush is initially up.

public int harvestField()
This method assumes that the buggle is starting in the lower left corner of a field facing EAST. When this method is invoked, the buggle will harvest the field of bagels to its front and left (up to the walls in front of and to the left of the buggle) and return the number of bagels harvested in this field.

public int harvestRow()
When this method is invoked, the buggle will harvest the row of bagels to its left (i.e. the vertical column above the buggle). The number of bagels harvested in this row is returned. This method should be invoked when the buggle is facing EAST and in the bottom row (the clear path) of the field.

public int harvestBagels()
When this method is invoked, the buggle will pick up all the bagels between it and the wall and return the number of bagels picked up. This method should be invoked when the buggle is facing NORTH and in the bottom row (the clear path) of the field.

public void stackBagels(int numberBagels)
When this method is invoked, the buggle will create a stack of the specified number of bagels in front of itself. This method assumes that there will always be at least enough space in front of the buggle for the bagel stack. This method should be invoked when the buggle is facing NORTH and in the bottom row (the clear path) of the field.

public int pullTarp()
When this method is invoked, the buggle should draw a black line from the wall in front of the buggle to the current cell in front of the buggle (i.e. do not color the cell the buggle is on when the method is invoked). This method returns the number of cells colored. This method should be invoked when the buggle is facing NORTH and at the end of its bagel stack (i.e. right where the tarp should start).

public void markRow(int numberBagels, int numberSpaces)
This method paints the current cell green if numberBagels is greater than numberSpaces. The current cell is painted red if numberBagels is less than numberSpaces. The current cell is not painted if the numberBagels is equal to numberSpaces. The buggle also marks the cell with the numberSpaces (using dropInt());

Notes:

Use paintCell to color cells:
public void paintCell (Color c)
Paints the cell under this buggle with the color c

Use dropInt to drop an integer into a particular cell:
public int dropInt (int n)
Drops the integer n into the current cell and returns the integer dropped.

Test your methods by executing the HarvestWorld.html applet. When you first load the applet, the BuggleWorld grid is empty. Hit Reset to generate a bagel field. Every time you Run this applet, the number of bagels reported by the Harvester buggle will appear in the cyan box within the parameter frame window.

A working applet can be found in the test subfolder of the HarvestWorld folder. You should experiment with this test applet to better understand the behavior of Harvester buggles.

Hint: It may be easier to write the methods in the reverse order from the order listed above. That is, you might want to start by writing the markRow() method.