Mini-Lab Entrance Assignment:
Intro to Aquarium Lab Series
This assignment will introduce you to a
program that will simulate fish swimming in an aquarium.
- Download and unzip the AquariumLabSeriesUsingBlueJ
project to your CS110 folder.
- Open the Aquarium Lab Series project in BlueJ.
The first thing you'll notice is that this program has more
classes than the other projects we have worked with so far.
You will be learning about and enhancing the Aquarium simulation
program over the next couple of weeks, and during that time you will
become familiar with these classes and how they relate to each
other. For now, though, we will consider just four:
- The AquaSimApplication class contains the
method to start the program.
- An Aquarium object represents the
aquarium in which fish will swim.
- AquaFish objects represent the
fish swimming in the aquarium.
- An AquaSimGUI object provides a graphical
user interface (display and control button) for the simulation.
- Run the Aquarium simulation using
As a quick way to become familiar with what the Aquarium simulation
program does, compile and run the program using the
method in the AquaSimApplication class. You should see a
window appear with a Start button and some instructions at the
bottom. This is the program's graphical user interface.
When you press the Start button, you should see
blue background (the aquarium). That is
all the program does for now. Close the graphical user
Preparation for First Aquarium Lab Series Mini-Lab
- Why do you think the AquaFish and AquaSimGUI constructors
ask for the aquarium as a parameter?
- Why does the Aquarium "add" method take a parameter but the
AquaFish "moveForward" method and the AquaSimGUI "showAquarium"
methods do not?
- What do you think happens when you move a fish forward a number of
times in a tall, thin aquarium? (This will be the focus of a
future lab exercise.)
Focus on the statements in the
that construct an
Compare them with the
specification for the
Aquarium constructor in the
class documentation. How do you explain the differences
between the class documentation specification and the actual code?
Look, Ma, no main!
Although stand-alone Java programs need a
as a starting point, in BlueJ it is possible to construct objects from
the icons in the project diagram and then invoke methods on those
To run the Aquarium simulation
in BlueJ without a
main method, we will have to do
everything that the
main method usually does, but
- Construct an Aquarium object.
To do this, right-click (or control-click on a Mac) on the icon
for the Aquarium class and select "new Aquarium(int width,
int height)". A dialog box will appear, asking you for the
width and height of the aquarium. You may enter 600 and
480, as the program does when executed from the
main method, or any other dimensions. You
will also have an opportunity to set the name of the instance
(for example, "aqua") or use the default ("aquarium1"). Once
you have constructed the aquarium object, a red icon for it will
appear in the object bench, the window area below the
project diagram and control panel. Note that you will not see
a display of an aquarium with a blue background, because you
have not yet created a graphical user interface and display.
- Construct AquaFish objects.
Right-click (or control-click) on the icon for the AquaFish
class, and construct a fish by selecting the "new
AquaFish(Aquarium aqua)" constructor. For the parameter,
use the name of the Aquarium object in the object bench
area (for example, "aqua" or "aquarium1").
You can also set the name of this AquaFish instance
(e.g., "fish1", "trout", "joe") or use the default ("aquaFish1").
Note that there still is
no visual display of the aquarium because there is, as yet, no
graphical user interface. Construct at least one more
AquaFish object in the object bench. Tell at least one
of your fish to move by right-clicking on the appropriate red
object icon in the object bench and selecting "moveForward".
Again note that there is no visual display of the fish
- Construct the graphical user interface.
Right-click (or control-click) on the icon for the AquaSimGUI
class, and construct a graphical user interface
by selecting the shortest and simplest of the AquaSimGUI
constructors, the one with only a single parameter.
Provide the name of your Aquarium object in the object bench,
just as you did for the fish. A graphical user interface
window with an empty aquarium display should appear. Even
if you were to press the Start button, the aquarium display would
- Display the aquarium.
Right-click (or control-click) on the red icon for the
AquaSimGUI object in the object bench, and select
"showAquarium()". You should see the blue background for
the aquarium, but no fish. This is because we constructed
fish that know about the aquarium (so that they can figure out
where the walls and top and bottom of the aquarium are), but we
did not add the fish to the aquarium.
- Display the aquarium with fish in it.
Add the fish to the aquarium by
right-clicking on the red Aquarium object in the object bench
and selecting the "add (AquaFish fish)" method for each AquaFish
object. Each time, provide the name of one of your
AquaFish objects as the parameter to the add method.
Now go back and right-click on the
red AquaSimGUI icon and select "showAquarium()"; you should
see the fish you added to the aquarium.
- Move and display fish.
Move one of your fish forward with its "moveForward" method and
then redisplay the aquarium with the "showAquarium" method in
the graphical user interface. You should see one fish move
forward. Then move both fish forward one or more times and
redisplay the aquarium.
- Close the graphical user interface window.
Notice that the objects in the object bench disappear.
They will also disappear if you edit any of the Java source
files and recompile the program. After compilation your
object bench will always start over from scratch.
- Experiment with a skinny aquarium.
Construct a new aquarium whose dimensions are 100 pixels wide
and 200 pixels high. Construct one or more fish and add them to
the aquarium, as you did before. Construct a graphical user
interface and display your aquarium and fish. Again, move your
fish forward several times.