Download presentation
Presentation is loading. Please wait.
Published byBarbra Booker Modified over 9 years ago
1
Georgia Institute of Technology Extending the Case Study Barbara Ericson ericson@cc.gatech.edu January 2005
2
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)
3
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()
4
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));
5
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
6
Georgia Institute of Technology Fish and HungryFish
7
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
8
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
9
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
10
Georgia Institute of Technology Sample New Classes
11
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
12
Georgia Institute of Technology Walls
13
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 rat in a maze –Some rats can move randomly –Some rats can go toward the cheese
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.