CS111, Wellesley College, Fall 2006

Problem Set 2

Due: Friday 22 September at the start of class

About this Problem Set

This problem set is intended to help you understand how Java works and to give you practice using, designing, and writing methods. This problem set consists of a paper-and-pencil problem and two programming problems. There's also an ungraded challenge problem for anyone interested in testing the limits of their programming savvy. There is a contest associated with the challenge problem (please see below) with PRIZES (hint: think Bagels!).

To get the code for this assignment, connect to the cs111 download directory via Fetch and download the folder ps02_programs.

How to turn in this Problem Set

Save the modified Writing2.java and RugWorld.java files in the ps02_programs folder. Submit the entire ps02_programs folder to your drop folder on the cs111 server. Turn in a hardcopy of the JEM diagram from task 1 and your modified Writing2.java and RugWorld.java files. If you chose to work on the ungraded challenge problem, you should also turn in the FontBuggle.java and HuggleWorld.java files.

When submitting your hardcopies, turn in only one package of materials. Please staple your files together with a cover page, and submit your hardcopy at the start of class on the due date. Dont forget to put one of the instructors' stickers on the cover page!

IMPORTANT NOTES:

  1. Pay careful attention to upper and lower case letters in the filenames.
  2. Once you have used Fetch to upload a file to the cs111 server, you should be sure to doublecheck that the file was actually uploaded. You can do this in Fetch by verifying that the file is now listed in your directory. Not only should you check that the file is listed, but you should check that it does not have a size of 0K. If the file isn't listed or if the size for the document is 0K, this means that there was an error in transferring it via Fetch and you must re-upload the document. When transferring a folder, you should check that its contents have been uploaded correctly. That is, you should be sure to check that every single file that you wish to submit has been uploaded correctly. Often times, although not always, you will see a message "Connection Failed" when there is an error in transferring your files.
  3. It is your responsibility to keep a personal back-up of every file that you submit electronically until you have received a grade for the assignment. Please make sure to keep a copy of your work, either on your own computer (or some other storage medium), or in your private directory (or, to play it safe, both).

Task 0: Meet the Instructors

Go to one of an instructor's office hours (your choices are: Sohie, Mark, and Lyn; but you cannot see the same person you saw last week). If you cannot make office hours, you must schedule an appointment. Go to the office and secure a sticker from the instructor you meet. Place that sticker on the problem set cover page. Now you have met two of us, and you have one more to go!

You cannot get credit for this assignment without a sticker, and we will NOT give you a sticker in class or lab.

Task 1: Java Execution Model

In this part of the homework, you will use the Java Execution Model to draw an Execution Diagram that summarizes the execution of a simple Buggle program. It is important to become familiar with the conventions for drawing Execution Diagrams, since they are an important tool for explaining the behavior of Java programs. In particular, Execution Diagrams explain the meaning of method invocation, parameter passing, local variable declarations, and the this variable. You will be expected to draw an Execution Diagram on Exam 1.

Your JEM Assignment

Below are the declarations for two classes: a SwapWorld class that is a subclass of BuggleWorld and a SwapBuggle class that is a subclass of Buggle.



public class SwapWorld extends BuggleWorld 
{
     public void run () 
     {
          SwapBuggle bg1 = new SwapBuggle();
          SwapBuggle bg2 = new SwapBuggle();
          SwapBuggle bg3 = new SwapBuggle();
          Point pt1 = new Point(6,3);
          Point pt2 = new Point(4,5);
          bg1.setPosition(pt1);
          bg2.setPosition(pt2);
          bg3.setPosition(new Point(pt1.x - pt2.x, pt1.y + pt2.y));
          bg2.setColor(Color.blue);
          bg1.setColor(Color.green);
          bg2.left();
          bg3.right();
          bg2.swap(bg3);
          bg3.swap(bg1);
     }
}

class SwapBuggle extends Buggle 
{
     public void swap (Buggle bg1) 
     {
          Point pt1 = this.getPosition();
          Point pt2 = bg1.getPosition();
          Color c1 = this.getColor();
          Color c2 = bg1.getColor();
          this.setPosition(pt2);
          bg1.setPosition(pt1);
          this.setColor(c2);
          bg1.setColor(c1);
     }
} 

Suppose that the run() method is invoked for an instance of SwapWorld. Your assignment is to draw an Execution Diagram for this method.

