Mini-Lab: Creating Fish in an Aquarium

Reading Specifications and Writing Client Code


This set of Mini-Lab Exercises is the first in a series in which students build a small program with several fish moving around in an aquarium. The set includes the following exercises:

Each section contains an Introduction to a problem or task, descriptions or examples of one or more Concepts to apply in solving the problem or completing the task, and an Exercise.

Before working through this Mini-Lab, students should understand the role of the main method in an application, the role of classes and objects, and the syntax for documenting code with comments.



Getting Started

Introduction

The Aquarium Simulation program is meant to simulate several fish moving in an aquarium. A skeleton of the program already exists. It contains six classes with which you will become familiar as you work through the exercises in this lab series. The skeleton program constructs an Aquarium object and a single AquaFish object; in the exercises in this Mini-Lab, you will be adding several more fish to the aquarium and directing them to move.

Exercise: Downloading and Running the Initial Program

  • You can download the files needed for the Aquarium Lab Series separately from the list below, download them as a single zip file, or download them as a BlueJ package. You will also be able to download the lab series instructions as a zip file once all of the labs have been updated.
     
    The code for the Aquarium simulation program consists of six classes and a graphics library:
    • AquaSimApplication (class with trivial main method, which merely constructs an AquariumController program and asks it to run)
    • AquariumController class (the controller creates the aquarium, the fish, and the graphical user interface; asks the fish to move; and asks the graphical user interface to display the aquarium)
    • Aquarium class (representing an aquarium)
    • AquaFish class (representing fish in the aquarium)
    • NavigationalAide class (a supplement or aide to the AquaFish class, which keeps track of a fish's size, location, and direction; separated out from the AquaFish class to make that class easier to read)
    • AquaSimGUI class (provides the user interface, e.g., buttons, text output, and graphical display)
    • jpt.jar (a graphics library from Northeastern University )
    All classes are covered by the GNU General Public License.

  • As a quick way to become familiar with what the Aquarium simulation program does, compile and run the program (remember that the main method is 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 a white fish drawn on a blue background (the aquarium). That is all the program does for now. Close the graphical user interface window to quit the program.

Exercise: A Simple Modification

  • Edit the AquariumController class to modify the first output statement to welcome users to the fish aquarium program rather than state that it will be an aquarium simulation. Test the modified program. Does it behave any differently? Why, or why not? (How you compile the program will depend on the Java development environment you use.)



Populating the Aquarium

Introduction

The AquariumController constructor creates an aquarium with a single fish in it. To add other fish to the aquarium, we must declare variables for the fish, construct the actual fish objects (AquaFish instances), and initialize the new variables to be references to the newly constructed fish. The AquaFish class documentation gives the specifics on how to construct objects of the class.



Let's Move!

Introduction

We have constructed new fish, but we cannot see them because we have not yet added them to the aquarium. And even if we could see them, the program wouldn't be very interesting because the fish aren't doing anything. We need to add the new fish to the aquarium and then read the class documentation for the AquaFish class to learn how to get our fish to do something interesting (like move).

Exercise

  • Modify the AquariumController class to add your new, additional fish to the aquarium, if you haven't done that already. Before you test your modified program, decide whether you expect this modification to change the appearance of the display or not, and, if so, how you expect it to be different. Then test the program. Was the actual result what you expected or not? If not, did you have the wrong expectation, or is the behavior of the program wrong?

  • Now it is time to start running the aquarium simulation. Research the class documentation for the AquaFish class to discover how to make a fish move forward. Add statements to the AquariumController class to move your three fish one step forward. (Where should these statements be added?) After you make the fish move, redisplay the aquarium. (Do you need to read the class documentation for the AquaSimGUI class to know how to do that, or do you have an example in the AquariumController class already?) Make sure there are blank lines that separate this sequence of new statements, which together perform a single function, from the existing code around them. Make sure that the internal comments reflect the behavior of your code.

  • Test your modified program. Was the behavior as you expected or not? If not, did you have the wrong expectation, or is the behavior of the program wrong? You may occasionally only see two fish (or even one!). This can happen when a larger fish overlaps and hides a smaller fish. Run your program a number of times to verify that you usually see three fish.

  • Update the class documentation at the top of the file to reflect your modifications.