Programming Assignment - Chapter 8



This is a very short and easy assignment intended to reinforce your understanding of late binding and polymorphism. First, a very brief (and incorrect) Biology lesson.

All living things are "Organisms". An Organism has a name (like "Joe" or "Sue"). Organsims are divided up into "Aves" (birds) or "Mammals". Aves lay eggs and Mammals are live bearing. Aves are further divided up into "Duck" and "Canary" . A Duck eats seaweed and says "quack". A Canary eats thistle and says "tweet". Mammals are divided up into Human and Dog. A Human eats hamburger and says "hello" , a Dog eats shoes and says "bark".

What to do:

Implement a Class Organism with a single instance varibale called "name" (String). Extend Organism to classes Aves and Mammal. Aves and Mammal should have an instance variable called "repro_method" (String). Extend class Aves to classes Duck and Canary, and extend class Mammal to classes Human and Dog. Duck, Canary, Human, and Dog should all have instance variables called "eats" (String) and "greets" (String). NOTE that every instance variable except the name (contained in Organism) is CONSTANT. Make sure to program it as such. Write all the appropriate constructors, toString methods, etc. Your grade will reflect how well you have implemented ideas presented in chapter 8.

Test your code on the following driver program:

public class Zoo
{
    public static void main(String[] args)
    {
        Organism[] zoo=new Organism[4];
        zoo[0]=new Duck("Donald");
        zoo[1]=new Human("Charlie Brown");
        zoo[2]=new Dog("Lassie");
        zoo[3]=new Canary("Tweety");
        for(int i=0;i<3;i++)
            System.out.println(zoo[i]);
    }
}

You MUST match the following output:


 Name: Donald
 Reproductive Method: Lays Eggs
 Eats: Seaweed
 Says: Quack

 Name: Charlie Brown
 Reproductive Method: Live Bearing
 Eats: Hamburger
 Says: Hello

 Name: Lassie
 Reproductive Method: Live Bearing
 Eats: Shoes
 Says: Bark

Name: Tweety
Reproductive Method: Lays Eggs
Eats: Thistle
Says: Tweet