Aquarium free response practice question

Some of the AP Computer Science Free Response Problems require you to manipulate an array or ArrayList in a way that is more complex than just looping through the entire data structure. You may also be required to manipulate the elements inside the structure. This problem is intended as practice with commonly tested manipulations.

Problem description

An aquarium stores information about the fish in its tanks, about the tanks themselves, and about the position of the tanks relative to each other. Each subclass of the Fish class stores information about the minimum and maximum temperatures that a fish can tolerate as well as other fish with which it is compatible. The declaration of the Fish class is shown below.

public abstract class Fish
{
 /** @return the minimum temperature the fish can tolerate
  *  Postcondition: minTemp() <= maxTemp() */
 abstract int minTemp();

 /** @return the maximum temperature the fish can tolerate
  *  Postcondition: maxTemp() >= minTemp() */
 abstract int maxTemp();

 /** @return true if the fish is compatible with other, false otherwise */
 abstract boolean isCompatible(Fish other);

 // There may be variables, constructors, and methods that are not shown.
}

The Tank class stores the temperature of the tank and the list of fish in the tank. The declaration of the Tank class is shown below.

public class Tank
{
 /** @return a list of all fish in the tank or an empty list
  *          if there are no fish in the tank */
 public List<Fish> getFish()
 {  /* implementation not shown */ }

 /** @return the temperature of the tank */
 public int temp()
 {  /* implementation not shown */ }

 /** Adds fishy to the tank
  *  @param fishy the fish to add */
 public void addFish(Fish fishy)
 {  /* implementation not shown */ }

 // There may be variables, constructors, and methods that are not shown.
}

The Aquarium class stores all tanks in the aquarium. The declaration of the Aquarium class is shown below. You will write several methods of the Aquarium class.

public class Aquarium
{
 /** The list of all tanks in the aquarium; guaranteed never to be null */
 private List<Tank> tanks;

 /** Returns a tank in the aquarium with a temperature fishy can tolerate and
  *  that does not contain a fish that is not compatible with fishy. Returns
  *  null if there is no such tank in the aquarium.
  *  @param fishy the fish to be checked
  *  @return a suitable tank for fishy or null if no such tank exists */
 private Tank findTank(Fish fishy)
 {  /* to be implemented in part (a) */ }

 /** Adds each fish in fishes to a suitable tank in the aquarium if such a
  *  tank exists. Each fish should be added to at most 1 tank.
  *  @param fishes the list of fish to add to the aquarium
  *  @return a list of the fish in fishes that could not be added
  *  Postcondition: all other fish remain in the same tanks */
 public List<Fish> addFish(List<Fish> fishes)
 {  /* to be implemented in part (b) */ }

 /** Adds fishTank to the aquarium if a suitable position can be found. The
  *  temperature of fishTank can be no more than 5 degrees different (lower or
  *  higher) than each of any adjacent tanks.
  *  @param fishTank the tank to add to the aquarium
  *  @return true if fishTank was added, false otherwise
  *  Postcondition: the order of the other tanks in the aquarium relative to each other is not changed */
 public boolean addTank(Tank fishTank)
 {  /* to be implemented in part (c) */ }

 // There may be instance variables, constructors, and methods that are not shown.
}

Part (a)

Method findTank finds and returns a tank in the aquarium with a temperature that fishy can tolerate and that does not contain a fish that is not compatible with fishy. If there is more than one such tank, any of them may be returned. If there is no such tank, the method returns null.

For Fish f1 and Fish f2: f1.isCompatible(f2) == f2.isCompatible(f1)

Complete method findTank below.

 /** Returns a tank in the aquarium with a temperature fishy can tolerate and
  *  that does not contain a fish that is not compatible with fishy. Returns
  *  null if there is no such tank in the aquarium.
  *  @param fishy the fish to be checked
  *  @return a suitable tank for fishy or null if no such tank exists */
 private Tank findTank(Fish fishy)

Part (b)

Method addFish adds each fish in fishes to a suitable tank in the aquarium if such a tank exists. The method returns a list of fish from fishes for which no suitable tank exists. Each fish should be added to at most one tank.

Assume that findTank works as specified regardless of what you wrote in part (a).

Complete method addFish below.

 /** Adds each fish in fishes to a suitable tank in the aquarium if such a
  *  tank exists. Each fish should be added to at most 1 tank.
  *  @param fishes the list of fish to add to the aquarium
  *  @return a list of the fish in fishes that could not be added
  *  Postcondition: all other fish remain in the same tanks */
 public List<Fish> addFish(List<Fish> fishes)

Part (c)

Method addTank adds fishTank to the aquarium if a suitable position can be found. The temperature of fishTank can be no more than 5 degrees different (lower or higher) than each of any adjacent tanks. The aquarium may contain zero or more tanks. The method returns true if fishTank was successfully added, false otherwise. The positions of the tanks already in the aquarium relative to each other should not be changed.

Complete method addTank below.

 /** Adds fishTank to the aquarium if a suitable position can be found. The
  *  temperature of fishTank can be no more than 5 degrees different (lower or
  *  higher) than each of any adjacent tanks.
  *  @param fishTank the tank to add to the aquarium
  *  @return true if fishTank was added, false otherwise
  *  Postcondition: the order of the other tanks in the aquarium relative to each other is not changed */
 public boolean addTank(Tank fishTank)

See the Aquarium solution or review it with AP CS Tutor Brandon Horn.

Get AP CS Help