First, create a lab03 directory:
$ cd cs190m $ mkdir lab03 $ cd lab03
Then, download Lab03.java and Fish.java into the folder you just created.
The ocean is host to an abundance of life. Aquatic residents can form different kinds of hierarchies, but we'll be modelling a simplified hierarchy of fish in an oceanic environment using inheritance.
The Fish class represents a generic fish in the sea. Fish that don't have any particular outstanding attributes can be represented by this class. In our environment, since there are only fish in our sea, the Fish class is the highest class in our class tree (besides Object, of course). Our fish have three primary attributes that we'll be using, which are declared in the Fish.java file for you. They are the name, size, and whether or not the Fish is alive.
Your first task is to write a constructor that has a name and size argument and assigns them to the provided variables. It should also make sure that the fish is alive.
Your next task is to write some accessors and mutators (you should know what these are from class). Fill in the method stubs for the three accessors and one mutator.
Note: The accessor and mutator methods should be one line. If you have more, than you're probably doing something wrong.
Write the eat() method. The logic is as follows:
Write the eatenBy() method. As you can see from the logic above, this method should be called when a fish is being eaten by another fish. For the Fish class, simply set isAlive to false and return true. This is because normal fish have no defense mechanisms.
Sharks are often considered to be apex predators, which essentially means they're at the top of the food chain. Given this, we can assume that sharks can successfully attack any prey except other sharks. However, in our lab, we'll assume that our tank only has one shark. There are four tasks for this file (first, create it):
Blowfish are a highly poisonous type of fish. Thus, even though a blowfish might be eaten, the eating fish will always die. There are three tasks for this class (first, create it):
A driver is provided for you in Lab03.java. Simply run it, and it will provide a simulation of your fish tank. If you're curious as to how it works, feel free to read through comments at your own leisure.
To turn in your lab:
$ cd ~/cs190m/lab03 $ turnin -v -c cs190m -p lab03 *.java
Many of you will probably run across problems while programming at some point during a lab. If that's the case, here are the resources you should use, in order:
Lab created by: Daniel Tang