CS111, Wellesley College, Fall 2007

Problem Set 2

Due Date Extended By One Week!
Now due Tuesday September 25 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 optional, 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

Last week you met one of the instructors (Stella or Brian or Lyn) during her/his office hours. This week, you must meet a different one of us! Go to the a different instructor's office hours and secure a sticker (you cannot see the same person you saw last week!). If you cannot make office hours, you must schedule an appointment. Place the sticker on the problem set cover page. Now you have met at least two of us!

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 task, 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: Drawing execution diagrams will help you understand 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();
        Location lcn1 = new Location(6,3);
        Location lcn2 = new Location(4,5);
        bg1.setPosition(lcn1);
        bg2.setPosition(lcn2);
        bg3.setPosition(new Location(lcn1.x - lcn2.x, lcn1.y + lcn2.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) {
        Location lcn1 = this.getPosition();
        Location lcn2 = bg1.getPosition();
        Color c1 = this.getColor();
        Color c2 = bg1.getColor();
        this.setPosition(lcn2);
        bg1.setPosition(lcn1);
        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.

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

This task uses methods to expand the capabilities of the buggle word writing program from problem set 1. Using a larger grid, but fewer 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 picture. The file Writing2.java contains a BuggleWorld subclass named Writing2 that creates a 25x25 grid and has the skeleton of a run() method that draws the picture. This file also contains a Buggle subclass named LetterBuggle for letter-drawing buggles. The run() method of the Writing2 class creates a new LetterBuggle named ellie whose job is to draw the above picture using methods from the LetterBuggle class. Skeletons of two such methods, writeName() and writeJ(), have been provided for you.


class Writing2 extends BuggleWorld {


    // write the word "JAVA" around the perimeter of the grid
    public void run () {

        // Create LetterBuggle ellie, who will perform all writing. 
        LetterBuggle ellie = new LetterBuggle(); 

	// Write "JAVA" at the bottom of the picture
        ellie.writeName(Color.red, Color.blue, Color.yellow);

        // Flesh out statements that write the other three occurrences
        // of "JAVA" and return ellie to her initial state. 

    }

    // Some other methods of the Writing2 class not shown here. 

}


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) {

        // Add code here to position this buggle correctly to start writing.
 
        this.writeJ(c1);  // write the "J" in color c1

        // Add statements here to write the "A V A" in the correct colors.

    }

    // Write the letter "J" in color jcolor and position buggle to write the next letter. 
    public void writeJ (Color jcolor) {
    
        // Flesh out the statements in the body of this method. 
  
    }
                     
    // Below, define methods for writing the letters "A" and "V",
    // as well as any other methods you find helpful. 
                     
}

Perform the following steps 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 (blithe, cy, maggie, rex and yelena) 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/color patterns:

pattern1(Color.black, Color.yellow)

pattern2(Color.black, Color.yellow)

pattern3(Color.black, Color.yellow)

pattern4(Color.black, Color.yellow)

Each of these 4 patterns is parameterized by two colors that allow different colors to be used in different situations.

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:

Your task is to write code that draws the rug pattern shown above. This rug pattern is composed out of many repeated subpatterns. This means that there are many opportunities for using methods to simplify the drawing of the rug pattern. Your goal is to describe as many subpatterns as you can using methods to avoid writing statements for the same subpattern twice.

The code for this problem is contained in the RugWorld folder. You will be editing the file RugWorld.java. This file contains the complete definition of a BuggleWorld subclass named RugWorld (which you should not change) and the skeleton of a Buggle subclass named RugBuggle that you must flesh out. The run() method of the RugWorld class creates a single RugBuggle named weaver that draws the entire rug via the makeRug() method in the RugBuggle class:


  public void run() {
    RugBuggle weaver = new RugBuggle();
    weaver.brushUp();
    weaver.makeRug();
  }

Note that weaver's brush is up when it starts drawing the rug.

You must observe the following guidelines to complete this task:

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 bagels. Click here for more details.