Question: How would you complete this program in Java ? How would you finish main program and write a test program? Explanation for step 1 This
How would you complete this program in Java ?
How would you finish main program and write a test program?
Explanation for step 1
This program defines three classes: ZooAnimals, Tiger, and Gorilla.
The ZooAnimals class is an abstract class that represents all types of zoo animals. It has four private fields: sound, name, numberOfLegs, and weight. These fields are accessible via the corresponding getter and setter methods. It also has an all-args constructor that initializes all of these fields, as well as an abstract method calculateWeeklyFoodNeededPounds that returns a double, and a toString method that returns a string representation of the object.
The Tiger class extends the ZooAnimals class, and adds a private field tigerType. It also has an all-args constructor that initializes all of the fields, including the tigerType field. It also has getter and setter methods for the tigerType field. Additionally, it overrides the calculateWeeklyFoodNeededPounds method to return 15% of the tiger's weight. It also overrides the toString method to return a string representation of the object that includes the tigerType field.
The Gorilla class also extends the ZooAnimals class, and adds a private field gorillaType. It also has an all-args constructor that initializes all of the fields, including the gorillaType field. It also has getter and setter methods for the gorillaType field. Additionally, it overrides the calculateWeeklyFoodNeededPounds method to return 50% of the gorilla's weight. It also overrides the toString method to return a string representation of the object that includes the gorillaType field.
The Test Program that prompts the user in a loop to create ZooAnimals and adds each ZooAnimals to an ArrayList . It also creates a method that prints and calculates the cost of feeding the animals. The user is prompted to enter the type of animal (T for Tiger or G for Gorilla), the animal's name, sound, number of legs, weight, and type. The program continues prompting the user to enter another animal until they enter 'n'.
Create a Gorilla class. It extends ZooAnimal
a private String gorillaType
An all-args constructor that creates a Tiger for all private fields.
getter/setter methods for all private fields.
a gorilla needs to eat 50% of it's weight each week. Use this for calculateWeeklyFoodNeededPounds
create a toString method that prints all of the fields
Create a Test Program
Write a test program that prompts the user in a loop to create ZooAnimals.
Add each ZooAnimal to an Arraylist.
create a method that prints and calculates the cost of feeding the animals assume that gorilla food costs $2 lbs and tigers cost $4 lbs -- call this method after gathering the information below
Ask the following question;
What Type of Zoo Animal, enter T for Tiger or G for Gorilla?
{type}'s name?
{type}'s sound?
{type}'s number of leg?
{type}'s weight?
{type}'s type:
Enter another Zoo, enter Y for yes?
Use an Enhanced for loop and for each ZooAnimal print out the ToString and the calculateWeeklyFoodNeededPounds
Example output:
What Type of Zoo Animal, enter T for Tiger or G for Gorilla? T Tigers name? Billy Tigers sound? ROAR Tigers number of legs? 4 Tigers Weight? 569 Tigers type? Bengal Enter another Zoo Animal, enter Y for yes? Y What Type of Zoo Animal, enter T for Tiger or G for Gorilla? G Gorillas name? Joe Gorillas sound? ROAR Gorillas number of legs? 2 Gorillas Weight? 445 Gorillas type? Mountain Enter another Zoo Animal, enter Y for yes? n Tiger [tigerType=Bengal, sound=ROAR, name=Billy, numberOfLegs=4, weight=569.0] Gorilla [gorillaType=Mountain, sound=ROAR, name=Joe, numberOfLegs=2, weight=445.0] The cost to feed the Tiger Billy for a week: $341.40 The cost to feed the Gorilla Joe for a week: $445.00
import java.util.ArrayList; import java.util.Scanner;
abstract class ZooAnimals { private String sound; private String name; private int numberOfLegs; private double weight;
public ZooAnimals(String sound, String name, int numberOfLegs, double weight) { this.sound = sound; this.name = name; this.numberOfLegs = numberOfLegs; this.weight = weight; }
public String getSound() { return sound; }
public void setSound(String sound) { this.sound = sound; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getNumberOfLegs() { return numberOfLegs; }
public void setNumberOfLegs(int numberOfLegs) { this.numberOfLegs = numberOfLegs; }
public double getWeight() { return weight; }
public void setWeight(double weight) { this.weight = weight; }
public abstract double calculateWeeklyFoodNeededPounds();
@Override public String toString() { return "ZooAnimals [sound=" + sound + ", name=" + name + ", numberOfLegs=" + numberOfLegs + ", weight=" + weight + "]"; } }
class Tiger extends ZooAnimals { private String tigerType;
public Tiger(String sound, String name, int numberOfLegs, double weight, String tigerType) { super(sound, name, numberOfLegs, weight); this.tigerType = tigerType; }
public String getTigerType() { return tigerType; }
public void setTigerType(String tigerType) { this.tigerType = tigerType; }
@Override public double calculateWeeklyFoodNeededPounds() { return getWeight() * 0.15; }
@Override public String toString() { return "Tiger [tigerType=" + tigerType + ", sound=" + getSound() + ", name=" + getName() + ", numberOfLegs=" + getNumberOfLegs() + ", weight=" + getWeight() + "]"; } }
class Gorilla extends ZooAnimals { private String gorillaType;
public Gorilla(String sound, String name, int numberOfLegs, double weight, String gorillaType) { super(sound, name, numberOfLegs, weight); this.gorillaType = gorillaType; }
public String getGorillaType() { return gorillaType; }
public void setGorillaType(String gorillaType) { this.gorillaType = gorillaType; }
@Override public double calculateWeeklyFoodNeededPounds() { return getWeight() * 0.5; }
@Override public String
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
