// Chapter 4, Exercise Set 2, Question 6 // A turning slow fish may turn right or left even if it doesn't move // outside its own cell. import java.awt.Color; import java.util.ArrayList; import java.util.Random; /** * Marine Biology Simulation: * The TurningSlowFish class represents a fish in the MBS that moves * very slowly. It moves so slowly that it only has a 1 in 5 chance * of moving out of its current cell into an adjacent cell in any * given timestep in the simulation. When it does move beyond its * own cell,its behavior is the same as for objects of the Fish * class. If it doesn't move beyond its cell, the fish may still * turn left or right. * * TurningSlowFish objects inherit instance variables and much of * their behavior from the SlowFish class. * **/ public class TurningSlowFish extends SlowFish { // Instance Variables: Encapsulated data for EACH slow fish private double probOfMoving; // defines likelihood in each timestep private double probOfTurning; // defines likelihood of turning in place private double probOfRight; // defines likelihood of turning right // constructors /** Constructs a turning slow fish at the specified location in a * given environment. This slow fish is colored green. * (Precondition: parameters are non-null; loc is * valid for env.) * @param env environment in which fish will live * @param loc location of the new fish in env **/ public TurningSlowFish(Environment env, Location loc) { // Construct and initialize the attributes inherited from SlowFish. super(env, loc, env.randomDirection(), Color.green); // Define the likelihood that a turning slow fish will move in any // given timestep. This is the same value for all slow fish. probOfMoving = 1.0/5.0; // 1 in 5 chance in each timestep probOfTurning = 0.1; // 1 in 10 chance of turning in place probOfRight = 0.5; // chance of turning right is one-half } /** Constructs a turning slow fish at the specified location and * direction in a given environment. * This slow fish is colored green. * (Precondition: parameters are non-null; loc and * dir are valid for env.) * @param env environment in which fish will live * @param loc location of the new fish in env * @param dir direction the new fish is facing **/ public TurningSlowFish(Environment env, Location loc, Direction dir) { // Construct and initialize the attributes inherited from SlowFish. super(env, loc, dir, Color.green); // Define the likelihood that a turning slow fish will move in any // given timestep. This is the same value for all slow fish. probOfMoving = 1.0/5.0; // 1 in 5 chance in each timestep probOfTurning = 0.1; // 1 in 10 chance of turning in place probOfRight = 0.5; // chance of turning right is one-half } /** Constructs a turning slow fish of the specified color at the * specified location and direction. * (Precondition: parameters are non-null; loc and * dir are valid for env.) * @param env environment in which fish will live * @param loc location of the new fish in env * @param dir direction the new fish is facing * @param col color of the new fish **/ public TurningSlowFish(Environment env, Location loc, Direction dir, Color col) { // Construct and initialize the attributes inherited from SlowFish. super(env, loc, dir, col); // Define the likelihood that a turning slow fish will move in any // given timestep. This is the same value for all slow fish. probOfMoving = 1.0/5.0; // 1 in 5 chance in each timestep probOfTurning = 0.1; // 1 in 10 chance of turning in place probOfRight = 0.5; // chance of turning right is one-half } // redefined methods /** Returns a string representing key information about this * turning slow fish. * @return a string indicating the fish's ID and location **/ public String toString() { return "TurningSlowFish " + super.toString(); } /** Creates a new turning slow fish. * @param loc location of the new fish **/ protected void generateChild(Location loc) { // Create a new fish, which adds itself to the environment. TurningSlowFish child = new TurningSlowFish(environment(), loc, environment().randomDirection(), color()); Debug.println(" Newly created: " + child.toString()); } protected void move() { Location currentLoc = location(); super.move(); if (currentLoc.equals(location())) // Have we not moved? { Random randNumGen = RandNumGenerator.getInstance(); // If this is the probOfTurning chance of turning, // turn left or right. if (randNumGen.nextDouble() < probOfTurning) { turnRightOrLeft(direction()); Debug.println(" Turned and now facing " + direction()); return location(); } } } /** Turns this fish right or left **/ protected void turnRightOrLeft(Direction dir) { Random randNumGen = RandNumGenerator.getInstance(); if (randNumGen.nextDouble() < probOfRight) { changeDirection(dir.toRight()); } else { changeDirection(dir.toLeft()); } } }