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. 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 the 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 a utility class called NavigationalAide that helps a fish keep track of its position and move around in the aquarium. You will need to read class documentation for these classes, but you will not need to read or modify their code as part of this lab series.

Exercise: Downloading and Running the Initial Program

  • You can download the files needed for the Aquarium Lab Series separately from the list below 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 five classes and a graphics library: 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. This is the program's graphical user interface. Some instructions should appear either at the bottom of this window or in a separate window. When you press the Start button, you should just see a blue background representing 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 AquaSimApplication class and 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 main method in AquaSimApplication.java creates an aquarium, but it doesn't have any fish in it. To add 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.

Exercise: Constructing Objects

  • 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.

  • Research the class documentation of (or specification for) the AquaFish class to discover how to construct a fish. Notice that there are two AquaFish constructors, so two different ways to construct a fish. For this lab, just concentrate on the simpler, one-parameter constructor. Edit the main method in AquaSimApplication.java to declare and construct three fish variables at the end of the first section. Give your new variables names that convey their purpose. 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.

  • 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?
    Hint: look at the comment above the statement that tells the user interface to show the aquarium. This exercise directed you to construct three fish, but have they been added to the aquarium yet?



Seeing is Believing

Introduction

We have constructed new fish, but we cannot see them because we have not yet added them to the aquarium. How do we know what operations we can use to do this and how to use them?

Exercise

  • Research the class documentation for the Aquarium class to discover how to add a fish to an aquarium. Modify the main method in the AquaSimApplication class to add all three of your new fish to the aquarium. (Analysis Question: where should this code go in the main method?) Again, 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?



Let's Move!

We can finally see our newly constructed fish in the aquarium, but the program isn't very interesting because the fish aren't doing anything. We need to read the class documentation for the AquaFish class to learn how to get our fish to do something interesting (like move).

Exercise

  • 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 main method 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 main method 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.