Question: Current code: //Animal.java public interface Animal { public String getSound(); public String getType(); } //Chick.java public class Chick implements Animal { private String myType; private

Current code:

//Animal.java public interface Animal { public String getSound(); public String getType(); }
//Chick.java public class Chick implements Animal { private String myType; private String mySound; Chick() { this.myType = "Chick"; this.mySound = "Cheap"; } @Override public String getSound() { return this.mySound; } @Override public String getType() { return this.myType; } } 
//Cow.java public class Cow implements Animal { private String myType; private String mySound; public Cow() { myType = "Cow"; mySound = "Moo"; } @Override public String getSound() { return this.mySound; } @Override public String getType() { return this.myType; } }
//Dog.java public class Dog implements Animal{ private String myType; private String mySound; Dog() { this.myType = "Dog"; this.mySound = "Oink"; } @Override public String getSound() { return this.mySound; } @Override public String getType() { return this.myType; } }
//NamedCow.java public class NamedCow extends Cow { private String Name; NamedCow(String name) { super(); this.Name = name; } public String getName() { return Name; } } 
//Farm.java public class Farm { private ArrayList animals; Farm() { animals = new ArrayList(); } public void addAnimal(Animal animal) { animals.add(animal); } public void removeAnimal(Animal animal) { animals.remove(animal); } @Override public String toString() { String str=""; for(Animal animal:this.animals) { if(animal instanceof NamedCow) { NamedCow namedcow = (NamedCow)animal; str+="Name = "+namedcow.getName()+"; Type= "+namedcow.getType()+"; Sound = "+namedcow.getSound()+"; "; } else { str+= "Type = "+animal.getType()+";Sound = "+animal.getSound()+"; "; } } return str; } }
//OldMacDonald.java this is the tester class public class OldMacDonald { public static void main(String[] args) { // TODO code application logic here NamedCow cow1 = new NamedCow("Cow 1"); Cow cow2 = new Cow(); Dog dog = new Dog(); Chick chick = new Chick(); Farm farm = new Farm(); farm.addAnimal(cow1); farm.addAnimal(cow2); farm.addAnimal(dog); farm.addAnimal(chick); System.out.println(farm); } }

needed:

Current code: //Animal.java public interface Animal { public String getSound(); public StringgetType(); } //Chick.java public class Chick implements Animal { private String myType;private String mySound; Chick() { this.myType = "Chick"; this.mySound = "Cheap"; }@Override public String getSound() { return this.mySound; } @Override public String getType()

Background: In this lab, we will use the well-known song 'Old MacDonald Had a Farm' to learn about Inheritance and Polymorphism Old MacDonald had a farm and several types of animals. Every animal shared certain characteristics. They each had a type (such as cow, chick or dog) and each made a sound (moo, cheap or oink). An Interface defines those things required to be an animal on the farm. public interface Animal { public String getSound(); public String getType(); ) 3. Create a complete farm to test all your animals. Here is the Farm.java source code. public class Farm { private Animal [] myFarm; public Farm() { myFarm = new Animal (3]; myFarm[0] = new Cow(); myFarm[1] = new Chick(); myFarm[2] = new Dog(); } public void animalSounds() { Animal temp; for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!