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
- Notes on JEMs
- YouTube video: Introduction to Pair Programming
- Article on pair programming: All I Really Need to Know about Pair Programming I Learned in Kindergarten
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:
- Pay careful attention to upper and lower case letters in the filenames.
- 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 inFetch
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 of0K
. If the file isn't listed or if the size for the document is0K
, 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. - 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:
- Rigorously apply the evaluation rules for assignments, declarations, and instance method invocations.
- You should draw only a single diagram that summarizes the final result of completely executing the above method. Although in practice execution frames are removed and reclaimed when their last statement is done executing, you should not remove any execution frames from your diagram once you have drawn them.
- Your Execution Land should show all non-black-box execution
frames created during the execution. There should be exactly three
such frames: one for the invocation of
run()
and one for each of the two invocations ofswap()
. Each execution frame should have two parts: (1) a variables section and (2) a statement section. Each variable section should include: (1) athis
variable; (2) any parameters declared by the invoked method; and (3) any local variables declared within the body of the invoked method. - Your Object Land should contain: an instance of the
SwapWorld
class, three instances of theSwapBuggle
class, and exactly six instances of theLocation
class (three from the three calls tonew swapBuggle()
and three from the three explicit calls tonew Location()
). Label each of your objects in Object Land with a unique reference label (e.g., B1, B2, B3 for the buggles; L1, L2 for the locations, etc). It is a bad idea to use reference labels which are the same as any variable names in your code. Use these labels in place of pointers. Your diagram should show the state of each object after the completion of therun()
method. - Draw your diagram using pencil and paper. Pens make errors difficult to fix.
- Please try to make your diagram as neat as possible. It's usually a good idea to re-draw it after you've figured out what happens.
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:
Perform the following steps to solve the problem: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. }
-
Add the rest of the code to the
writeJ()
method so that it draws the letter "J". Assume that when you start to write a letter (other than"V"), the buggle is in the lower left square of a 4 x 5 grid, facing the direction you are writing in. At the end of the method, make sure that the buggle is placed in the correct position and direction to start the next letter (at the lower left square of the next 4 x 5 grid, separated by one space from the previous letter, facing the direction of writing). - Note that the
writeJ()
method takes aColor
parameterjcolor
. Use this parameter inside the body ofwriteJ()
to set the color of the buggle appropriately. -
Define similar methods (each also with a single
Color
parameter) to write an "A" and a "V". Note that "V" occupies a 3 x 5 grid instead of the 4 x 5 grid occupied by each of the other letters. - Add code to the
writeName()
method so that it draws the word "JAVA" by invoking the methods that write the individual letters in the appropriate color. - Add code to the
run()
method so that it draws the word 4 times around the perimeter of the grid, by invoking thewriteName()
method repeatedly. You will need to make sure that you invokewriteName()
with the correct color parameter in each instance in order to match the picture above. You will need to come up with a way to turn and change your heading at each corner of the grid.
- You may not invoke the
setPosition()
orsetHeading()
methods. - You should use the methods
brushUp()
andbrushDown()
where appropriate. - Each letter except "V" should fit in a 4 x 5 grid, and should be separated from the next letter by one blank space.
- The letter "V should occupy a 3 x 5 grid as shown.
- Ellie must draw the letters in the appropriate colors. All the
colors used in the picture above are
Color
constants from theColor
class. These constants are only used in therun()
method. They may not be used anywhere else in your code. - Ellie must end up in the position shown above, facing the correct
direction (
EAST
). - 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:
- Top-down approach: Start by identifying some big repeating patterns that make up the rug. You want to write a method to produce each one of those big patterns. As you concentrate on each one of them, try to see if you can identify smaller patterns that make up the bigger pattern. If so, you should write a method to draw each of the smaller patterns, and then invoke those methods to produce the big pattern. This process can be repeated several times.
- Bottom-up approach: Alternatively, you can start by identifying very small patterns that can be put together to produce larger patterns, which in turn can be put together to produce even larger patterns, until the whole rug is produced.
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:
- Do not modify the
RugWorld
class. - Complete the definition of the
RugBuggle
class by (1) defining themakeRug()
method; and (2) defining all the methods needed to produce each of the repeated patterns. - Your
makeRug()
method must draw the colors and bagels exactly as shown in the above rug picture.
Helpful Hints:
-
The
Buggle
methodsetCellColor(Color c)
can be used to paint the cell under the buggle a certain color without having to move the buggle. For example,sara.setCellColor(Color.gray);
sara
. The buggle's brush state is not changed by the invocation of thesetCellColor()
method. -
You can and should temporarily modify the
run()
method to test out small parts of your code. Comment out the invocation ofmakeRug()
and replace it by the code you want to test. Remember that you can comment out a line of code by adding//
(two slashes) before that line of code. (DrJava's edit menu has comment lines and uncomment lines commands.) - You should feel free to add as many methods as you see fit. There are many opportunities for creating methods and capturing patterns in this problem.