// Chapter 4, Exercise Set 1, Question 7 TurningDarter Class // The turningDarter has all the characteristics of a darter fish // except that it has a 0.1 chance of turning right or left before it // tries to move forward. import java.awt.Color; import java.util.Random; public class TurningDarter extends DarterFish { // Instance Variables: Encapsulated data for EACH TurningDarter fish private double probOfTurning; // defines likelihood of turning in each timestep private double probOfRight; //defines likelihood of turning right // constructors /** Constructs a turning darter fish at the specified location in a * given environment. This turning darter is colored magenta. * (Precondition: parameters are non-null) * @param env environment in which fish will live * @param loc location of the new fish in env **/ public TurningDarter(Environment env, Location loc) { // Construct and initialize the attributes inherited from Fish. super(env, loc, env.randomDirection(), Color.magenta); probOfTurning = 0.1; probOfRight = 0.5; } /** Constructs a turning darter fish at the specified location with * the direction in a given environment. * This turning darter is colored magenta. * (Precondition: parameters are non-null) * @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 TurningDarter(Environment env, Location loc, Direction dir) { // Construct and initialize the attributes inherited from Fish. super(env, loc, dir, Color.magenta); probOfTurning = 0.1; probOfRight = 0.5; } /** Constructs a turning darter fish of the specified color at the * specified location and direction. * (Precondition: parameters are non-null) * @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 TurningDarter(Environment env, Location loc, Direction dir, Color col) { // Construct and initialize the attributes inherited from Fish. super(env, loc, dir, col); probOfTurning = 0.1; probOfRight = 0.5; } /** Returns a string representing key information about this fish. * @return a string indicating the fish's ID and location **/ public String toString() { return "TurningDarter " + super.toString(); } /** Moves this fish in its environment. * A turning darter fish first sees if it should turn left or right * and then darts forward (as specified in nextLocation) if possible, * or reverses direction (without moving) if it cannot move forward. **/ protected void move() { // If this is the 0.1 chance of turning, turn left or right Random randNumGen = RandNumGenerator.getInstance(); if ( randNumGen.nextDouble() < probOfTurning ) { turnRightOrLeft(direction()); Debug.println(toString() + " Turned and now facing " + direction()); } super.move(); } /** 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()); } } }