Aquarium Lab Series    

Mini-Lab: Creating Fish in an Aquarium

Reading Specifications and Writing Client Code

Alyce Brady
Kalamazoo College


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, (usually) abridged versions of one or more Patterns that will be useful 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. Students should also have read over the patterns that appear in this document before the lab.



Getting Started

Introduction

The Aquarium Simulation program is meant to simulate several fish moving in an aquarium. A skeleton of the program already exists. The main class, the one that runs the simulation, is the AquaSimApplication class. There is also an Aquarium class and an AquaFish class. The skeleton program constructs an Aquarium object in the main method; in the exercises that follow, you will be creating several AquaFish objects in the aquarium and directing them to move.

The program also contains an AquaSimGUI class that provides a graphical user interface for the Aquarium Lab Series. The interface is an area where the program can display a graphical picture of an aquarium and its fish. The program also includes several "black box" utility classes that provide necessary functionality for the AquaFish and AquaSimGUI classes, but which you will not need to read or modify as part of this lab series.

Exercise

  • Download the zip file containing the starting code files for the Aquarium Lab Series by right-clicking or shift-clicking on the following link: AquariumLabSeries.zip.

    When you unzip this file, it will create a folder called AquariumLabSeries  that contains the instructions for the labs, a folder called Code, and a folder containing class documentation.  The Code folder contains three classes you will be viewing and modifying, a sub-folder with other "black-box classes" needed by the program (that you do not need to study), and a graphics library.

    All classes are covered by the GNU General Public License.

  • Edit AquaSimApplication.java to modify the first output statement to welcome users to the fish aquarium program. Test the modified program.



Populating the Aquarium

Introduction

The main method in AquaSimApplication.java creates an aquarium, but it doesn't have any fish in it. We can use the Declare-Construct-Initialize pattern to create fish to go in the aquarium. The AquaFish interface gives the specifics on how to construct objects of the class.

Exercise

  • 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 actually runs it (or will, when we have added some more functionality to it). The third section wraps up the program, reminding the user how to quit.

  • Research the abbreviated description of (or specification for) the AquaFish class to discover how to construct a fish. Edit the main method in AquaSimApplication.java to declare and construct three fish variables at the end of the first section. Give them Intention Revealing Names. Using blank lines, separate this sequence of new statements, which together perform a single function, from the existing code around them. Add a single comment preceding them that describes the purpose of the sequence.

  • Test your modified program. Does it look any different? You constructed three fish, but the userInterface is only drawing the aquarium, not the fish.



Let's Move!

Introduction

Now the aquarium has three fish, but we can't see them. And even if we could see them, it wouldn't be very interesting because they aren't doing anything. We need to Read the [AquaSimGUI] Interface for Methods to learn how to view fish objects and Read the [AquaFish] Interface for Methods to learn how to get our fish to do something interesting (like move).

Exercise

  • Research the abbreviated specification for the AquaSimGUI class to discover how to view a fish. Modify the main method to display the three fish you constructed. You want to draw the fish after drawing the aquarium (or else the blue water in the aquarium will hide the fish), but before repainting the user interface. Test your modified program.
  • Now it is time to start running the aquarium simulation. Research the abbreviated specification for the AquaFish class to discover how to make a fish move forward. Add statements to the main method to move your three fish one step forward. (Where should these statements be added?) Using blank lines, separate this sequence of new statements, which together perform a single function, from the existing code around them. Add a single comment preceding them that describes the purpose of the sequence. Then add code (and comments) to redraw the fish.
  • Test your modified program. If you don't see the fish move, you probably forgot to repaint the user interface to make the changes visible. You may also see two copies (or one "blurred" copy) of each fish -- the fish in its old location and in its new location. If you see this, you forgot to redraw the aquarium to "erase" (or cover up) all the fish in their old locations before displaying the fish in their new locations. Add a line to the beginning of the second block of display statements to redraw the aquarium. You may even 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.
  • Now when you run your program you should no longer have two copies of each fish, but you probably can't really see the fish move because it happens too quickly to see. Research the abbreviated specification for the AquaSimGUI class to discover how to ask the user interface to pause after each set of display statements so that you can see the fish before and after they move.
  • Update the program documentation at the top of the file to reflect your modifications.



Copyright Alyce Faulstich Brady, 2001-2002.