CS111, Wellesley College

Lab 6

Wedn, Oct 10, 2007


Today

Download the lab6_programs folder from the cs111d account on cs.wellesley.edu. For the following tasks, write your recursive methods and add testing code in the BagelWorldRec.java file that is provided for you, in lab6_programs.

Note that the tasks today might be challenging for many people. Try as many as you have time during the lab, and don't be disappointed if you don't get to all of them. However, make sure you get back to it, study and understand the solutions once they are posted (some time late on Wednesday.) Tasks 3, 4 and 5 can be worked on in any order. Just choose what is more appealing to you!

The Console window

You are advised to add System.out.println() statements to the recursive methods that you will be writing. These statements, when thoughtfully placed, can help you understand how your program works, and can be a big help in debugging when your program doesn't work, or doesn't work correctly. Each time a
System.out.println("message");
statement is reached, the "message" is printed in the console window, within DrJava. Remember to examine the console as you run , or step through your program, and clear it between program runs (in DrJava: Tools --> Clear Console).

The size of the grid

In some of the problems today, you may need a bigger or smaller grid. You can change the dimensions of the grid from within the BuggleWorld.java file: On the top of the file, there are two lines of code responsible for setting the size of the grid.
public int rows = 20; // Number of rows. public int cols = 20; // Number of columns.
You can set those variables to the numbers you want. (Of course, don't forget to re-compile!)

Testing your methods

Call each method you write in the BagelWorldRec class, from within the run(). Test your methods a few times, with different arguments each time, to make sure they work fine.

Task 1 -- throwBagel()

Write a recursive method, throwBagel(), that places a bagel a number of cells forward from the buggle's position. The number of cells forward is passed as a parameter to the method. The position and heading of the buggle should not change after the method has been invoked. The cells between the buggle's starting position and the bagel's position will be colored the same color as the buggle's color.
Here is a snapshot of invoking the throwBagel() method:

snapshot of throwBagel(5) invocation

Task 2 -- JEM Diagrams for Recursive methods

Draw a Java Execution Model diagram that shows all of the execution frames created by invoking the run() method on an instance of the BagelWorldRec class. The method run() should look like this:

  public void run() {
    BagelBuggleRec betty = new BagelBuggleRec();
    
    betty.throwBagel(3);
    
  } 

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.

Task 3 -- skip()

Write a recursive method, skip(), that produces a row of bagels in every other cell on a grid. The method takes one integer parameter, n, that is the number of bagels that are dropped in the row. The position and heading of the buggle are not changed after the invocation of the method. Write all your code within the skip() method (no auxiliary methods used), and do not use the paintCell() method. You can assume there is enough space on the grid for all the bagels to be placed. Here is a snapshot of invoking the skip() method:

snapshot of skip(8) invocation

Task 4 -- triangle()

Write a recursive method, triangle() that produces a triangle of bagels, as shown below:

snapshot of triangle(10) invocation

Of course, the size of the basis of the triangle should be flexible.

Task 5 -- spiral()

Write a recursive method, spiral() that produces a spiral of bagels, as shown below:

snapshot of spiral(17) invocation

Of course, the size of the outside spiral should be flexible. Notice that you get better-looking spiral in the center when the size of the side of the biggest spiral is a power of 2 + 1 (i.e. size of side of biggest spiral = 2^n +1). Consider this when you are testing your method.