Log in
Start Eclipse
Get project skeleton
Retrieve the Lab5 project skeleton from the LabSkeletons
respository. When you do this, you will notice lots of errors in the
code (i.e. lots of red "X"s). Don't panic - this is actually exactly
right. The classes we are providing to you refer to the classes which
you haven't yet written, which is why the compiler is unhappy. As you
write the missing classes, the errors will go away.
Disconnect the project from the repository
Make sure you disconnect your local copy of the Lab 5 project skeleton
from the repository.
Look at what you have...
In the Lab5 project you will see two packages:
lab5 and lab5support. You do not need to
worry about the code in the lab5support package.
In the lab5 package there is one class,
Driver, which is essentially empty. You must complete
the definition of this class (which includes inserting proper
javadocs) so that it creates a flock and a swarm, creates some buttons
in the graphical user interface (the button classes have already been
defined - you simply need to instantiate them), and hooks the flock
and swarm up to the appropriate buttons.
...think ahead...
The basic design you want implement is shown in class diagram. I
recommend you open this in a new tab. If you're using
Netspape or Firefox, right click on the
link, and select "Open link in new tab".
Class diagram
Now, before you feel that this is overwhelming, remember how we
deal with complexity: we deal with it one small step at a
time. We'll get you started in this lab description, and
your lab TAs will help you out along the way too.
Step 1 - Start a new UML diagram
For this lab, let us use Green to build as much of the project as
possible. To start, right click on the lab5.Driver in
the package explorer, and select "Green UML"->"Add to New UML
Diagram". Once lab5Driver is open in the diagram, move
it to the center of the diagram (click, drag, and drop).
Step 2 - Create a Window
The Driver class is the part of our overall system that
takes reposibility for creating all the (major) components of the
system, and making sure they are related in the right ways so they can
communicate with each other.
The first thing we want to ensure is that the Driver
class creates an instance of the lab5lib.Window class.
This points to a dependency relationship between the
Driver class and the lab5lib.Window class
(we'll make it a local variable dependency). To draw this
relationship in the diagram you will first need to open
lab5lib.Window in the diagram. To do this, make sure the
lab5.Driver class box is not selected.
When a class box is selected, its name compartment is highlighted in
green. If it isn't selected, its name compartment is the same tan
color as the rest of the box's compartments.
Now right-click on an empty part of the diagram canvas, and select
"Open Type in Editor". This brings up a winow; in the "Select
entries" box type "lab5lib.Window" and click "OK". A classbox for
lab5lib.Window should appear.
Draw a local variable dependency) between the
Driver class and the lab5lib.Window class.
The cardinality should be 1 and the relationship should be created in
the constructor of Driver.
Note: Green seems to be a little cranky here. When I draw the
relationship the correct code is generated, but the relationship line
does not show up. If this is the case for you, don't worry, but do
make sure that the correct code is generated in the
Driver class.
Step 3 - Create a Swarm
Use Green to create a new class. The class we'll create first is the
Swarm class. Select the "Class" icon in Green's tool
palette. Click somewhere in the diagram. This will bring up a window
which will prompt you for the following information (some may be
filled in for you already):
Source folder: Lab5
Package: lab5
Name: Swarm
This Swarm class is very important, because it is going
to represent a group of Bees. For now, let us have the
Driver create a Swarm. Draw a local variable
dependency) between the Driver class and the
Swarm class. The cardinality should be 1 and the
relationship should be created in the constructor of
Driver.
Don't forget to write javadocs for classes and methods
as you go along! It is much easier to write the javadocs as you're
writing the code, rather than as an afterthough. Get into the habit
of writing javadocs as you write the code. In fact, you can write
javadocs for classes and methods before you complete their definitions
as a guide to help you keep in mind what the classes and
methods are supposed to be doing.
Step 4 - Set up a "Start Swarm" button
There support code for this lab has some buttons already defined.
There are four pre-defined buttons. When clicked, one will cause a
swarm of bees to start buzzing around, while another will (when
clicked) cause the swarm of bees to stop moving altogether.
Open the lab5.support.StartSwarmBuzzingButton in the
current diagram by opening the lab5.support package,
right-clicking on the
lab5.support.StartSwarmBuzzingButton class, and selecting
"Green UML"->"Open in Last Viewed Class diagram". When this opens in
the diagram you will notice that there already is a relationship
between the lab5.support.StartSwarmBuzzingButton class
and the Swarm class.
Now set up an instantiation dependency between
Driver and
lab5.support.StartSwarmBuzzingButton.
Now edit the Driver class. Organize the generated code so that
the window is created first, the swarm second. Finally, make sure
that the swarm is passed as an argument to the button constructor, and
that the button reference is used as an argument in an addButton
method call on the window. Calling this method with the button
reference will ensure that the button appears visually in the program.
Step 5 Add startBuzzing to Swarm
Define a startBuzzing method in the
Swarm class - for now it is fine if its
method body is empty.
Step 6 - Test!
Great! Now you're at a point where you can test your program.
Even though it won't do much, by testing it now you can verify that
you have correctly connected the pieces you are using. After
resetting the DrJava interactions pane, use DrJava to instantiate the
lab5.Driver class. A window should open that looks like
this:
If you think there may be a problem at this point, ask your TA for
help. Otherwise, continue on!
Step 7 - Add a "Stop Swarm" button
You can now add the second button that controls the swarm. Do that
by setting up an instantiation dependency between
Driver and
lab5.support.StopSwarmBuzzingButton. Once you've done
that, define a stopBuzzing method in the
Swarm class - for now it is fine if its method body is
empty.
Step 8 - Test!
Great! Now you're at a point where you can test your program.
Even though it won't do much, by testing it now you can verify that
you have correctly connected the pieces you are using. After
resetting the DrJava interactions pane, use DrJava to instantiate the
lab5.Driver class. A window should open that looks like
this:
If you think there may be a problem at this point, ask your TA for
help. Otherwise, continue on!
Step 9 - The Swarm
Now you're in a position to add bees to the swarm. Doing this will
let you see the effect of pushing the buttons! Remember that we want
starting or stopping the swarm to start or stop each bee in the swarm.
We do this by ensuring that when the startBuzzing or
stopBuzzing of the Swarm is called, the
corresponding method is called on each bee in the swarm.
Again, we'll do this incrementally. First let's add one
Bee to the Swarm. Use Green to create the
appropriate (i.e. going in the right direction) composition
relationship of cardinality 1.
Next you need to edit the code in the definition of the
Swarm class. Just like the shapes from lab 3, like
lab2lib.BouncingBall, had to be added to the
lab3lib.Window by calling the addShape
method on the window, you have to add the Bee to the
lab3lib.Window by calling the addShape
method. But wait, where do we get a lab5lib.Window from?
Remember that the Driver created a
lab5lib.Window. Edit your Swarm class so
that its constructor takes a lab5lib.Window as a
parameter. Then edit the instantiation of the Swarm in
the Driver so that the lab5lib.Window
reference is passed as an argument.
Step 10 - Test!
Now, if you test your program, you should get a Bee
buzzing around in the window. But pressing the buttons still doesn't
appear to do anything. That's OK - you should be all set to carry
on.
Step 11 - Make the Swarm stop its Bee
In fact, pressing the buttons does do something: pressing
the "Stop Swarm" button causes the stopBuzzing method of
the Swarm class to be called! So what? Well, if you
want your Bee to stop moving around, you need to define
the stopBuzzing method so that it calls the
stopBuzzing method of the Bee object whose
reference you're storing in the instance variable in
Swarm.
Step 12 - Test!
Now, if you test your program, you should get a Bee
buzzing around in the window. Pressing the "Stop Swarm" button should
now make the Bee stop moving around. Pressing the "Start
Swarm" button doesn't yet appear to do anything.
Step 13 - Make the Swarm start its Bee
Now you want your Bee to start moving around, when the
"Start Swarm" button is pressed. Define the startBuzzing
method so that it calls the startBuzzing method of the
Bee object whose reference you're storing in the instance
variable in Swarm.
Step 14 - Test!
Now, if you test your program, you should get a Bee
buzzing around in the window. Pressing the "Stop Swarm" button should
now make the Bee stop moving around. Pressing the "Start
Swarm" button should now make the Bee start moving around.
Step 15 - Add more Bees!
You're almost done with the Swarm! All you need to do
is add at least one more Bee. How many more you add is
up to you. To add more Bees, you need to set up the
appropriate composition relationships, and make sure the new
Bees are added to the lab5lib.Window, and
edit the startBuzzing and stopBuzzing
methods so that the new Bees also react when the buttons
are pressed.
Step 16 - Test!
Now, if you test your program, you should get a swarm of Bees
buzzing around in the window. Pressing the "Stop Swarm" button should
now make all the Bees stop moving around. Pressing the "Start
Swarm" button should now make all Bees start moving around.
Steps 17 - Do the Flock
Now you're halfway done! Just follow the same steps to create a
Flock of Birds. The only constraint is that
we'd like your Flock and Swarm differ in how
many Birds and Bees they have. Don't forget
to work incrementally, testing as you go along.
As a rough guideline, you can expect 30 points to come from your
JavaDocs, 60 points to come from coding, and 10
points to come from proper submission.
Once you are ready to submit, export your solution as a jar file.
Name your jar file Lab5.jar
Use the electronic submission command that corresponds to your
recitation. For instance students in section A1 will submit by
typing,