Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu...

Preview:

Citation preview

Extending the Case Study

Barbara Ericson

ericson@cc.gatech.edu

January 2005

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)

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()

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 imageDisplayMap.associate("SlowFish",

new FishImageDisplay("smallfish.gif",

Direction.EAST));

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

Fish and HungryFish

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

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

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

Sample New Classes

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

Walls

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 rat in a maze – Some rats can move randomly– Some rats can go toward the cheese

Recommended