Mini-Lab: Creating Fish in an Aquarium

Reading Interfaces 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, (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 function in an application and the role of classes and objects. Students should also have read over the patterns that appear in this document before the lab.



Getting Started

Introduction

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

The program also contains a Display class. There are actually two different implementations of the Display class. One produces a text-based display of the fish in the aquarium; the other draws them graphically using the CMU Graphics package*. The main function constructs a single Display object that you will use to display the aquarium after you have created and moved the fish. Finally, the program includes some utility classes and functions, including the RandGen class from the Advanced Placement Marine Biology Case Study+ for generating random numbers, and the WaitNClear function, which is useful if you are using the graphical implementation of the Display class.

Exercise

  • Edit the main function to modify the output statement to welcome users to the fish aquarium program. Test the modified program.



Populating the Aquarium

Introduction

The main function 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

    Research the abbreviated AquaFish interface to discover the simplest way to construct a fish. Edit the main function in the aquarium program to construct three fish variables.



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 [Display] Interface for Methods to learn how to display 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 Display interface (in its header file) to discover how to display a fish. Modify the main function to display the three fish you constructed. 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.

  • Research the abbreviated AquaFish interface class to discover how to make a fish move forward. Add statements to the main function to move your three fish one step forward. Then display the fish again. Again, use blank lines and comments to separate functional units of code from each other. Do this in the output as well, by adding a statement to print a newline after the initial display of your fish.

      Question: Are the fish guaranteed to still be in the bounds of the aquarium after the move forward? Why or why not?

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



*The CMU Graphics Library was created by the Carnegie Mellon University School of Computer Science and is used in their introductory programming courses. It is available from Mark Stehlik's Advanced Placement page.

+The Aquarium series of labs is loosely based on the Advanced Placement Computer Science Marine Biology Simulation Case Study, available from the College Board for face-to-face teaching purposes.



Copyright Alyce Faulstich Brady, 2000.