1 / 15

Extending the Case Study

Learn how to subclass Fish, add HungryFish, BottomFish, SickFish, non-Fish classes, and more. Modify GUI classes to accommodate new classes and utilize interfaces for flexibility in modeling aquatic scenarios. Enhance with Walls, Snorkelers, and Dolphins.

ellenhunter
Download Presentation

Extending the Case Study

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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

  4. 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

  5. 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

  6. Fish and HungryFish Georgia Institute of Technology

  7. 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

  8. 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

  9. 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

  10. Sample New Classes Georgia Institute of Technology

  11. 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

  12. Walls Georgia Institute of Technology

  13. Other Ways to Extend • Change the way things are drawn • Create new classes that extend FishDisplay like RoundFishDisplay and NarrowFishDisplay • Reuse environment 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

  14. Georgia Institute of Technology

  15. 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

More Related