1 / 19

Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes. What classes are necessary?. To model fish swimming in a bounded environment , the program has: Fish objects Environment object Simulation object. What does the Simulation class do?.

merle
Download Presentation

Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

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. Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

  2. What classes are necessary? To model fish swimming in a bounded environment, the program has: Fish objects Environment object Simulation object

  3. What does the Simulation class do? The Simulation class represents the behavior that happens in every timestep of the simulation, which is fish movement in this case. Since fish should know how to act in a simulation, both the Fish and Simulation classes share responsibility for knowing what behavior is required in the simulation.

  4. What does the Environment class do? The Environment class models the rectangular grid, keeping track of the fish in the grid. It does not care what their behavior is, except when that behavior changes the number of fish in the grid or their locations. The behavior of the environment does not depend upon the behavior of its occupants, which in our simulation just happen to be fish.

  5. What does the Fish class do? The Fish class encapsulates basic information about a fishand basic fish behavior. For now this behavior is just moving to an adjacent cell in the environment.

  6. public class Simulation { private Environment theEnv; private EnvDisplay theDisplay; public Simulation(Environment env, EnvDisplay display) { theEnv = env; theDisplay = display; theDisplay.showEnv(); Debug.println("-------- Initial Configuration --------"); Debug.println(theEnv.toString()); Debug.println("---------------------------------------"); } public void step() { Locatable[] theFishes = theEnv.allObjects(); for ( int index = 0; index < theFishes.length; index++ ) ((Fish)theFishes[index]).act(); theDisplay.showEnv(); Debug.println(theEnv.toString()); Debug.println("-------- End of Timestep --------"); } }

  7. public interface Environment { int numRows(); int numCols(); boolean isValid(Location loc); int numCellSides(); int numAdjacentNeighbors(); Direction randomDirection(); Direction getDirection(Location fromLoc, Location toLoc); Location getNeighbor(Location fromLoc, Direction compassDir); ArrayList neighborsOf(Location ofLoc); Locatable[] allObjects(); boolean isEmpty(Location loc); Locatable objectAt(Location loc); void add(Locatable obj); void remove(Locatable obj); void recordMove(Locatable obj, Location oldLoc); }

  8. // Fish class heading and variables public class Fish implements Locatable { private static int nextAvailableID = 1; // next avail unique identifier private Environment theEnv; // fish environment private int myId; // unique ID for this fish private Location myLoc; // fish's location private Direction myDir; // fish's direction private Color myColor; // fish's color

  9. // Fish class constructors and the initialize method public Fish(Environment env, Location loc) { initialize(env, loc, env.randomDirection(), randomColor()); } public Fish(Environment env, Location loc, Direction dir) { initialize(env, loc, dir, randomColor()); } public Fish(Environment env, Location loc, Direction dir, Color col) { initialize(env, loc, dir, col); } private void initialize(Environment env, Location loc, Direction dir, Color col) { theEnv = env; myId = nextAvailableID; nextAvailableID++; myLoc = loc; myDir = dir; myColor = col; theEnv.add(this); }

  10. // Fish class randomColor method protected Color randomColor() { Random randNumGen = RandNumGenerator.getInstance(); return new Color(randNumGen.nextInt(256), // amount of red randNumGen.nextInt(256), // amount of green randNumGen.nextInt(256)); // amount of blue }

  11. // Fish class accessor methods public int id() { return myId; } public Environment environment() { return theEnv; } public Color color() { return myColor; } public Location location() { return myLoc; } public Direction direction() { return myDir; }

  12. // Fish class accessor methods public boolean isInEnv() { return environment().objectAt(location()) == this; } public String toString() { return id() + location().toString() + direction().toString(); }

  13. // Fish class movement methods - act and its helpers headings public void act() protected void move() protected Location nextLocation() protected void changeLocation(Location newLoc) protected void changeDirection(Direction newDir)

  14. // Fish class act method public void act() { if ( isInEnv() ) move(); }

  15. // Fish class move helper method protected void move() { Debug.print("Fish " + toString() + " attempting to move. "); Location nextLoc = nextLocation(); if ( ! nextLoc.equals(location()) ) { Location oldLoc = location(); changeLocation(nextLoc); Direction newDir = environment().getDirection(oldLoc, nextLoc); changeDirection(newDir); Debug.println(" Moves to " + location() + direction()); } else Debug.println(" Does not move."); }

  16. // Fish class nextLocation helper method protected Location nextLocation() { ArrayList emptyNbrs = emptyNeighbors(); Direction oppositeDir = direction().reverse(); Location locationBehind = environment().getNeighbor(location(), oppositeDir); emptyNbrs.remove(locationBehind); Debug.print("Possible new locations are: " + emptyNbrs.toString()); if ( emptyNbrs.size() == 0 ) return location(); Random randNumGen = RandNumGenerator.getInstance(); int randNum = randNumGen.nextInt(emptyNbrs.size()); return (Location) emptyNbrs.get(randNum); }

  17. // Fish class emptyNeighbors helper method protected ArrayList emptyNeighbors() { ArrayList nbrs = environment().neighborsOf(location()); ArrayList emptyNbrs = new ArrayList(); for ( int index = 0; index < nbrs.size(); index++ ) { Location loc = (Location) nbrs.get(index); if ( environment().isEmpty(loc) ) emptyNbrs.add(loc); } return emptyNbrs; }

  18. // Fish class changeLocation helper method protected void changeLocation(Location newLoc) { Location oldLoc = location(); myLoc = newLoc; environment().recordMove(this, oldLoc); }

  19. // Fish class changeDirection helper method protected void changeDirection(Direction newDir) { myDir = newDir; }

More Related