![]() |
Problem Set 2 Due: Friday 22 September at the start of class |
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.
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.
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.
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:
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 quite a few instances of the
Point
class. Label each of your objects in Object
Land with a unique reference label (e.g., B1, B2, B3 for the
buggles; P1, P2 for the points, 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 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:// 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 }
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.
writeName()
method so that it draws
the word "JAVA" by invoking the methods that write the individual
letters and by using the three color parameters correctly.run()
method so that it draws the
word 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 and change your heading at
each corner of the grid.setPosition()
or setHeading()
methods.brushUp()
and
brushDown()
where appropriate.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.
EAST
).
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:
run()
method.RugBuggle
for the entire rug
(weaver
, who has already been created in the file).setPosition()
or setHeading()
methods. You
can use any of the other Buggle
methods.EAST
).EAST
. The buggle must finish in
position to start the next pattern element as shown below:
pattern1
, pattern2
,
pattern3
, and pattern4
, respectively. Each
pattern should take two Color
parameters. The first
color is the color represented in black, and the second color
is the color represented in yellow in the above pictures. Name
your parameters c1
and c2
. For example, the
skeleton for the pattern1
method should look like
this:public void pattern1 (Color c1, Color c2) { // add code here }
makeRug()
: this method creates the entire rug; it has no parameters.outerRing()
: this method draws the outer ring.
It should have two Color
parameters named c1
and c2
. c1
represents the outer color of the border (blue
in this rug) and c2
represents the inner color of the border (red
in this rug). A snapshot of the rug after running outerRing()
is shown below:
innerPattern()
: this method draws the inner pattern
which is repeated twice in the rug as shown below:
This method should take two Color
parameters named
c1
and c2
. c1
will represent
the areas colored in black above and c2
will represent
the areas colored in yellow above. 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. Follow the naming conventions as given for the other
methods.
innerRow()
: this method draws the pattern in the center of the innerPattern.
This method should have two Color parameters.centerRow()
: this method draws the pattern in the center of the rug.
This method should also have two Color
parameters.EAST
and should finish that way.
Helpful Hints:
paintCell(Color c)
method 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 paintCell()
method.
run()
method to test out small parts
of your code. Comment out the invocation of makeRug()
and add what
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 baggles. Click here for more details.