COP3330-02 Object Oriented Programming

 

Spring 2003

Program 4 (Zoo)

 

Due:  March 27, 2003

 

Objectives:

The objectives of this assignment are to get hands-on experience:

 

Overview:

You will create a hierarchy of animals that belong to a zoo.  The superclass of all your animals, ZooAnimal, has been created for you.  In addition, a subclass of ZooAnimal called Bird has also been provided.  You should use Bird as an example when coding your animal classes.  Be careful though; not all animals are exactly like Birds.  See below for the specific state and behavior that should belong to each of the animal classes you create.

 

The ZooAnimal Hierarchy:

 

Specifics:

 

Class:   ZooAnimal (provided for you)

 

The root of our hierarchy is the class ZooAnimal.  You are to download this class and use it as is.  Listed below are the fields and methods of the ZooAnimal class.

 

Fields:

 

The name of this ZooAnimal.

protected String name;

 

A random number generator with a predetermined seed.  This should be used in your isHungry methods.

private Random random;

 

Methods:

 

Constructor.  This method should be reused by your subclasses.  (Use Bird’s constructor as an example of reusing the superclass constructor).

public ZooAnimal(String name)

 

Returns this ZooAnimal’s name.

public getName()

 

Returns the next random number from random (see above field).

protected double nextRandom()

 

Returns true if this ZooAnimal is currently hungry.  Subclasses must override this.

public abstract boolean isHungry()

 

Prints to the screen the fact that this ZooAnimal is eating.

public void eat()

 

Interface:  Flyer (provided for you)

 

All classes that can fly should implement the Flyer interface.  You are to download this interface and use it as is.  Listed below are the abstract methods of the Flyer class.

 

Methods:

 

Print to the screen the fact that the implementing object is flying.  This method may or may not adjust the altitude of the implementing object. 

void fly()

 

Returns the current altitude of the implementing object.

int getAltitude()

 

Class:  Bird (provided for you)

 

Subclasses of the abstract Bird class are animals that can fly.  You are to download this class and use it as is.  Listed below are the fields and methods of the Bird class.

 

Fields:

 

The current altitude of this Bird.

protected int altitude;

 

Methods:

 

Constructor.  Reuses constructor of superclass.

public Bird(String name)

 

Overridden abstract method of the superclass ZooAnimal.  Birds are hungry 40% of the time.  Therefore, isHungry returns true is the random number x that is generated is 0 <= x < 0.40.  You should use this as an example for your isHungry methods.

public boolean isHungry

 

Prints to the screen the fact that this Bird is flying and adjusts this Bird’s altitude.

public void fly

 

Returns the altitude of this Bird.

public int getAltitude

 

Class:  ZooAnimalTester (provided for you)

 

You are to download this class and use it as is.  This class has only a main method.  Use it to test your hierarchy after you’ve completed coding it.

 

Example (correct) output.  If you don’t have this, something is wrong:

 

Patrick is eating

 

 

Below are the classes that you must write.

 

Class:  Eagle

 

 

Class:  Mammal

 

 

Class:  Platypus

 

 

Class:  Bat

 

 

Class:  Fish

 

 

Class:  Shark

 

 

Interface:  Swimmer

 

 

Class:  ZooAnimalTester2

 

This class has only a main method.  In the main method, you are to create an array that holds ZooAnimals.  Do not declare your array of type Flyer.  In the array, put four Flyers:  two Eagles and two Bats, in that order.  Give them the names Billy, Susan, Mark, and Julie, in that order.  Iterate over the array, downcasting each to Flyer and then calling its fly method.  Call its getAltitude method and print this altitude to the screen also.

 

Example (correct) output.  If you don’t have this, something is wrong:

 

Billy is flying

Altitude: 1

Susan is flying

Altitude: 1

Mark is flying

Altitude: 3

Julie is flying

Altitude: 3

 

Summary:

 

Submittal:

Submittals should follow the program specifications of the class.

 

Write-up:

There is no write-up required for this program.