Extending the Fish Class
After I had become familiar with the existing Marine Biology Simulation code, I talked to the marine biologists about what modifications or new features they wanted next. They wanted me to create several new kinds of fish with specialized patterns of movement to see what effect that would have on the simulation. Two that they were interested in to start were fish that dart quickly back and forth, and fish that move so slowly that they seem to move only occasionally.
First, Simple Subclass
To make sure I understood how to create a subclass, and how a subclass
inherits from, and extends, its superclass, I decided to create the
simplest possible Fish subclass, and then build up to the
subclasses that the biologists were interested in.
Programming Exercise: Simplest Possible Subclass
- Construct a new class, replacing its contents with the contents of this minimalist template. Change the class name before you save the file. Give it any name that you want (but not
Whitefish,DarterFish, orSlowFish, which are three classes you will develop shortly.)- Copy the constructors (and only the constructors) from the
Fishclass. Rename them to match the name you gave the class.- Specify that the class is a subclass of the
Fish(extends theFishclass). (How do you do this?)- Remove the contents of the last constructor, replacing the code with a single line to call
super. This will initialize whatever data your class inherits from theFishclass. (What is the difference between callingthisand callingsuper? Can some constructors callthisand some callsuper?)- Are any other methods absolutely required?
- Edit the
MBSAppclass to include your new class in the list of MBS object types. Then add a newDisplayMap.associatestatement that says how to display objects of your new class. Use different display choices to display theFishclass and your new class.- Before you run your program, think about what you expect the results of your testing to be. Do you expect objects of your new class to behavior similarly to, or differently from, the
Fishclass? (Have you added or redefined any behavior?)- Run the simulation program, constructing and populating the grid manually. Include objects of both the `Fish` class and your new class. Do the objects of your new class behave any differently from objects of the
Fishsuperclass?- Once you have the program working with your new class, make sure it has all three constructors that its
Fishsuperclass has.
Second, Simple Subclass
Programming Exercise: Whitefish
- Create a subclass of the
FishcalledWhitefishwhose constructors create white fish. (It would probably be easiest to start by copying and pasting from the class you created above, then modifying it to construct fish that are white.)- Edit the
MBSAppclass to include your new class in the list of MBS object types. Then add a newDisplayMap.associatestatement that says how to display objects of your new class. Use different display choices to display theFishclass and your new class.- Before you run your program, always think about what you expect the results of your testing to be. Do you expect objects of this new class to behavior similarly to, or differently from, the
Fishclass?- Run the simulation program, constructing and populating the grid manually. Include objects of both the `Fish` class and your new class. Do the objects of your new class behave any differently from objects of the
Fishsuperclass?
Extending a Class, Redefining Methods
Programming Exercise: Fish with Age
- Edit your
Whitefishclass to add an instance variable to keep track of a fish's age. (Run the program to make sure it still works; do you expect anything about the behavior to be different yet?)- Modify your constructor(s) to give a fish a random age when you construct it. If you've used calls to
thiscreatively, you will only have to modify one constructor. (Run the program to make sure it still works; do you expect anything about the behavior to be different yet?)- Redefine the
toStringmethod to report on a fish's age, along with its class name, location, and direction. You can start by copying the method from theFishclass into theWhitefishclass. (Run the program to make sure it still works; do you expect anything about the behavior to be different yet?)
Programming Exercise: Aging Fish
It would be more realistic if the fish age over the course of the simulation.
- Redefine the
actmethod in theWhitefishclass to callmove(as it does in theFishclass) and also increment the fish's age.- Run your program to test the new
Whitefishbehavior with a small number of fish. What are the fish ages when you first create them? Use the "Step N Times" button to run the simulation for a set number of times. What are the fish ages now?