Extending the Case Study Barbara Ericson ericsoncc gatech
Extending the Case Study Barbara Ericson ericson@cc. gatech. edu March 2006
Ways to Extend the Case Study • Subclass Fish – Add Hungry. Fish that eat other fish when they are hungry enough – Add Bottom. Fish that stay on the bottom – Add Sick. Fish that spread disease to neighbors • Add non-Fish classes – Walls (extend Abstract. Drawable) – Snorkelers (extend Abstract. Actionable) – Dolphins (extend Mammal)
How to Subclass Fish • Create a new class that extends Fish – Like Hungry. Fish • 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 generate. Child to create this kind of fish – protected void generate. Child(Location loc) • Add any methods needed by act()
Adding Subclasses of Fish • Edit MBSGUI. java – Add the new class name to fish. Class. Names • String[] fish. Class. Names = {"Fish", "Hungry. Fish", "Darter. Fish", "Slow. Fish"}; – Add a way to display the new class • Custom drawn one Display. Map. associate("Hungry. Fish", new Round. Fish. Display()); • Gif image Display. Map. associate("Slow. Fish", new Fish. Image. Display("smallfish. gif", Direction. EAST));
Adding Hungry. Fish • 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 Hungry. Fish
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, Bounded. Env, and MBSGUI, and Simulation
Added Interfaces and Classes • Interfaces – Drawable inherits from Locatable – Actionable inherits from Drawable • New Classes – Abstract. Actionable the class to use to create non-fish objects that can act() and die() – Abstract. Drawable the class to use to create non-fish objects that don’t act() – Drawable. Image. Display 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 Abstract. Actionable • Added a Starfish Class – Which drops to the bottom and moves along the bottom – Extends Abstract. Actionable • Added an Orca Class – Extends Mammal
Sample New Classes
Adding Walls • The Wall class extends Abstract. Drawable • Displayed by Wall. Display which just draws a filled rectangle – extends Abstract. Drawable. Display • 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 Fish. Display like Round. Fish. Display and Narrow. Fish. Display • Reuse environment classes for Checkers • Simulate a mouse in a maze – Some mice can move randomly – Smart mice can go towards the cheese
For More Information • See http: //coweb. cc. gatech. edu/ice-gt/165 • And http: //coweb. cc. gatech. edu/ice-gt/166
- Slides: 15