Please be careful. This code is specifically designed to be tricky in a number of places. Be sure to pay attention to the following:


Task 2: Buggle Word Writing using Methods

It is possible to use methods to expand the capabilities of the buggle word writing program from problem set 1. For example, using a larger grid, but fewer actual lines of code, a buggle can write the word "JAVA" multiple times, around the perimeter of the grid, as shown below:

Your assignment:

Using methods, create the above grid. The file Writing2.java contains Java code that creates a larger grid and defines a LetterBuggle, a new class of objects that extends the Buggle class. A new LetterBuggle named ellie has also been created for you, along with two LetterBuggle methods; writeName(), and writeJ(), as shown below:


// write the word "JAVA" around the perimeter of the grid

public void run () 
{
     LetterBuggle ellie = new LetterBuggle();
     ellie.writeName(Color.red, Color.blue, Color.yellow);

     // statements that write the word in various colors around 
     //the perimeter of the grid

}

class LetterBuggle extends Buggle 
{
     // write the word "JAVA", in the appropriately colored letters, 
     // by invoking methods with appropriate color parameters for 
     // writing the individual letters
     // Note that the J and the V are always the same color, and 
     // each A can be a different color from the 
     // color of the J and V.
     public void writeName (Color c1, Color c2, Color c3)
     {
          // fill in with statements that will position this Buggle 
          // correctly to start writing
 
         this.writeJ(c1);  // write the "J" in color c1

         // fill in with statements to write the "A V A"  
         // in the correct colors

     }

     // write the letter "J"
     public void writeJ(Color jcolor) 
     {
          // set the buggle's color to jcolor

          this.left();  // face NORTH
          this.forward();  // draw hook in lower left of "J"

          // the rest of the statements for writing a "J"
  
     }
                     
     // methods for writing the other letters in the word JAVA
                     
}

Perform the following steps to solve the problem: You must conform to the following rules:
  1. You may not invoke the setPosition() or setHeading() methods.
  2. You should use the methods brushUp() and brushDown() where appropriate.
  3. Each letter except "V" should fit in a 4x5 grid, and should be separated from the next letter by one blank space.
  4. The letter "V should occupy a 3x5 grid as shown.
  5. Ellie must draw the letters in the appropriate colors. All the colors used in the picture above are Color constants from the Color class. These constants are only used in the run() method. They may not be used anywhere else in your code.
  6. Ellie must end up in the position shown above, facing the correct direction (EAST).
  7. Use methods to solve the problem.

Task 3: The Buggle Bagel Ruggle Company


Important note: Before you begin working the third task, make sure to save and close all Java files from the previous task.
The buggles from Problem Set 1, becky, bobby, bertie, billy and benny, had the foresight to copyright their Buggle Olympic Symbol. As a result, they made a killing on the use of the logo for Buggle Olympics memorabilia and merchandise. So, they decided to invest in a rug-making enterprise: The Buggle Bagel Ruggle Company, which designs and weaves rugs made by dropping bagels in interesting ways on a BuggleWorld grid. Here is an example of a rug they created:

The buggles are great designers, but, unfortunately, they don't know much about manufacturing. It takes so long to hand-drop the bagels individually that it's impossible to make any money. Luckily for them, there is a way to automate the production of the rugs. As it turns out, the design shown above can be produced using just 4 different 3x3 grids of bagel patterns:

pattern 1

pattern 2

pattern 3

pattern 4

The rug itself is created by placing these patterns side by side to tile the entire rug. The rug, divided into 3x3 grids (outlined in black lines), is shown below:

The rug pattern includes a lot of repetitive patterns. This means that there are many opportunities for using methods to generate the pattern efficiently. Your task is to write code to create the rug pattern shown above in the most efficient manner you can think of. The code for this problem is contained in the RugWorld folder. The file RugWorld.java contains the initial set-up for creating the rug shown above. becky, bobby, bertie, benny and billy have hired a RugBuggle (another new class of objects which extends the Buggle class) named weaver to produce the rugs. You should add your code to this file, but you should not remove any existing code.

You must observe the following constraints:

Helpful Hints:


Non-Graded, Non-Credit Challenge Problem: HuggleWorld

Up for a challenge? Here's a golden opportunity to put your buggle skills to the test. Design greetings cards and win a bag of baggles. Click here for more details.