Graphic by Keith Ohlfs

CS111, Wellesley College, Fall 1999

Problem Set 6

Due: Friday, October 29 by 4:00 p.m.

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


Note: this is the final (updated) version of Problem Set 6, which includes the description for Problem 4. In order to do Problem 4, you should download the MazeCounter_new folder, which can be found in the ps6_programs_new folder.

When you are downloading, be careful not to overwrite any work that you have done on your personal copy of ps6_programs! To reduce the likelihood of such an accident, the ps6_folder and its three subfolders have all had a "_new" attached to the end of their names.

The Patchwork_new folder contains a new Test subfolder with a Patchwork.html applet that shows quilts for levels 1 through 6. You should experiment with this test applet as part of doing the assignment. The Patchwork.java skeleton has also been extended with menu items for al 6 quilts.

The Patchwork folder also has been changed to include examples of the patchwork quilt at values of n from 1 through 6. Since you'll want to save any work you've already done in your personal copy of Patchwork.java, you should just copy the initializePictureChoices() method from the new version to your current one.

The HungryWorld_new folder is exactly the same as the old version.

Note: the following problem set description refers to folder names without the "_new"suffix.


Reading Assignment:

About this Problem Set

The purpose of this problem set is to give you more experience with recursion, including practice with invocation trees, and writing recursive methods which return values.

There are four problems on this assignment. It is recommended that you work on the first two before your laboratory, and work on the second two after your laboratory.

How to turn in this Problem Set

Homework Problems :

Turn in only one package of hardcopy materials. Staple your files together with the cover page, and submit your hardcopy package by placing it in the box outside of Jennifer's office (E104, directly across from E101).

Reminders


Homework Problem 1: Invocation Tree for Pascal's Triangle

As discussed in class, an invocation tree is an abbreviated version of a Jave Execution Model. It contains a node for each method called during the execution of a program. Each node contains the name of the method, the values of its parameters, and (where applicable) its result. There is an edge connecting each "parent" node to the "children" nodes for the method invocations encountered directly within the body of the parent. The children nodes are all shown at the same vertical level, arranged from left to right in the order of their execution.

For example, consider a recursive definition of the Fibonacci function:

	public int fib (int n) {
		if (n < 2) {
			return n;
		} else {
			return fib(n-1) + fib(n-2);
		}
	}

Below is an invocation tree for the invocation fib(6). Each node has the form fib(n):r, where n is the parameter of the invocation of fib and r is the result returned by the invocation.

In this problem, you will draw an invocation tree for the invocation of a method that computes an element of Pascal's triangle. Pascal's triangle is a triangular arrangement of numbers whose outer edges consist of 1s and each of whose inner elements is the sum of the two numbers immediately above it to its right and left:

                      1
                   1     1
                1     2     1
             1     3     3     1
          1     4     6     4     1
       1     5    10    10     5     1
    1     6    15    20    15     6     1

Let P(r,i) indicate the value of the ith element in the rth row of Pascal's triangle, where rows are numbered from top to bottom starting with 0, and elements in a row are numbered from left to right starting with 0. For example, P(4,0) = 1, P(4,1) = 4, and P(4,2) = 6.

Here is a recursive method that computes the value of P(r,i):

 
	public int P (int r, int i) {
	  if ((i == 0) || (i == r)) {
  		return 1;
  	} else {
  		return P(r - 1, i - 1) + P(r - 1, i);
  	}
	}

For this problem, you are to draw a complete invocation tree for the invocation P(6,4). Each node of your tree should have the form P(r,i):a, where r is the row number, i is the element number within a row, and a is the answer returned by the invocation. Be sure to draw your nodes small enough so that they all fit on a single piece of paper. All children of the same parent should appear at the same vertical level, as in the fib example above.


Homework Problem 2: 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 1 and 6, experiment with the test applet Patchwork.java within the Test subfolder of the Patchwork folder.

In your definition, you should pay careful attention to how the color parameters shift in recursive calls. Your final definition should be very short. You should be able to write the entire 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. 


Homework Problem 3: Hungry World

