CS111 Spring 2008

Take-Home Exam 2

Due Tuesday 22 April 2008 at 9:30 a.m.

Instructions

Submitting Your Exam

In each task, you will be writing Java code. In Task 3, part D, you are also asked to draw an iteration table.

Advice


Task 1: StarWorld [20 points]

In this problem you will explore recursion in PictureWorld. In the exam2_programs directory, you will find a file StarWorld.java (in the StarWorld directory), which you are to edit. Specifically, add a method to StarWorld.java whose prototype is:
public Picture  makeStarPyramid(Color c1, Color c2, int levels);

Define this recursive method to create the patterns shown below. Notice that in the file there is one primitive defined for you, which draws a star as shown below:

Pyramids built from the star pattern are shown below:

When the level is 0 (zero), the picture should be empty. Note that in the file StarWorld.java, the method invocations to create these pyramids have already been included for you. All you need to do is fill in the body of the method makeStarPyramid() such that it generates the pictures above. You may use methods from the PictureWorld contract. You do not need to define any other auxiliary methods in your solution.


Task 2: BagelMapper [45 points]

Background

Magellan Buggle is an explorer who wanders about the grid searching for new bagels to consume. As he goes, he creates a map of the grid to record the locations of all the bagels. Since map-making requires lots of energy, Magellan eats every bagel in the grid in the process of making the map. He leaves behind a colored square in every cell where there used to be a bagel.

Assume that Magellan always explores the grid by starting in its southwest corner facing east. For instance, here is a snapshot of Magellan before he maps a rich bagel field on the island of Bago Bago:

Magellan poised to map a field of tasty bagels.

Below is a picture of the same field after Magellan has mapped it. He has eaten every bagel and left behind a patch of his color (which happens to be red) in every cell where there used to be a bagel. (If Magellan were instead blue, he would leave a blue patch.)

The state of the bagel field after Magellan has mapped it.

The map that Magellan creates is printed on the console as a sequence of IntLists. For the above example, Magellan's printed map is shown below. Notice that the first line ([5, 1, 0, 1, 1, 1, 1]) in the map below shows the mapping of the base row of the grid, i.e., the row where the buggle starts out:

[5, 1, 0, 1, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 1, 0]
[3, 0, 0, 0, 1, 1, 1]
[3, 1, 1, 0, 0, 0, 1]
[6, 1, 1, 1, 1, 1, 1]
map whole grid:  18

The integers in the map can be understood with the help of the color-coding shown below:

5 1 0 1 1 1 1
0 0 0 0 0 0 0
1 0 0 0 0 1 0
3 0 0 0 1 1 1
3 1 1 0 0 0 1
6 1 1 1 1 1 1
After the maps of each row print out, the total number of bagels in the entire grid is printed.

As a second concrete example, consider another 6×6 field with 20 bagels:

A second tempting field of bagels before mapping.
The second field after mapping.
The map of the second field.
[3, 0, 0, 1, 0, 1, 1]
[5, 1, 1, 1, 1, 0, 1]
[5, 1, 1, 0, 1, 1, 1]
[1, 0, 0, 0, 1, 0, 0]
[1, 0, 0, 1, 0, 0, 0]
[5, 1, 1, 1, 0, 1, 1]
map whole grid:  20
Some General Notes:

Task 2a: mapRow()

Define the following instance method in the BagelMapper class:

public IntList mapRow()
If (1) the buggle is c cells away from the wall that is in the direction it is headed (counting the cell the buggle occupies) and (2) these c cells contain b bagels, then invoking mapRow() engenders the following behavior:
  • The buggle eats all b bagels under and in front of it, and paints each cell formerly containing a bagel with the buggle's color.
  • After mapRow() returns, the state of the buggle should be exactly the same as it was before mapRow() was invoked.
  • The method returns (but does not print) an integer list with c + 1 elements, where the first element is b and the other c elements are a "map" of 0s and 1s indicating the former positions of the b bagels as described above.
Notes:

Task 2b: Recursive mapGrid()

Define the following instance method in your BagelMapper class:

public int mapGrid()
If
  1. the buggle is in cell (1, 1) of the buggle grid facing east;
  2. the eastern wall is c cells away from the buggle (including the cell the buggle is in);
  3. there are b bagels in the c × r subgrid defined by the position of the buggle and the eastern and northern walls.
Then invoking mapGrid() engenders the following behavior:
  • The buggle eats all b bagels in the c x r subgrid. and paints each cell formerly containing a bagel with the buggle's color.
  • For each row (starting at the bottom), mapGrid() prints out the list generated by mapRow() for that row.
  • The method returns (but does not print out) an integer corresponding to the total number of bagels in the grid.
  • After mapGrid() returns, the buggle should be at its starting position, i.e., the start of the bottom row of the grid.
Notes:

Task 3: An Application: Prime Numbers [35 points]

Write an application that takes an integer command line argument, tests it, and outputs a message saying whether the argument is a prime number or not. As a reminder, an integer is a prime number if it has only two distinct divisors: 1 and itself. For example, 7 is a prime number, since it only has two distinct divisors: the number 1 and itself. On the other hand, the number 12 is not a prime number, since it has more divisors than the trivial ones (1 and 12); namely, the numbers 2, 3, 4, and 6 are all non-trivial divisors of 12. Non-prime numbers are called composite numbers.

Create a class, named Primality, in a file named Primality.java. Then complete the following 4 subtasks (you draw the iteration table first if this helps you write the code):

  1. A class (static) method that takes an integer to be tested for primality. This method should count and return the number of its factors. The definition of this method must employ a while loop.
  2. A class (static) method with identical behavior that uses a for loop instead.
  3. A class (static) main() method that reads the input from the command-line, invokes the methods above, and prints an informative message about the primality of the input number. Make sure to invoke both of the methods above to test them thoroughly. You may comment out testing code, but do not delete testing code: we want to see it.
  4. Draw an iteration table that describes the loops you wrote for computing the number of factors of a number.

The figure below illustrates an execution of a working application from the "Interactions" tab at the bottom of the Dr. Java window.