![]() Graphic by Keith Ohlfs |
|
This problem set is intended to help you understand how Java works
and to give you practice using, designing, and writing methods. Task
1 is a pencil-and-paper problem. The code for tasks 2 and 3 are
available in the ps2_programs
folder in the cs111
download directory on nike.
Save the modified Writing2.java
and
RugWorld.java
files in the ps2_programs
folder. Submit the entire ps2_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
.
When submitting your hardcopies, we ask that you turn in only one package of materials. Please staple your files together with a cover page, and submit your hardcopy package by placing it in the box outside of Jennifer's office (E104, across from E101).
IMPORTANT NOTES:
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.
Before continuing with this problem, please study the conventions for drawing execution diagrams (i.e. your notes from lecture).
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 Object Land contains an instance of the
SwapWorld
class that has the reference label SW1.
Your assignment is to draw an Execution Diagram for the execution of
the statement
SW1.run()
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. The statement section should show the
result of evaluating all expressions. Each expression should be
replaced by a representation of its value.
SwapWorld
class, three instances of
the SwapBuggle
class, three instances of the
Point
class, and three instances of the
Color
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. You need not
show any Direction
instances in Object Land; use the
convention of representing such instances by special reference
labels like WEST
. Your diagram should show the
state of each object after the completion of the execution of the
run
method.
It is possible to use methods to expand the capabilities of the buggle name writing program from problem set 1. For example, using a larger grid, but fewer actual lines of code, a buggle can write the name "ELLIE" 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 which creates a larger grid and defines a LetterBuggle, a new class of objects which extends the Buggle class. A new LetterBuggle named ellie has also been created for you, along with two LetterBuggle methods; writeName, and writeE, as shown below:
// write the name "ELLIE" around the perimeter of the grid public void run () { LetterBuggle ellie = new LetterBuggle(); ellie.writeName(); // add your code here // statements which will write the name around the perimeter of the grid } class LetterBuggle extends Buggle { // write the name "ELLIE", in the appropriately colored letters, by setting the color, // and then by invoking methods for writing the individual letters public void writeName () { this.setColor(Color.red); this.writeE(); // write the first "E", in red // add your code here // statements which will write the "L L I" in the correct colors this.setColor(Color.green); this.writeE(); // write the last "E", in green } //write the letter "E" public void writeE() { this.brushDown(); this.forward(2); // add your code here // the rest of the statements for writing an "E" } // add your code here // methods for writing the other letters in the name }
Perform the following steps to solve the problem:
You must conform to the following rules:
The buggles from Problem Set 1, becky, bobby, bertie, billy and benny, 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 patterns:
![]() pattern 1 |
![]() pattern 2 |
![]() pattern 3 |
![]() pattern 4 |
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 the code which will 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:
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 }
Color
variables.
Name your variables 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 outerRing is 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.
Color
parameters. Follow the naming conventions as given for the
other methods.
Color
parameters.
Color
parameters.
The six methods above must meet the invariant that the buggle must start and finish in the same cell, facing the same direction. You must assume that the buggle starts in the bottom left corner of each pattern (as shown above) facing 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.paintCell(Color.gray);
paintCell
method.
//
(two
slashes) before that line of code.