Need help with this JAVA practice problem You run a zoo whic

 Need help with this JAVA practice problem  You run a zoo which contains Animals. So each Animal will be represented  by an abstract class.  Consider creating classes Animal, Cow, Horse, Snake, etc.   Your Animal class will have the following instance variables:     private String name;     private double weight;     private int age;      Your Animal class will have the following constructors:         Animal()     Animal(String n, double weight, int age) Your Animal class will have the following methods:      String makeNoise() - this will be an abstract method double getWeight() - returns the weight of this animal      public String toString() - returns a String with information about this Animal ***************************************  You will have the following classes which all extend Animal: Cow, Horse, Snake.  Cow will have an instance variable called: private int num_spots All variables should be initialized after either of the 2 constructors: Cow() Cow(String name, double weight, int age, int num_spots)  You will have the methods  String makeNoise() which returns \"Moooo\" toString() - returns info about all variables including Animal things ********************************************** Horse will have an instance variable called: private double top_speed All variables should be initialized after either of the 2 constructors: Horse() Horse(String name, double weight, int age, double top_speed)  You will have the methods  String makeNoise() which returns \"Whinny\" toString() - returns info about all variables including Animal things ********************************************** Snake will have an instance variable called: private int num_fangs All variables should be initialized after either of the 2 constructors: Snake() Snake(String name, double weight, int age, int num_fangs)  You will have the methods  String makeNoise() which returns \"Hisssssss\" toString() - returns info about all variables including Animal things ********************************************** You will have a Zoo class that contains the following instance variables:      private int actual_num_animals;     private int num_cages;     private Animal[] animals;  You will have the constructors: Zoo() - default num_cages will be 3 Zoo(int num_cages)  You will have the following methods:       void add(Animal a) - adds an animal to your Zoo     double total_weight() - returns the total weight of all animals in the zoo void make_all_noises() - Print out the noises made by all of the animals.           In otherwords, it calls the makeNoise() method for all animals in the zoo. void print_all_animals() - prints the results of calling toString() on all animals in the zoo.   ************************************************ Generate output by running the following main in the  Zoo class:  The results should go into the JH6_worksheet.txt   public static void main(String[] args)     {         Zoo z = new Zoo();         Snake sly = new Snake(\"Sly\", 5.0 , 2, 2);         Snake sly2 = new Snake(\"Slyme\", 10.0 , 1, 2);         Cow blossy = new Cow(\"Blossy\", 900., 5,  10);         Horse prince = new Horse(\"Prince\", 1000., 5, 23.2);          // Following not allowed because Animal is abstract         //Animal spot = new Animal(\"Spot\", 10., 4);                  z.add(sly);         z.add(sly2);         z.add(blossy);         z.add(prince);                  z.make_all_noises();         System.out.println(\"Total weight =\" + z.total_weight());         System.out.println(\"**************************\");         System.out.println(\"Animal Printout:\");         z.print_all_animals();            System.out.println(\"********* Now we will make the Second Zoo\");         Zoo z2 = new Zoo(5);         z2.add(sly);         z2.add(sly2);         z2.add(blossy);         z2.add(prince);         z2.add( new Horse(\"Warrior\", 1200, 6, 25.3));         z2.add( new Horse(\"Harry\", 1100, 4, 21.3));         System.out.println(\"Total weight of z2=\"+z2.total_weight());         z2.make_all_noises();         z2.print_all_animals();              } 

Solution

Animal.java

public abstract class Animal {
   private String name;
private double weight;
private int age;
public Animal(){
   name=\"\";
   weight=0;
   age=0;
}
public Animal(String n, double weight, int age){
   this.name=n;
   this.weight=weight;
   this.age=age;
}
   public double getWeight() {
       return weight;
   }
   public abstract String makeNoise();
   @Override
   public String toString() {
       return \"name=\" + name + \", weight=\" + weight + \", age=\" + age;
   }
  
}

----------------

Cow.java

----------------

public class Cow extends Animal {
   private int num_spots;

   public Cow(){
       super();
       num_spots=0;
   }
   public Cow(String name, double weight, int age, int num_spots){
       super(name, weight, age);
       this.num_spots=num_spots;
   }

   @Override
   public String makeNoise() {
       // TODO Auto-generated method stub
       return \"Moooo\";
   }
   @Override
   public String toString() {
       return super.toString()+\"num_spots=\" + num_spots;
   }

}

----------------

Horse.java

-----------------

