Question: I need help with a java assignment: For this assignment, we are going to simulate a aquarium that contains fish. One rule about aquariums is
I need help with a java assignment: For this assignment, we are going to simulate a aquarium that contains fish. One rule about aquariums is that there is a limit to how many fish it can hold due to oxygen consumption. If you add too many, there won't be enough oxygen and the fish will die. So, the careful aquarium owner will keep track of the fish added to the tank so as to maintain a healthy environment for the fish. You'll write a application that helps aquarium owners determine how many fish can be added to a tank. Here's a UML diagram of the classes needed to solve the problem:
Fish
Fish will be an abstract class. It's meant to model the common characteristics of all fish. It's a class we will never create an instance of, but it is useful as it defines the attributes and behavior for all subclasses of fish. In this model, we are stating that all fish swim in a zone, and have some amount of oxygen consumption.
zone is an integer representing the zone of the aquarium where the fish spends most of it's time. The zones are defined as follows: top = 1, middle = 2, bottom = 3.
There is one constructor, that takes an integer representing the zone and saves it in the instance variable named zone.
There is a getter method for zone variable.
There is an abstract method (shown in italics in UML) named getOxygenConsumption(), that returns the oxygen content needed by the fish.
There is an abstract method (shown in italics in UML) named swim() that returns a String representing how the fish swims.
The two abstract methods are "instructions" to subclasses. Any subclass that we want to create an instance of will have to define how/where they swim, and how much oxygen they require.
Note that the abstract class Fish implements the Comparable interface. This interface is part of the Java API and does not have to be created in this assignment. Since Fish implements Comparable, it means that it must provide a concrete implementation of the compareTo() method. We will use it to sort the fish by zone. The compareTo() method will be analogous to the ComparableRectangle in listing 13.9, except we will order by zone, instead of area.
subclasses of Fish
default constructor: for all subclasses are simple and contain just one line of code: a call to the super class constructor, passing a integer representing the zone they swim in. There are no other constructors for subclasses of Fish.
Danio's swim at the top, so it should forward a 1 to the superclass constructor.
Tetra's swim in the middle, so it should forward a 2 to the superclass constructor.
Cory's swim at the bottom, so it should forward a 3 to the superlcass constructor.
getOxygenConsumption(): This method just returns an integer representing how much oxygen the fish requires. It could be the result of a calculation, but we'll keep it simple and return an integer as follows:
Danios: return 12.
Tetras: return 10;
Corys: return 18;
swim(): This method describes how a particular type of fish swim. For our model, we'll return a string in each sublcass as follows:
Danios: return "Danio darting".
Tetras: return "Tetra gliding".
Corys: return Cory just hanging out".
Aquarium
This class contains our fish. To hold our fish, you will use an ArrayList with the concrete type set to Fish (as shown in UML). ArrayLists allow us to add items to the list without worrying if we are going back the end of the list, as it resizes the list for us. And, it is part of the Collections framework, which allows us to do things like sort the ArrayList (assuming we implement comparable).
You should have a instance variable that represents the oxygen content of the tank. This will be based on the surface area of the tank.
Constructor takes two arguments, the width and the length of the surface area. The constructor should use these two variables to calculate the surface area, and save it in the capacity instance variable.
add() method takes one argument of type: Fish. That means any instance of Fish can be used here, such as Danio, Cory, or Tetra. This method should not add the fish if doing so will use up any remaining oxygen. In other words, Fish can be added to the tank, so long as the oxygen consumption of the new Fish, plus the existing Fish, do not exceed the capacity of the tank. If the new Fish can be added, it should be added to the ArrayList, and the method should return true. If the new Fish cannot be added, then the method should return false.
getFish() is a getter for the arraylist.
getNumberOfFish() should return the number of fish in the tank. (This is determined by how many fish are in your array list)
empty() removes all Fish. This is done by removing all Fish from the array list. Use the appropriate method in the array list to do this -- no loop is required.
watch(): This method will first sort the array list. It will then use a foreach loop to loop through the list of Fish and do the following:
For each Fish object, it will call the swim() method. The Fish will return a String stating how they swim.
It should then print this string, one String per line.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
