Class Fish

java.lang.Object
  |
  +--Fish
All Implemented Interfaces:
Locatable
Direct Known Subclasses:
DarterFish, SlowFish

public class Fish
extends java.lang.Object
implements Locatable

Marine Biology Simulation: The Fish class represents a fish in the Marine Biology Simulation. Each fish has a unique ID, which remains constant throughout its life. A fish also maintains information about its location and direction in the environment.

See Also:
Environment, Direction, Location

Constructor Summary
Fish(Environment env, Location loc)
          Constructs a fish at the specified location in a given environment.
Fish(Environment env, Location loc, Direction dir)
          Constructs a fish at the specified location and direction in a given environment.
Fish(Environment env, Location loc, Direction dir, java.awt.Color col)
          Constructs a fish of the specified color at the specified location and direction.
 
Method Summary
 void act()
          Acts for one step in the simulation.
protected  void changeDirection(Direction newDir)
          Modifies this fish's direction.
protected  void changeLocation(Location newLoc)
          Modifies this fish's location and notifies the environment.
 java.awt.Color color()
          Returns this fish's color.
 Direction direction()
          Returns this fish's direction.
protected  java.util.ArrayList emptyNeighbors()
          Finds empty locations adjacent to this fish.
 Environment environment()
          Returns this fish's environment.
 int id()
          Returns this fish's ID.
 boolean isInEnv()
          Checks whether this fish is in an environment.
 Location location()
          Returns this fish's location.
protected  void move()
          Moves this fish in its environment.
protected  Location nextLocation()
          Finds this fish's next location.
protected  java.awt.Color randomColor()
          Generates a random color for this fish.
 java.lang.String toString()
          Returns a string representing key information about this fish.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Fish

public Fish(Environment env,
            Location loc)
Constructs a fish at the specified location in a given environment. The Fish is assigned a random direction and random color. (Precondition: parameters are non-null; loc is valid for env.)
Parameters:
env - environment in which fish will live
loc - location of the new fish in env

Fish

public Fish(Environment env,
            Location loc,
            Direction dir)
Constructs a fish at the specified location and direction in a given environment. The Fish is assigned a random color. (Precondition: parameters are non-null; loc and dir are valid for env.)
Parameters:
env - environment in which fish will live
loc - location of the new fish in env
dir - direction the new fish is facing

Fish

public Fish(Environment env,
            Location loc,
            Direction dir,
            java.awt.Color col)
Constructs a fish of the specified color at the specified location and direction. (Precondition: parameters are non-null; loc and dir are valid for env.)
Parameters:
env - environment in which fish will live
loc - location of the new fish in env
dir - direction the new fish is facing
col - color of the new fish
Method Detail

randomColor

protected java.awt.Color randomColor()
Generates a random color for this fish.
Returns:
the color of the new fish

id

public int id()
Returns this fish's ID.
Returns:
the unique ID for this fish

environment

public Environment environment()
Returns this fish's environment.
Returns:
the environment in which this fish lives

color

public java.awt.Color color()
Returns this fish's color.
Returns:
the color of this fish

location

public Location location()
Returns this fish's location.
Specified by:
location in interface Locatable
Returns:
the location of this fish in the environment

direction

public Direction direction()
Returns this fish's direction.
Returns:
the direction in which this fish is facing

isInEnv

public boolean isInEnv()
Checks whether this fish is in an environment.
Returns:
true if the fish is in the environment (and at the correct location); false otherwise

toString

public java.lang.String toString()
Returns a string representing key information about this fish.
Overrides:
toString in class java.lang.Object
Returns:
a string indicating the fish's ID and position

act

public void act()
Acts for one step in the simulation.

move

protected void move()
Moves this fish in its environment.

nextLocation

protected Location nextLocation()
Finds this fish's next location. A fish may move to any empty adjacent locations except the one behind it (fish do not move backwards). If this fish cannot move, nextLocation returns its current location.
Returns:
the next location for this fish

emptyNeighbors

protected java.util.ArrayList emptyNeighbors()
Finds empty locations adjacent to this fish.
Returns:
an ArrayList containing neighboring empty locations

changeLocation

protected void changeLocation(Location newLoc)
Modifies this fish's location and notifies the environment.
Parameters:
newLoc - new location value

changeDirection

protected void changeDirection(Direction newDir)
Modifies this fish's direction.
Parameters:
newDir - new direction value