[CS111 Home Page]
[Syllabus]
[Assignments]
[Documentation]
[FAQ]
[CS Dept.]
[CWIS]
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. To get the code
for this assignment, connect to nike via Fetch and download
the folder ps2_programs from the directory
/usr/users/cs111/download.
How to turn in this Problem Set
Submit the entire ps2_programs
folder to your drop folder on the cs111 server. Before submitting your work, make sure
that all the files have been saved and the project compiles and runs as it should. After
submitting your work, make sure to doublecheck that all the files have been uploaded correctly.
If you need directions on how to submit your work or how to check whether the submission was
successful, please click here. Please
make sure to keep a copy of your work, either on a zip disk, or in your private directory
(or, to play it safe, both).
Task 1: Buggle Name Writing using Methods
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 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 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 writeJ, as shown below:
// write the word "JAVA" around the perimeter of the grid
public void run () {
LetterBuggle ellie = new LetterBuggle();
ellie.writeName();
// add your code here
// statements which will write the word "JAVA" around the perimeter of the grid
}
class LetterBuggle extends Buggle {
// write the word "JAVA", in the appropriately colored letters, by setting the color,
// and then by invoking methods for writing the individual letters
public void writeName () {
// fill in with statements which will position this Buggle correctly to start writing
this.setColor(Color.blue);
this.writeJ(); // write the first "J", in blue
// add your code here
// statements which will write the "A V A" in the correct colors
}
//write the letter "J"
public void writeJ() {
this.left(); // face NORTH
this.forward(); // draw hook in lower left of "J"
// add your code here
// the rest of the statements for writing a "J"
}
// add your code here
// methods for writing the other letters in the word
}
Perform the following steps to solve the problem:
-
Add the rest of the code to the writeJ method so that it draws the letter
"J". This code should be very similar to the code you used in problem set 1 for
that purpose! 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).
-
Define similar methods 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.
-
Add code to the writeName method so that it draws the word "JAVA" by setting
the color and then invoking the methods which write the individual letters.
-
Add code to the 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 come up with a way to turn and change your heading at each
corner of the grid.
You must conform to the following rules:
-
You may not invoke the "setPosition" or "setHeading" methods.
-
You should use the methods "brushUp" and "brushDown" where appropriate.
-
Each letter except "V" should fit in a 4x5 grid, and should be separated
from the next letter by one blank space.
-
The letter "V should occupy a 3x5 grid as shown.
-
Ellie must draw the letters in the appropriate colors (use "setColor" to
change colors).
-
Ellie must end up in the position shown above, facing the correct direction
(EAST).
-
Use methods to solve the problem.
Task 2: The Buggle Bagel Ruggle Company
Important note: Before you begin working the second task, make sure to close
the project that you were using for the first task by choosing Close from the
File menu to close . Then double-click on the RugWorld.mcp icon to open
the second project.
Alternatively, to be on the safe side (i.e. to make sure that you are not running the
code from the previous task instead of the new one), you might want to quit CodeWarrior
entirely and reopen it for the new project. To do this
- quit CodeWarrior (File -> Quit),
- quit the Applet Runner
by selecting Apple Applet Runner from the menu of active applications in the upper right corner
of the screen, then choosing Quit from the File menu,
- double-click on the RugWorld.mcp icon to open the second project
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 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 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:
-
You can not modify any of the code in the run method.
-
You should use a single RugBuggle for the entire rug (weaver, who
has already been created in the file).
-
You may not, and do not need to, use the setPosition() or setHeading()
methods. You can use any of the other Buggle methods.
-
You should draw the colors and bagels exactly as shown in the rug.
-
The buggle must finish in the same place it started, facing the same direction
(i.e. EAST).
-
You must create the rug pattern by using the 4 3x3 patterns shown above.
-
The buggle must start in the bottom left corner of each pattern (as shown
above) facing EAST. The buggle must finish in position to start the next
pattern element as shown below:

-
You must name the method that generates each of the four patterns above
as 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
}
You must also define the following methods:
-
makeRug: this method creates the entire rug; it has no parameters.
-
outerRing: this method draws the outer ring; it has no parameters.
It should have two 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:

-
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.
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:
-
The 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);
will paint the cell under sara gray. sara's brush state is not changed
by the invocation of the paintCell method.
-
The RugBuggle's brush is initially up before making the rug.
-
You can temporarily modify the 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.
-
The row patterns are created by alternating patterns 3 and 4 like so (pattern3--pattern4--pattern3):


-
You should feel free to add as many more methods as you see fit. There
are many more opportunities for creating methods in this problem.