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
Fish
class. Rename them to match the name you gave the class.- Specify that the class is a subclass of the
Fish
(extends theFish
class). (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 theFish
class. (What is the difference between callingthis
and callingsuper
? Can some constructors callthis
and some callsuper
?)- Are any other methods absolutely required?
- Edit the
MBSApp
class to include your new class in the list of MBS object types. Then add a newDisplayMap.associate
statement that says how to display objects of your new class. Use different display choices to display theFish
class 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
Fish
class? (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
Fish
superclass?- Once you have the program working with your new class, make sure it has all three constructors that its
Fish
superclass has.
Second, Simple Subclass
Programming Exercise: Whitefish
- Create a subclass of the
Fish
calledWhitefish
whose 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
MBSApp
class to include your new class in the list of MBS object types. Then add a newDisplayMap.associate
statement that says how to display objects of your new class. Use different display choices to display theFish
class 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
Fish
class?- 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
Fish
superclass?
Extending a Class, Redefining Methods
Programming Exercise: Fish with Age
- Edit your
Whitefish
class 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
this
creatively, 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
toString
method to report on a fish's age, along with its class name, location, and direction. You can start by copying the method from theFish
class into theWhitefish
class. (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
act
method in theWhitefish
class to callmove
(as it does in theFish
class) and also increment the fish's age.- Run your program to test the new
Whitefish
behavior 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?