13
Extending the Case Study Barbara Ericson [email protected] January 2005

Georgia Institute of Technology Extending the Case Study Barbara Ericson [email protected] January 2005

Embed Size (px)

Citation preview

Page 1: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

Extending the Case Study

Barbara Ericson

[email protected]

January 2005

Page 2: Georgia Institute of Technology 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)

Page 3: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

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

Page 4: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

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

Page 5: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

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

Page 6: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

Fish and HungryFish

Page 7: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

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

Page 8: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

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

Page 9: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

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

Page 10: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

Sample New Classes

Page 11: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

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

Page 12: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

Walls

Page 13: Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005

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