Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

19
Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

description

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?. - PowerPoint PPT Presentation

Transcript of Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

Page 1: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

Marine Biology Simulation

Case Study

Lab03 Slides

Investigate the Core Classes

Page 2: 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

Page 3: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

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.

Page 4: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

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.

Page 5: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

What does the Fish class do?

The Fish class encapsulates basic information about a fish and basic fish behavior.

For now this behavior is just moving to an adjacent cell in the environment.

Page 6: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

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

Page 7: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

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

Page 8: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

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

Page 9: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

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

Page 10: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

// 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}

Page 11: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

// 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; }

Page 12: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

// Fish class accessor methods

public boolean isInEnv(){ return environment().objectAt(location()) == this; }

public String toString(){ return id() + location().toString() + direction().toString();}

Page 13: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

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

Page 14: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

// Fish class act method

public void act(){ if ( isInEnv() ) move();}

Page 15: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

// 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.");}

Page 16: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

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

Page 17: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

// 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;}

Page 18: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

// Fish class changeLocation helper method

protected void changeLocation(Location newLoc){ Location oldLoc = location(); myLoc = newLoc; environment().recordMove(this, oldLoc);}

Page 19: Marine Biology Simulation Case Study Lab03 Slides Investigate the Core Classes

// Fish class changeDirection helper method

protected void changeDirection(Direction newDir){ myDir = newDir;}