CS111 Spring 2010

Problem Set 2

Due Tuesday 09 February 2010

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. There is a paper-and-pencil problem and two programming problems.

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

Reading

Working In Pairs

On Task 3 (and only Task 3), you are strongly encouraged (but not required) to work with a partner as part of a two-person team. If you work on a team, your team will submit a single softcopy and hardcopy of your team-solution to Task 3 and the same grade will be given to both team members for Task 3.

All work by a team must be a true collaboration in which members actively work together on all parts of the Task. It is not acceptable for team members to split up the Task and work on parts independently. All programming should be done with both team members working at the same computer console. It is strongly recommended that both team members share the responsibility of "driving" (typing at the keyboard), swapping every so often. The only work that you may do alone is debugging code that you have written together and talking with the instructors and drop-in tutors.

There are many advantages to programming in pairs. People who program in pairs often claim to take less time than those who program alone. By continuously reviewing the code they find bugs sooner. Catching more bugs also leads to higher-quality code. When it comes to problem solving, two heads are better than one, and less time is spent exploring blind alleys. It can be a better learning experience, since team members both learn from and teach each other. And pair programmers often report that the experience is more enjoyable than programming alone. Many empirical studies have confirmed these and other benefits of pair programming. For example, see The Costs and Benefits of Pair Programming and other publications by Laurie Williams.

It's only fair to note that there are drawbacks to programming in pairs as well. Some pairs take longer to complete a program than they would individually. A mismatch in the skill level or working style of pair members can lead to friction between the individuals and disrupt the work. At Wellesley, the most common problem in pair programming is finding enough time in common to work together. You should not choose a partner to program with on Task 3 unless you can schedule at least a few hours to work together. To find a partner with a schedule similar to yours, feel free to post a message on the course Q&A conference.

Although you clearly can share Java code with your team partner on this assignment, other aspects of the course collaboration policy still hold. In particular, while you can talk with other individuals and teams about high-level problem-solving strategies, you cannot share any Java code with them.

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.

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 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 during her/his office hours. This week, you must meet another one of us. Go to another 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 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 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:


public class Writing2 extends BuggleWorld
{
        public void setup ()
        {
                setDimensions(25, 25); 
        }

        // write the word "JAVA" around the perimeter of the grid
        public void run ()
        {
                // ellie will perform all writing. 
                LetterBuggle ellie = new LetterBuggle(); 

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

                // Statements that write the other three occurrences of
                // "JAVA" and return ellie to her initial state go here.
    
        }

        // ------------------------------------------------------------------
        // Enables this applet to run as an application

        public static void main (String [] args)
        {
                runAsApplication(new Writing2(), "Writing2");
        }
}


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. 
        public void writeName (Color c)
        {

                // Add code here to position this buggle correctly to 
                // start writing.
 
                this.writeJ(c);

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

        }

        // Write the letter "J" in the given color and 
        // position buggle to write the next letter. 
        public void writeJ (Color jcolor)
        {
      
                // Statements to implement method go here.
  
        }
                     
        // 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: 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 4 x 5 grid, and should be separated from the next letter by one blank space.
  4. The letter "V should occupy a 3 x 5 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

For this task (and only this task), you are strongly encouraged (but not required) to work with a partner as part of a two-person team. If you work on a team, your team will submit a single softcopy and hardcopy of your team-solution to the task and the same grade will be given to both team members for the task. Please see the top of this web page for details about pair programming.

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: The design shown above can be produced by using some repeated patterns.

Your task is to identify those repeating patterns, write code that draws them, and eventually draw the whole rug. Because of the repeating patterns, there are many opportunities for using methods to simplify the drawing of the whole rug pattern.

Getting your analysis started:
Here are a couple of ways to start thinking about this problem:

In practice, most people employ a combination of the two approaches above when solving a complicated problem.

Your goal is to produce the above rug and to do it with code that is easy to read and understand. You will be graded on the extent to which you avoid a lot of repetition in your code by capturing patterns.

You must define at least 5 methods, in addition to makeRug(). You can define more methods if that helps clarify your code. Of course, some of your defined methods will invoke other methods.

Getting your coding started: 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 complete. 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.makeRug(Color.green, Color.yellow, Color.cyan, Color.pink);
}

You must observe the following guidelines:

Helpful Hints: