Extending the Case Study.ppt: uploaded 1 April 2016 at 4:01 pm

Download Report

Transcript Extending the Case Study.ppt: uploaded 1 April 2016 at 4:01 pm

Extending the Case Study
Barbara Ericson
[email protected]
March 2006
Georgia Institute of Technology
Ways to Extend the Case Study
• Subclass Fish
– Add HungryFish that eat other fish when they are
hungry enough
– Add BottomFish that stay on the bottom
– Add SickFish that spread disease to neighbors
• Add non-Fish classes
– Walls (extend AbstractDrawable)
– Snorkelers (extend AbstractActionable)
– Dolphins (extend Mammal)
Georgia Institute of Technology
How to Subclass Fish
• Create a new class that extends Fish
– Like HungryFish
• Add any fields needed by the new class
• Add constructors that call super to initialize the
inherited private fields
• Override the act method
– public void act()
• Override generateChild to create this kind of fish
– protected void generateChild(Location loc)
• Add any methods needed by act()
Georgia Institute of Technology
Adding Subclasses of Fish
• Edit MBSGUI.java
– Add the new class name to fishClassNames
• String[] fishClassNames = {"Fish", "HungryFish",
"DarterFish", "SlowFish"};
– Add a way to display the new class
• Custom drawn one
DisplayMap.associate("HungryFish", new RoundFishDisplay());
• Gif image
DisplayMap.associate("SlowFish",
new FishImageDisplay("smallfish.gif",
Direction.EAST));
Georgia Institute of Technology
Adding HungryFish
• Add a field to say how hungry this fish is
– private int hunger = 0;
• Add a field to say when ready to eat
– private static final int NEED_TO_EAT = 5;
• Override the act() method
– Increase the hunger each time the method is called
– If hungry enough eat a random fish neighbor
• Move to the neighbor’s location and reset hunger to 0
– Otherwise use the inherited move method
Georgia Institute of Technology
Fish and HungryFish
Georgia Institute of Technology
Adding Non-Fish
• The problem is the assumption in many
classes that you will only have Fish or
subclasses of Fish
• To solve this I needed to change the GUI
classes
– So use mbsguigt.jar instead of mbsgui.jar in
your classpath
– Replace Fish, BoundedEnv, and MBSGUI,
and Simulation
Georgia Institute of Technology
Added Interfaces and Classes
• Interfaces
– Drawable inherits from Locatable
– Actionable inherits from Drawable
• New Classes
– AbstractActionable the class to use to create non-fish
objects that can act() and die()
– AbstractDrawable the class to use to create non-fish
objects that don’t act()
– DrawableImageDisplay the class to use to display gifs
for non-fish objects
Georgia Institute of Technology
Sample New Classes
• Added a Mammal Class
– Need to come to the surface to breathe
– Extends AbstractActionable
• Added a Starfish Class
– Which drops to the bottom and moves along
the bottom
– Extends AbstractActionable
• Added an Orca Class
– Extends Mammal
Georgia Institute of Technology
Sample New Classes
Georgia Institute of Technology
Adding Walls
• The Wall class extends AbstractDrawable
• Displayed by WallDisplay which just draws
a filled rectangle
– extends AbstractDrawableDisplay
• Has an id, location, direction, color, and
environment
• Don’t act or die
Georgia Institute of Technology
Walls
Georgia Institute of Technology
Other Ways to Extend
• Change the way things are drawn
– Create new classes that extend FishDisplay
like RoundFishDisplay and
NarrowFishDisplay
• Reuse classes for Checkers
• Simulate a mouse in a maze
– Some mice can move randomly
– Smart mice can go towards the cheese
Georgia Institute of Technology
Georgia Institute of Technology
For More Information
• See http://coweb.cc.gatech.edu/ice-gt/165
• And http://coweb.cc.gatech.edu/ice-gt/166
Georgia Institute of Technology