public class Horse extends Animal {
   private double top_speed;
   public Horse(){
       top_speed=0;
   }
   public Horse(String name, double weight, int age, double top_speed){
       super(name, weight, age);
       this.top_speed=top_speed;
   }
   @Override
   public String makeNoise() {
       // TODO Auto-generated method stub
       return \"Whinny\";
   }
   @Override
   public String toString() {
       return super.toString()+\",top_speed=\" + top_speed;
   }

}

------------------

Snake.java

-----------------

public class Snake extends Animal {
   private int num_fangs;
   public Snake(){
       num_fangs=0;
   }
   public Snake(String name, double weight, int age, int num_fangs){
       super(name, weight, age);
       this.num_fangs=num_fangs;
   }

   @Override
   public String makeNoise() {
       // TODO Auto-generated method stub
       return \"Hisssssss\";
   }
   @Override
   public String toString() {
       return super.toString()+\",num_fangs=\" + num_fangs;
   }

}

------------------

Zoo.java

------------------

public class Zoo {
      private int actual_num_animals;
   private int num_cages;
   private Animal[] animals;

   public Zoo(){
       this.num_cages=3;
       actual_num_animals=0;
       animals=new Animal[num_cages+1];
   }
   public Zoo(int num_cages){
       this.num_cages=num_cages;
       actual_num_animals=0;
       animals=new Animal[num_cages+1];
   }
  
   void add(Animal a){
       animals[actual_num_animals]=a;
       actual_num_animals++;
   }
  
   double total_weight(){
       double sum=0;
       for (Animal animal : animals) {
           sum+=animal.getWeight();
       }
       return sum;
   }
  
   void make_all_noises(){
       for (Animal animal : animals) {
           System.out.println(animal.makeNoise());
       }
   }
  
   void print_all_animals(){
       for (Animal animal : animals) {
           System.out.println(animal);
       }
   }
  
   public static void main(String[] args) {
Zoo z = new Zoo();
Snake sly = new Snake(\"Sly\", 5.0 , 2, 2);
Snake sly2 = new Snake(\"Slyme\", 10.0 , 1, 2);
Cow blossy = new Cow(\"Blossy\", 900., 5, 10);
Horse prince = new Horse(\"Prince\", 1000., 5, 23.2);

// Following not allowed because Animal is abstract
//Animal spot = new Animal(\"Spot\", 10., 4);
  
z.add(sly);
z.add(sly2);
z.add(blossy);
z.add(prince);
  
z.make_all_noises();
System.out.println(\"Total weight =\" + z.total_weight());
System.out.println(\"**************************\");
System.out.println(\"Animal Printout:\");
z.print_all_animals();

System.out.println(\"********* Now we will make the Second Zoo\");
Zoo z2 = new Zoo(5);
z2.add(sly);
z2.add(sly2);
z2.add(blossy);
z2.add(prince);
z2.add( new Horse(\"Warrior\", 1200, 6, 25.3));
z2.add( new Horse(\"Harry\", 1100, 4, 21.3));
System.out.println(\"Total weight of z2=\"+z2.total_weight());
z2.make_all_noises();
z2.print_all_animals();
   }
}

OUTPUT:

Hisssssss
Hisssssss
Moooo
Whinny
Total weight =1915.0
**************************
Animal Printout:
name=Sly, weight=5.0, age=2,num_fangs=2
name=Slyme, weight=10.0, age=1,num_fangs=2
name=Blossy, weight=900.0, age=5num_spots=10
name=Prince, weight=1000.0, age=5,top_speed=23.2
********* Now we will make the Second Zoo
Total weight of z2=4215.0
Hisssssss
Hisssssss
Moooo
Whinny
Whinny
Whinny
name=Sly, weight=5.0, age=2,num_fangs=2
name=Slyme, weight=10.0, age=1,num_fangs=2
name=Blossy, weight=900.0, age=5num_spots=10
name=Prince, weight=1000.0, age=5,top_speed=23.2
name=Warrior, weight=1200.0, age=6,top_speed=25.3
name=Harry, weight=1100.0, age=4,top_speed=21.3

 Need help with this JAVA practice problem You run a zoo which contains Animals. So each Animal will be represented by an abstract class. Consider creating clas
 Need help with this JAVA practice problem You run a zoo which contains Animals. So each Animal will be represented by an abstract class. Consider creating clas
 Need help with this JAVA practice problem You run a zoo which contains Animals. So each Animal will be represented by an abstract class. Consider creating clas
 Need help with this JAVA practice problem You run a zoo which contains Animals. So each Animal will be represented by an abstract class. Consider creating clas

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site