![]() |
Problem Set 10 Due on Tuesday, December 11 at 6pm |
The purpose of this problem set is to give you experience with data abstraction, graphics, and animations. In Task 1, you will explore data abstraction by fleshing out a very different implementation of the simple Buggle class we studied in Lecture 19. In Task 2, you will create your own animation!
All code for this assignment is available
in the ps10_programs
folder in the cs111 download
directory on the CS fileserver.
On this problem set, you are 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 the problem set and the same grade will be given to both team members.
All work by a team must be a true collaboration in which members actively work together on all parts of the assignment. It is not acceptable for team members to split up the problems and work on them 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 this assignment
unless you can schedule at least 10 hours to work together.
To find a partner with a schedule similar to yours, feel free
to post a message on CS111-F07 Q&A
.
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.
Your hardcopy packet should consist of:
AbbyBuggle.java
from Task 1b.
.java
files you wrote or
modified from Task 2.
You need only submit one hardcopy per team. On your cover sheet, please indicate in whose drop folder the softcopy can be found.
ps10_programs
folder, which should include:
Buggle
subdirectory should
contain your final version of AbbyBuggle.java
.
MyAnimation
subdirectory should
contain all the necessary code to run your personal animation.
Abby Stracksen has been studying
SimpleBuggle.java
,
an implementation of a SimpleBuggle
class that is similar to the
Buggle
implementation discussed in
Lecture 19.
Instances of SimpleBuggle
are similar to the
Buggle
instances we have used all semester
in that they have four state components (position, heading,
color, and brushDown state), can move forward and backward,
and can turn left and right. However, they
can't drop or detect bagels, can't detect walls,
can't leave a trail, and can't paint the color of a cell.
The SimpleBuggle
class includes the following
main()
method that exercises all of the methods
in the class:
// This main method exercises all the methods of SimpleBuggle. public static void main (String[] args) { SimpleBuggle becky = new SimpleBuggle(); System.out.println("Initial state:\n" + becky); for (int i = 1; i <= 3; i++) { Location loc = becky.getPosition(); becky.setPosition(new Location(loc.y + 3, 2*loc.x + 4)); Direction d = becky.getHeading(); becky.setHeading(d.left()); Color c = becky.getColor(); becky.setColor(new Color(c.getBlue(), c.getGreen(), c.getRed()/2)); if (becky.isBrushDown()) becky.brushUp(); else becky.brushDown(); System.out.println("State at beginning of step " + i + ":\n" + becky); becky.forward(2*i); becky.left(); becky.backward(i); becky.left(); becky.forward(i); becky.right(); becky.backward(); System.out.println("State at end of step " + i + ":\n" + becky); } }
Invoking java SimpleBuggle
displays the following results:
Initial state: [SimpleBuggle: pos = Location(x=0,y=0); dir = EAST; col = java.awt.Color[r=255,g=0,b=0]; brushDown = true] State at beginning of step 1: [SimpleBuggle: pos = Location(x=3,y=4); dir = NORTH; col = java.awt.Color[r=0,g=0,b=127]; brushDown = false] State at end of step 1: [SimpleBuggle: pos = Location(x=5,y=5); dir = WEST; col = java.awt.Color[r=0,g=0,b=127]; brushDown = false] State at beginning of step 2: [SimpleBuggle: pos = Location(x=8,y=14); dir = SOUTH; col = java.awt.Color[r=127,g=0,b=0]; brushDown = true] State at end of step 2: [SimpleBuggle: pos = Location(x=5,y=12); dir = EAST; col = java.awt.Color[r=127,g=0,b=0]; brushDown = true] State at beginning of step 3: [SimpleBuggle: pos = Location(x=15,y=14); dir = NORTH; col = java.awt.Color[r=0,g=0,b=63]; brushDown = false] State at end of step 3: [SimpleBuggle: pos = Location(x=19,y=17); dir = WEST; col = java.awt.Color[r=0,g=0,b=63]; brushDown = false]
Abby decides to experiment with a different implementation of
SimpleBuggle
that she calls AbbyBuggle
.
Instances of the AbbyBuggle
.class behave exactly
like instances of the SimpleBuggle
.class except that
the toString()
method displays AbbyBuggle:
instead of SimpleBuggle:
. In particular, invoking
java AbbyBuggle
should display the same results as above,
except that every occurrence of SimpleBuggle:
should be replaced by AbbyBuggle:
.
The interesting thing about Abby's AbbyBuggle
class
is that even though it behaves like SimpleBuggle
,
the representations it uses internally are
dramatically different from those used by SimpleBuggle
.
Here are the instance variables of the AbbyBuggle
class:
// Instance variables of the AbbyBuggle class: private int[] pos; // position of buggle: a 2-element array of {x,y} private int[] dir; // heading of buggle: a 2-element array of {delta-x,delta-y} private int col; // color of buggle: an integer representation of the color private int brushDown; // brush state of buggle: 0 = false, 1 = true
The instance variables of AbbyBuggle
do not
contain any Location
instances,
Direction
instances,
Color
instances,
or boolean
values.
Instead, the instance variables of AbbyBuggle
contain only integers or arrays of integers.
An AbbyBuggle
instance has four instance variables:
pos
is an array of two integers representing the
position of the buggle:
pos[0]
is the x-coordinate of the position;
pos[1]
is the y-coordinate of the position.
dir
is an array of two integers representing the
heading of the buggle. This array should have one of only four
possible values:{1,0}
represents EAST
;
{0,1}
represents NORTH
;
{-1,0}
represents WEST
;
{0,-1}
represents SOUTH
.
col
is the integer representation of a color
used in the Pic
class in PS9. This can be determined
by invoking the getRGB()
method on a Color
instance. For example:Color.red.getRGB()
,
which is -65536
.
Color.green.getRGB()
,
which is -16711936
.
Color.blue.getRGB()
,
which is -16776961
.Color
instance using the
public Color(int rgb)
constructor method.
For example, new Color(-65536)
returns
a Color
instance equivalent to Color.red
.)
brushDown
is an integer representing whether or not
the brush is down: 1
is used to indicate that tbe brush is down.
0
is used to indicate that tbe brush is up.
Although unusual, Abby's representations have some benefits.
For example, her representations of positions and headings
makes it very easy to implement the forward(n)
method:
public void forward (int n) { pos[0] += n*dir[0]; // Recall that this means: pos[0] = pos[0] + n*dir[0]; pos[1] += n*dir[1]; }
Using a little vector geometry, Abby is able to develop an efficient
way to turn left and right. Given a direction array (x,y)
,
the direction that results from turning left is (-y,x)
,
and the direction that results from turning right is (y,-x)
.
Below is Abby's left()
method; right()
is similar.
// To rotate left by 90 degrees, new_x = -y; new_y = x. public void left() { int new_x = -dir[1]; int new_y = dir[0]; dir[0] = new_x; dir[1] = new_y; }
Abby's AbbyBuggle
class illustrates data abstraction:
two classes (in this case, SimpleBuggle
and AbbyBuggle
)
can exhibit the same abstract external behavior even though their concrete internal
representations are radically different. In more advanced programming
(such as in CS230 Data Structures), the principle of data abstraction is
frequently used by the implementers of a class to make the implementation of a class
more efficient while still maintaining the same contract with the clients of the class.
AbbyBuggle
class
that includes complete declarations of the instance variables
and the forward(int n)
, left()
,
toString()
,and main()
methods.
This skeleton may be found in the file
AbbyBuggle.java
within the Buggle
subdirectory of ps10_programs
.
However, before she can finish the implementation,
Abby is hired by Facebook as a consultant for their troubled Beacon project,
and must focus all of her energy on this new project.
Based on the recommendation of your CS111 instructors,
Abby hires you to complete the implementation of her
AbbyBuggle
skeleton.
You must complete the following subtasks:
Task 1a:
An object diagram depicts the state of an object in ObjectLand, using the conventions we have used throughout the semester.
Location
instance would be a big box labeled Location
containing smaller boxes labeled x
and y
for its two instance
variables.
Location
s
would be drawn as a big box labeled Location[]
containing
boxes labeled 0
through 4
, each of which contains
a pointer to a Location
instance (or null
).
In this subtask, you are to draw two object diagrams for the
AbbyBuggle
instance named becky
created in the main()
method of the AbbyBuggle
class:
becky
right before the for
loop is entered. This corresponds
to the state that is displayed immediately after the phrase Initial state:
.
becky
right after the for
loop is exited. This corresponds
to the state that is displayed immediately after the phrase State at end of step 3:
.
In both cases, your object diagram should show the actual instance
variables used by AbbyBuggle
objects -- i.e., integers and
arrays of integers, not Location
s, Direction
s,
Color
s, or boolean
s.
Task 2a:
Flesh out the method declarations of the following methods within
AbbyBuggle.java
to complete the implementation
of the AbbyBuggle
class:
// Constructor method: public AbbyBuggle (); // Instance methods: public Location getPosition(); public void setPosition (Location loc); public Direction getHeading(); public void setHeading (Direction d); public Color getColor(); public void setColor (Color c); public boolean isBrushDown(); public void brushDown(); public void brushUp(); public void right(); public void forward(); public void backward(); public void backward (int n);
Each of your method bodies should be only a few lines long. You are not allowed to modify the existing instance variables or add new ones.
You can test your fleshed out implementation of AbbyBuggle
by executing java AbbyBuggle
in the Dr. Java Interaction Pane.
This should display the same results as
java SimpleBuggle
presented above, except that
every occurrence of SimpleBuggle:
should be
replace by AbbyBuggle:
.
You can use AnimationWorld
to create dazzling
animations that show off your artistry and your programming talents.
Here is a chance to be creative!
For this task, build an animation of your own design. You may use existing sprites that we have studied, but you should create at least one new kind of sprite from scratch and use it. Of course, you are welcome to make as many new sprites as you want.
The MyAnimation
subdirectory of the
ps10_programs
directory contains a copy of the sprites
and animations shown in Lecture 23.
You can run the Lecture 23 animations by
executing java LectureShowcase
.
This is a good starting point for
your own animations. You may edit the existing files or create ones
of your own.
Add any animations you create to
MyShowcase.java
.
Execute java MyShowcase
to create the animation showcase
for viewing your animation.
Alternatively, you can run your animation in a web browser
by browsing the file MyShowcase.html
.
Visit the animation gallery in the CS111 Museum for examples of past student animations. We will add your animations to this gallery so that everyone can view them!
You should design your animation so that it can be reset properly.
That is, pressing the Reset button should start the animation
from its initial state. You can do this by defining the resetState()
method for each of your sprites appropriately.
Your animations will be evaluated based on the following criteria: creativity, artistry, level of mastery exhibited for graphics and animation design and implementation, and code clarity.
If you need help on any aspect of this challenge, do not hesitate to talk to the instructors and/or TAs.