Due Tuesday Feb 24.
Reading: Finish Ch 3 in the Objects
First
text and start Chapter 4. Work along
with the book, using BlueJ.
Prepare: Play Oscar van Deventer's Jukebox game,
starting at level 1: http://www.clickmazes.com/ovd/ixojuke.htm
Think about the objects and behaviors you will implement in java. In
future weeks, we will make this fancier.
Lab: In the lab on wednesday
morning Feb 18, 2009, write and debug code for the following
simplified version with
only one switch and pockets of size 1, as discussed in class. You
should be able to insert red or blue balls and have them end up in one
of two buckets.
HW: Pack your project folder
as a jar file and email it as
an attachment to the TA by the start of class tuesday Feb 24.
- Ball a ball object. It has a color
- Bucket to collect balls at the bottom of the game. It has
a preferred color and keeps track of how many balls of the correct and
incorrect color it has received.
- Tube passes balls down to a bucket or switch
- Switch it is in one of two states, left/right and
contains two pockets.
- Pocket It holds balls. It has a size, but for now the
size can be just 1. We'll generalize that in a later week, but include
an isFull() accessor now.
- Game It creates buckets, tubes, and switches
appropriately connected, and has an insertRed() and insertBlue()
method which create a ball.
Notes:
- When a ball is created, specify its color in the constructor.
Make the colors be strings, e.g., "red" and "blue". You'll need an
accessor for the color.
- There should be a receive(ball) method for buckets,
tubes, switches, and pockets, to accept a ball.
- When a bucket is created, specify what color ball it expects in
the constructor. When it receives a ball, it should count how many
total it contains of the correct and incorrect colors. There should be
accessors for both those counts. To compare Strings, use the built-in
equals(...)
method,
-
if (color1.equals(color2)) ...
- When a tube is created, we want to specify its outlet in the
constructor. The outlet might be either a bucket or a switch. After we
discuss inheritance, we'll know how to do this by passing a
single parameter in a clean way. For now, give each tube two
parameters: a bucket outlet and a switch outlet, but specify one as
null
when you create it. When a tube receives a ball, it passes it on to the
outlet which is not null. You can say things like this:
-
if (outletBucket != null) outletBucket.receive(ball);
- When a pocket is constructed, specify a size, and an outletTube.
Only use and worry about a size of 1 for now. We'll generalize to
larger pockets next week. When a pocket receives a ball, it stores it
for later. Pockets should have an isFull() method which
returns a boolean. They also have an empty() method, which
causes it to empty its stored ball in the outlet tube.
- When a switch is constructed, specify the size of its left and
right pockets and the outlets of the left and right pockets. The
constructor will construct the two pockets and tell each pocket its
size and outlet. Write a toggle() method which flips the state
to the opposite state. When a switch receives a ball, the state
determines which pocket receives the ball. Then ask the pocket if it is
full; if so, toggle the state and tell the full pocket to empty itself.
- The game will eventually contain four switchs but for this week
one working switch with pockets of size 1 is enough. There should be a
top tube to feed the switch and two outlet tubes connecting the switch
to
left
and right buckets. When you construct each object, you need to tell it
its outlet(s), so they must already exist; this means you must
construct the objects from the bottom up, first the buckets and lastly
the top tube. Besides the constructor, the game needs only two
methods for now, insertRed() and insertBlue(), which
put a ball in the top tube. Then it prints a line to the
console saying how many correct
and incorrect balls are in the buckets.
- For now, no mutators are needed and the only accessors required
are for a ball's color, a pocket's fullness, and the buckets' counts.
- Include a line of comment before each class and before each
method explaining its purpose.