Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class Zoo { public static void main(String[] args) { Animal one = new Ani

ID: 3641778 • Letter: P

Question

public class Zoo {

       public static void main(String[] args) {

              Animal> new Animal("Wilder", "black");

              Dog two = new Dog("Fido", "brown", "woof!");

              Cat three = new Cat("Kitty", "white", "meow!");

              one.greet();

              two.greet();

              three.greet();

       }

}

public class Animal {

       public String name;

       public String color;

       public Animal(String name, String color) {

              this.name = name;

              this.color = color;

       }

       public void greet() {

              System.out.println("I am an Animal. My name is " + name + " and my color is " + color);

       }

}

public class Dog extends Animal {

       public String greeting;

       public Dog(String name, String color, String greeting) {

              super(name,color);

              this.greeting = greeting;

       }

       public void greet() {

              System.out.println(greeting + " My name is " + name + " and my color is " + color);

       }

}

public class Cat extends Animal {

       String greeting;

       public Cat(String name, String color, String greeting) {

              super(name, color);

              this.greeting = greeting;

       }

       public void greet() {

              System.out.println(greeting + " My name is " + name + " and my color is " + color);

       }

}

Using polymorphism Modify your zoo program (from class assignment 14) to create an array of five Animals. Three of the them should be dogs and two should be cats. Use polymorphism to invoke 'greet()' method for all these 5 animals in a loop. Using interfaces: You now want dogs to also exhibit 'pet' behavior. However, you cannot assign 'pet' behavior to Animal class because not all animals can be pets. If you create a Pet class, Dog cannot inherit from it because Java doesn't allow multiple inheritance. So you need to create an interface called Pet that has a method called makePet()? . This method should print a statement Please make me your pet!' Change Dog class to implement this interface so that, whenever required, its 'Pet' behavior can be invoked

Explanation / Answer

//Here is the Zoo class rewritten to use the Polymorphism array

public class Zoo {

       public static void main(String[] args) {

              Animal arr[] = {new Dog("Joe","Brown","woof!"), new Dog("Phil","White","bark!"),

                           new Dog("Jeff","Yellow","arf!"), new Cat("Kitty","Blue","meow!"),

                           new Cat("Max","Black","purr!")};

              for(int i = 0; i < arr.length; ++i) {

                     arr[i].greet();

              }

       }

}

//Here is the pet interface

public interface Pet {

       public void makePet();

}

//Here is the Dog class rewritten to implement the Pet interface

public class Dog extends Animal implements Pet {

       public String greeting;

       public Dog(String name, String color, String greeting) {

              super(name,color);

              this.greeting = greeting;

       }

       public void greet() {

              System.out.println(greeting + " My name is " + name + " and my color is " + color);

       }

       public void makePet() {

              System.out.println("Please make me your pet!");

       }

}