Objectives To understand inheritance To understand polymorph

Objectives:

To understand inheritance To understand polymorphism


Instructions For this project, you will implement a program that creates objects of the following Animal types

Dog

Cat

Chicken
All three of these concrete classes will extend a single Animal class (which should be abstract.)

Animal class

The Animal abstract superclass should provide the following methods:

1. getPrefix - Concrete method. Returns a String. Takes no parameters.
This method should return a String that says, “I am a” – it’s very simple


2. getAnimalType - Abstract method. Returns a String. Takes no parameters.
This method will be implemented by the subclasses.

3.getName Concrete method. Returns a String. Takes no parameters.
This method will return the name of the animal, stored in a private field named name, of type String

4. setName - Concrete method. Return type is void. Takes one parameter, a String (the name to be set)
This method sets the private field name to a parameter, of type String

5.makeNoise - Abstract method. Return type is void. Takes no parameters.
This method will be implemented by the subclasses.

Dog class

This concrete subclass of the Animal superclass should provide the following methods (in addition, or as implementations of the inherited abstract methods.)

1. getAnimalType - Implementation of the inherited abstract method. Returns a String. Takes no parameters.
Returns ”Dog”


2. makeNoise - Implementation of the inherited abstract method. Return type is void. Takes no parameters.
For the Dog class, this should print out, “Woof! Woof!”


3. doDoggyThings - Return type is void. Takes no parameters.
Prints out, “I’m digging a hole, and burying my bone!”

Cat class

1. getAnimalType- Implementation of the inherited abstract method. Returns a String. Takes no parameters.
Returns “Cat

2. makeNoise - Implementation of the inherited abstract method. Return type is void. Takes no parameters.
For the Dog class, this should print out, “Meow! Meow!”

3. doKittyThings- Return type is void. Takes no parameters.
Prints out, “I’m tearing up your couch with my claws, and leaving dead mice in places

The Client (Driver) Class

Provide a driver class, named AnimalTester that will test each of the aforementioned subclasses and their methods.

You should do the following in your main method in order to test them:
1 .Create a polymorphic reference to each of the two Animal subclasses o Hint: Cat cat = new Cat() is not a polymorphic reference

2. Test the implementations of all the inherited methods through the polymorphic reference directly and show that they work correctly

3. To test the subclass-specific methods (e.g., doDoggyThings), you need to cast the polymorphic reference to the appropriate subclass type



Solution

abstract class Animal{
    private String name;
    public String getPrefix(){
        return \"I am a \";
    }

    abstract String getAnimalType();

    String getName(){
        return this.name;
    }

    void setName(String name){
        this.name = name;
    }

    abstract void makeNoise();
}

class Dog extends Animal{
    String getAnimalType(){
        return \"Dog\";
    }

    void makeNoise(){
        System.out.println(\"Woof! Woof!\");
    }

    void doDoggyThings(){
        System.out.println(\"I am digging a hole, and burying my bone!\");
    }
}

class Cat extends Animal{
    String getAnimalType(){
        return \"Cat\";
    }

    void makeNoise(){
        System.out.println(\"Meow! Meow!\");
    }

    void doKittyThings(){
        System.out.println(\"I’m tearing up your couch with my claws, and leaving dead mice in places.\");
    }
}

class Client{
    public static void main(String[] args) {
        Animal cat = new Cat();
        Animal dog = new Dog();
        cat.setName(\"cat\");
        System.out.println(cat.getPrefix());
        System.out.println(cat.getName());
        dog.setName(\"dog\");
        System.out.println(dog.getPrefix());
        System.out.println(dog.getName());
        Cat cat2 = (Cat)cat;
        Dog dog2 = (Dog)dog;
        cat2.makeNoise();
        cat2.doKittyThings();
        cat2.getAnimalType();
        dog2.makeNoise();
        dog2.doDoggyThings();
        dog2.getAnimalType();
    }
}

Objectives: To understand inheritance To understand polymorphism Instructions For this project, you will implement a program that creates objects of the followi
Objectives: To understand inheritance To understand polymorphism Instructions For this project, you will implement a program that creates objects of the followi
Objectives: To understand inheritance To understand polymorphism Instructions For this project, you will implement a program that creates objects of the followi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site