This problem deals with a buggle that has the following behavior. The buggle starts out in the middle of the first row of a grid facing north. The grid cells are randomly populated with a user-specified number of bagels; however, the bagels are initially placed so that none are in the column in which the buggle initially faces. The buggle moves row-by-row from the bottom row to the top row, and performs the following action at every row:

For example, suppose the initial grid configuration is:

Then the final grid configuration should be:

To see a working applet, look in the HungryWorld folder for the Test folder. Open the HungryWorld.html file from the Test folder with the Applet Viewer. Experiment with the HungryWorld to make sure you understand the task.

Part a. Assume that you are provided with a public void eatRow() method that implements the row-eating behavior described by the three bullets above. Use this method to implement the following method:

public void eatRows()
Apply the eatRow() method at every row in the grid as the buggle moves
from the bottom row to the top row. 

Part b. Assume that you are provided with the following methods:

public int countBagels()
Return the number of bagels between the buggle and the wall it is facing.
Calling this method should not change the state of the buggle. 
	
public void eatBagels()
Eat all the bagels between the buggle and the wall it is facing, 
leaving behind a colored square in every cell that originally contained a bagel.
Calling this method should not change the state of the buggle. 
Using the above two methods, implement the public void eatRow() method mentioned in part a.
 
Part c. Implement the  public int countBagels() method described in part b.
 
Part d.  Implement the  public void eatBagels() method described in part b.


Homework Problem 4: MazeCounter

Maisy Buggle comes from a long line of maze-navigating buggles. But unlike other buggles, she is not satisifed with stopping when she finds the first bagel in a maze. She wants to explore the world and find out how many bagels there are in the entire maze! For instance, suppose Maisy starts at "home" (facing eastward at point (1,1)) in the following maze:

Using the same maze-navigating strategy followed by the PathFinder buggle in Lecture 13, Maisy can traverse the entire maze and determine that it contains 17 bagels. (You should review the PathFinder strategy by studying the code for findBagel() in PathFinderWorld.java in lec13_programs, which is located in the CS111 download folder.) Suppose that, after exploring the submaze accessible from a given cell, Maisy writes in the cell the number of bagels in that submaze. Then after she returns to her home position, the maze would be annotated as follows.

Note that the number in each cell is the number of bagels to the left, right, in front of, and under Maisy when she is in a cell (assuming she is facing the direction in which she entered the cell).

To get a better sense for how the above numbers are generated, you should experiment with the test applet MazeCounterWorld.html within the Test subfolder of the MazeCounter folder of ps6_programs. This applet has a parameter window that look like this:

The numbers in yellow control the side length of the maze and the number of bagels placed randomly in the maze. If you change these and press the Reset button, a new maze will be created with these parameters. The cyan-colored box labelled result is where Maisy writes the total number of bagels in the maze when she returns home. For example, after Maisy has explored the maze depicted above, the parameter window changes to:

Before proceeding with this problem, you should play with the test applet and study the above pictures until you understand how the numbers for each cell are determined.

Maisy is a member of the MazeCounter class of buggles. Your goal in this problem is to define a collection of methods in the MazeCounter class that direct Maisy to count the number of bagels in any maze, also annotating the cells with numbers, as shown above. You should flesh out the skeleton of the MazeCounter class in the MazeCounter folder of ps6_programs. This skeleton contains a "stub" version of a method with the following contract.

public int countBagels()
Counts the number of bagels to the left, right, in front of, and under this buggle.Drops this number in the cell under this buggle, and then returns it.Leaves the state of this buggle unchanged.

You should replace the stub with a method that works. As part of your solution, you should define any auxiliary methods that you find helpful. It is strongly recommended that you use a decomposition similar to the one used by the PathFinder buggle in lec13_programs. Your solution should work for any maze size and any number of bagels. (Warning: do not attempt to place more bagels in the maze than there are grid cells. This situation is not handled gracefully, and will cause your machine to crash.)

In order to drop an integer in a cell, you will need to invoke the following brand-spanking-new Buggle primitive. (Only the buggles in MazeCounter world understand this method.)

public int dropInt (int n)
"Drops" the integer n in the cell of this buggle. The cell shows the most recently dropped integer. This method returns the integer that was dropped.