![]() |
Problem Set 2
Due Date Extended By One Week! |
To get the code for this assignment, connect to the cs111
download directory via Fetch
and download the folder
ps02_programs
.
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:
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.
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.
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.
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:
run()
and one
for each of the two invocations of swap()
. Each
execution frame should have two parts: (1) a variables section and
(2) a statement section. Each variable section should include: (1)
a this
variable; (2) any parameters declared by the
invoked method; and (3) any local variables declared within the
body of the invoked method.
SwapWorld
class, three instances of the
SwapBuggle
class, and exactly six instances of the
Location
class (three from the three calls to
new swapBuggle()
and three from the
three explicit calls to new 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 the
run()
method.
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.
Perform the following steps to solve the problem:
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. }
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 4x5 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 4x5 grid, separated
by one space from the previous letter, facing the direction of
writing).writeJ(Color jcolor)
method takes a
Color
parameter jcolor
. Use this parameter
inside the body of writeJ()
to set the color of the
buggle appropriately.
Color
parameter) to write an "A" and a "V". Note that "V" occupies a 3x5
grid instead of the 4x5 grid occupied by each of the other
letters.writeName()
method so that it draws
the word "JAVA" by invoking the methods that write the individual
letters and by using its three color parameters correctly.run()
method so that it draws the
word "JAVA" 4 times around the perimeter of the grid, by invoking the
writeName()
method repeatedly. You will need to make
sure that you invoke writeName()
with the correct 3 color
parameters in the right order to match the given picture (above). You
will need to come up with a way to turn the buggle at each corner of the
grid. At the end of the run()
method, ellie
must have the same position and heading as when she was 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:
RugWorld
class.
RugBuggle
class
by (1) fleshing out the provided skeleton for the makeRug()
method; (2) defining all the methods described below; and
(3) defining any additional methods you think are helpful.
setPosition()
or setHeading()
methods. You
can use any of the other Buggle
methods.makeRug()
method must draw the colors and bagels
exactly as shown in the above rug picture.makeRug()
method is invoked on a buggle,
its final position and heading must be the same as its initial position and heading.
|
|
pattern1()
, pattern2()
,
pattern3()
, and pattern4()
, respectively. Each
pattern should take two Color
parameters.
For example,
the skeleton for the pattern1()
method should look like
this:public void pattern1 (Color c1, Color c2) { // Write statements to draw pattern1 here. }
This skeleton happens to use the parameter names c1
and c2
, but you can use whatever parameter names you like.
outerRing()
: this method draws the outer ring.
It should have two Color
parameters.
The first color parameter represents the outer color of the border (blue
in the rug you are drawing) and the second color parameter represents the
inner color of the border (red
in the rug you are drawing). A snapshot of the rug after invoking
outerRing(Color.blue, Color.red)
is shown below:
innerPattern()
: this method draws the inner pattern
shown below that is repeated twice in the rug:
This method takes two Color
parameters
that determine the colors used in the pattern.
For example, the above picture was created by the invocation
innerPattern(Color.black, Color.yellow)
.
Note that the actual rug uses the
colors cyan and pink in one instantiation of the pattern and green and
orange in the other instantiation of the pattern.
innerRing()
: this method draws the border of the
inner pattern. This method should also have two Color
parameters.
innerRing(Color.black, Color.yellow)
should draw
the following pattern:
innerRow()
: this method draws the pattern in the center of the inner pattern.
This method should have two Color parameters.
For example, innerRow(Color.black, Color.yellow)
should draw
the following pattern:
centerRow()
: this method draws the pattern in the center of the rug.
This method should also have two Color
parameters.makeRug()
, the five methods above must meet
the invariant that the buggle must start and finish in the same cell,
facing the same direction. You should assume that the buggle starts in the
bottom left corner of each pattern (as drawn above) facing rightward
and should finish that way.
Buggle
method setCellColor(Color c)
can be used to paint the cell under
the buggle a certain color without having to move the buggle. For example,sara
gray. sara
's brush state is not changed
by the invocation of the setCellColor()
method.
weaver
's brush is initially up before making the rug.run()
method to test out small parts
of your code. Comment out the invocation of makeRug()
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.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.