Lab03: Inheritance

Objectives

Setup

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 Aquatic Life

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

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.

The Shark Class

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):

  1. Declare the class to be a subclass of Fish.
  2. A constructor must be written for the class. The constructor should take in only the name. It should call the Fish constructor, passing in the name and 0 for the size. The size is 0 because we won't be using it.
  3. Override the eat() method from the Fish class. The overriding method should simply call eatenBy() and return true.
  4. Override the eatenBy() method from the Fish class. It should simply return false, since sharks cannot be eaten.

The BlowFish Class

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):

  1. Declare the class to be a subclass of Fish.
  2. Write the constructor. Like the Shark class, the constructor should only take in the name, but should use 10 as the size.
  3. Override the eatenBy() method. It should set the other fish to be dead, call the parent version of eatenBy(), and then return true.

Testing Your Work

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.

Turning In Your Lab

To turn in your lab:

$ cd ~/cs190m/lab03
$ turnin -v -c cs190m -p lab03 *.java

Getting Help

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:

  1. Your textbook
  2. Lecture notes
  3. Java API documentation
  4. Your lab TA

Lab created by: Daniel Tang