Write an Animal class that has two member variables called weight and height. It
ID: 3883350 • Letter: W
Question
Write an Animal class that has two member variables called weight and height. It should have two member methods called makeNoise and eat. Both of these methods should only have a print statement saying what that method is. Make the Animals comparable using the weight as the deciding factor of who is bigger. Have setters for both member variables. Write 3 more classes called Dog, Cat and Pig. Each of these classes should have a method called makeNoise. The makeNoise of Dog should have a print statement saying Bark. The makeNoise of Cat should have a print statement saying MEOW. The makeNoist of pig should have a print statement saying OUNK. In main class create two objects of type Cat, two objects of type Dog and two objects of type Pig. Set the height and weight of each one as you like. Create an array of type Animal and save all these 6 objects in this array. Name this array arrayA. Write a for loop in main that repeats 6 times and each time call the method makeNoise by having arrayA[i].makeNoise(). You will see that the same line makes different sounds depending what object is stored in that location of the array. We call this Polymorphism. So you have written a polymorphic code.Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Animal implements Comparable<Animal>
{
/* two member variables */
private double weight;
private double height;
/* Two member methods */
public void makeNoise()
{
System.out.println("Make noise");
}
public void eat()
{
System.out.println("eat");
}
/*Setter methods for 2 member variables*/
public void setWeight(double wt)
{
this.weight=wt;
}
public void setHeight(double ht)
{
this.height=ht;
}
/*making animals comparable */
public int compareTo(Animal o)
{
return Double.compare(this.weight, o.weight);
}
}
/* Part 1 ends here, part 2 starts */
class Dog extends Animal
{
public void makeNoise()
{
System.out.println("Bark");
}
}
class Cat extends Animal
{
public void makeNoise()
{
System.out.println("Meow");
}
}
class Pig extends Animal
{
public void makeNoise()
{
System.out.println("Ounk");
}
}
/*Part 2 ends here, part 3 starts*/
/* Main Class - i named it as fin21, give the same name to the program file*/
public class fin21
{
public static void main (String[] args) throws java.lang.Exception
{
Cat c1=new Cat();
Cat c2=new Cat();
Dog d1=new Dog();
Dog d2=new Dog();
Pig p1=new Pig();
Pig p2=new Pig();
c1.setWeight(3.8);
c1.setHeight(25.1);
c2.setWeight(4.2);
c2.setHeight(24.3);
d1.setWeight(50.5);
d1.setHeight(69.7);
d2.setWeight(55.7);
d2.setHeight(71.1);
p1.setWeight(40.2);
p1.setHeight(20.1);
p2.setWeight(40.1);
p2.setHeight(19.1);
Animal[] arrayA=new Animal[6];
arrayA[0]=c1;
arrayA[1]=c2;
arrayA[2]=d1;
arrayA[3]=d2;
arrayA[4]=p1;
arrayA[5]=p2;
/*Part 4*/
for(int i=0;i<6;i++)
{
arrayA[i].makeNoise();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.