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

The Animal class defines two functions getName () & getHeartRare (), as shown in

ID: 3797425 • Letter: T

Question

The Animal class defines two functions getName () & getHeartRare (), as shown in the figure above. These function return the values of the private member variables name & heartRate, respectively. The Mammal class is child of Animal, and defines one function: getLifeSpan (). This function returns the value of the private member variable lifeSpan. The Zebra class is a child of Mammal and defines one function getHabitat (). This function returns the value of the private variable habitat. The Bear class is a child of Mammal and defines one function getAverageSize (). This function returns the value of the private variable averageSize. From the description, above, complete the following: In your development environment, create a new directory, called Quiz 2. Using function and class names exactly as given in the description and figures above, create Java classes to design and represent all the classes in this hierarchy. Place these class files Inside the Quiz 2 directory. Besides the files created for this test, there should be no other files of any type under the Quiz 2 directory. For each class, define two constructors: a default, no-argument constructor, and a constructor to assign values to all the member variables, including the variables defined in the class's parent, if any. Adding getter and setter methods, in addition to the ones in the figure, is entirely optional; however, you can only use constructors to assign values to class instance variables from your Driver.java program. Use variable types exactly as given in the figure. All class-level member variables must be defined as private.Again, all class-level variables are private. Points will be deducted if this requirement or any other stated requirement is not fulfilled. Create a Driver class, called 'Driver.java', that has a main() function to test the class above. Use a Scanner to read the user's response for the name, heartRate, lifeSpan, and habitat. Use this data to construct a Zebra object. Display the object variables values using the functions given in the classes. Compile and run the Driver. Make sure it displays the expected output.

Explanation / Answer

PROGRAM CODE:

Animal.java

package Quiz2;

public class Animal {

   private String name;

   private int heartRate;

  

   public Animal()

   {

       name = "";

       heartRate = 0;

   }

  

   public Animal(String name, int heartRate)

   {

       this.name = name;

       this.heartRate = heartRate;

   }

  

   public String getName()

   {

       return name;

   }

  

   public int getHeartRate()

   {

       return heartRate;

   }

}

Mammal.java

package Quiz2;

public class Mammal extends Animal{

   private double lifeSpan;

  

  

   public Mammal()

   {

       super();

       lifeSpan = 0;

   }

  

   public Mammal(String name, int heartRate, double lifeSpan)

   {

       super(name, heartRate);

       this.lifeSpan = lifeSpan;  

   }

  

   public double getLifeSpan()

   {

       return lifeSpan;

   }

}

Zebra.java

package Quiz2;

public class Zebra extends Mammal{

   private String habitat;

  

   public Zebra()

   {

       super();

       habitat = "";

   }

  

   public Zebra(String name, int heartRate, double lifeSpan, String habitat)

   {

       super(name, heartRate, lifeSpan);

       this.habitat = habitat;

   }

   public String getHabitat()

   {

       return habitat;

   }

}

Bear.java

package Quiz2;

public class Bear extends Mammal{

  

   private double averageSize;

  

  

   public Bear()

   {

       super();

       averageSize = 0;

   }

  

   public Bear(String name, int heartRate, double lifeSpan , double averageSize)

   {

       super(name, heartRate, lifeSpan);

       this.averageSize = averageSize;

   }

   public double getAverageSize()

   {

       return averageSize;

   }

}

Driver.java

package Quiz2;

import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {

       String name, habitat;

       double lifeSpan;

       int heartRate;

       Scanner keyboard = new Scanner(System.in);

       System.out.print("Enter the name: ");

       name = keyboard.nextLine();

       System.out.print("Enter the heart rate: ");

       heartRate = Integer.parseInt(keyboard.nextLine());

       System.out.print("Enter the life span: ");

       lifeSpan = Double.valueOf(keyboard.nextLine());

       System.out.print("Enter the habitat: ");

       habitat = keyboard.nextLine();

       Zebra zebra = new Zebra(name, heartRate, lifeSpan, habitat);

       System.out.println(" Details of Zebra: ");

       System.out.println("Name: " + zebra.getName());

       System.out.println("Heart Rate: " + zebra.getHeartRate() + "/min");

       System.out.println("Life Span: " + zebra.getLifeSpan() + " yrs");

       System.out.println("Habitat: " + zebra.getHabitat());

   }

}

OUTPUT:

Enter the name: Zebra Pet

Enter the heart rate: 72

Enter the life span: 40

Enter the habitat: forest

Details of Zebra:

Name: Zebra Pet

Heart Rate: 72/min

Life Span: 40.0 yrs

Habitat: forest