MBCS Test

This test involves reasoning about the code for the Marine Biology Case study. You should have your copy of the case study with you for the exam.
  1. Consider defining a new class of Fish called StraightFish, which will move straight forward, one space at a time, until it hits something, then turn in a random direction. When this fish turns, it has an equal chance to end up facing in any of the three directions other than the one in which it was facing. Determining that it was blocked and turning instead constitutes an entire move: the fish does not move forward on a step in which it turned.

    The StraightFish is defined by extending the Fish class and overriding the move() and nextLocation() methods. You will also write a helper method turn() to turn in a random direction that is not the fish's current direction.

    The partial declaration for class StraightFish is shown below.

    public class StraightFish extends Fish {
       // Turn the fish toward one of the three directions that is not
       // how it is facing now, with all three having equal chance.
       private void turnRandom() {
       /* to be implemented in part (a) */ }
       
       // Return the next location in front of the fish, or the fish's
       // current location if that location is blocked.
       protected Location nextLocation() {
       /* to be implemented in part (b) */ }
       
       // Either move the fish forward, or, if that space is blocked,
       // turn a random direction, leaving it at the same location.
       protected void move() {
       /* to be implemented in part (c) */ }
    }

    1. Write the turnRandom() method for the StraightFish class. This method should turn the fish randomly to face in one of the three directions that it was not facing before, with each direction being equally probable.

      In writing turnRandom(), you may use any of the accessible methods from any of the classes in the case study.

      Complete the method turnRandom() below.

      // Turn the fish toward one of the three directions that is not
      // how it is facing now, with all three having equal chance.
      private void turnRandom() {

    2. Override the nextLocation() method for the StraightFish class. This method should return either the location directly in front of the fish, if that space is empty, or the fish's current location if that space is blocked.

      In writing nextLocation(), you may use any of the accessible methods from any of the classes in the case study.

      Complete the method nextLocation() below.

      // Return the next location in front of the fish, or the fish's
      // current location if that location is blocked.
      protected Location nextLocation() {

    3. Override the move() method for the StraightFish class. This method either move the fish forward one space, of, if that space is blocked, turn it randomly toward another direction.

      In writing move(), you may call the turnRandom() and nextLocation() methods. Assume that these methods work as described, regardless of what you wrote in the previous parts. You may also use any of the accessible methods from any of the classes in the case study.

      Complete the method move() below.

      // Either move the fish forward, or, if that space is blocked,
      // turn a random direction, leaving it at the same location.
      protected void move() {

  2. The following code segment is meant to tell a fish to move 100 times, counting up how many times it turned left.

    int count = 0;

    for(int k = 0; k < 100; k++) {
       Direction dir = fish.direction();
       Direction dirLeft = dir.toLeft();
       
       fish.act();
       
       if( /* conditition */ ) {
          count++;
       }
    }

    Which of the following would work as the condition?
    1. dir.equals(dirLeft)
    2. dir.equals(fish.direction())
    3. dirLeft.equals(dir)
    4. dirLeft.equals(fish.direction())
    5. (fish.direction()).equals(new Direction(dir))

          
          
          
          
          
          
  3. A fish is at (2, 2) in the environment shown to the left, facing north. What location will be returned by the following code?

public Location next(Fish fish) {
   Environment env = fish.environment();
   Location loc = fish.location();
   Direction dir = fish.direction();
   loc = env.getNeighbor(loc, dir);
   loc = env.getNeighbor(loc, dir.toRight());
   return env.getNeighbor(loc, dir);
}


  1. The code below is for a Fish subclass called Snail:

    public class Snail extends Fish {
       public Snail(Environment env, Location loc) {
          super(env, loc);
       }
       
       public boolean breed() {
          return false;
       }
       
       public void die() {
       }
       
       public Location nextLocation() {
          ArrayList a = emptyNeighbors();
          for(int i = 0; i < a.size(); i++) {
             Location loc = (Location)a.get(i);
             if(!environment().isEmpty(loc) ||
                   environment().neighborsOf(loc).size() == 4) {
                a.remove(i);
                i--;
             }
          }
          if(a.size() == 0) {
             return location();
          }
          Random r = RandomNumberGenerator.getInstance();
          return a.get(r.nextInt(a.size()));
       }
    }

    Describe the behavior of the Snail.