Class | Tree | Index | Help
| AP® Computer Science Marine Biology Simulation
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## Class Fish

    java.lang.Object
      |
      +--Fish

All Implemented Interfaces:
  Locatable

Direct Known Subclasses:
  DarterFish, SlowFish

public class Fish
extends java.lang.Object
implements Locatable

AP® Computer Science Marine Biology Simulation:
A `Fish` object 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.

Modification History: - Modified to support a dynamic population in the environment: fish can now breed and die.

The `Fish` class is copyright© 2002 College Entrance Examination Board (www.collegeboard.com).

Version:
  1 July 2002
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 boolean` | `breed()` Attempts to breed into neighboring locations.
`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.
`protected void` | `die()` Removes this fish from the environment.
`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.
`protected void` | `generateChild(Location loc)` Creates a new fish with the color of its parent.
`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.
`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` is 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` is 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.
  Returns:
    the new random color

### 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, location, and direction

### act

    public void act()

  Acts for one step in the simulation.

### breed

    protected boolean breed()

  Attempts to breed into neighboring locations.
  Returns:
    `true` if fish successfully breeds; `false` otherwise

### generateChild

    protected void generateChild(Location loc)

  Creates a new fish with the color of its parent.
  Parameters:
    `loc` - location of the new fish

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

### die

    protected void die()

  Removes this fish from the environment.

Class | Tree | Index | Help
| AP® Computer Science Marine Biology Simulation
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright© 2002 College Entrance Examination Board