Getting Ready:
main method,
as you did in the
Getting
Started with BlueJ
lab. Remember that it should just display an empty aquarium
with no fish.AquaSimApplication icon
in the project diagram to look at its source code in an editor
window.
Move the
editor window containing the source code aside so that you can
see the two BlueJ windows (editor window and project window)
side by side.
Read the initial version of the main method in
AquaSimApplication.java. This method has three
sections. The
first constructs the objects needed to run the simulation. The
second and third sections, which are currently much shorter,
run the simulation (or will, when we have added some more
functionality to it) and wrap up the program, reminding the
user how to quit.
main,
as you did in the the "Look, Ma, no main!" exercise from the
Getting
Started with BlueJ
lab.
As you construct each object and invoke each method,
notice how your actions, and the parameters you provide, compare
with the Java statements in the
main method in the AquaSimApplication
class. Can you tell which actions correspond to the missing
code in the main method?
From Lab 1:
Exercise 4: Look, Ma, no main!
- 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
mainmethod, 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 movement.
- 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 remain empty.
- 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.
main method
that construct an
Aquarium object.
Compare them with the
specification for the
Aquarium constructor in the Aquarium
class documentation. How do you explain the differences
between the class documentation specification and the actual code?
How is the code similar to what you did manually as part of the
"Look, Ma, no main!" exercise?