Question: I'm working on a program that has a driver, animal, and zoo class. The driver is the main class, the animal class represents an animal
I'm working on a program that has a driver, animal, and zoo class. The driver is the main class, the animal class represents an animal in a zoo (their color, name, weight, and height), and the zoo class represents a zoo and holds a list of animals that are present at the zoo and calculates statistics about these animals. I'm having issues with two methods in the zoo class. i'll paste the animal class below along with the 2 methods in the zoo class that I need help with. If you need to know anything else just let me know!
public class Animal { private String color; private String name; private double weight; private double height; /** * Empty (Default) Constructor * * Sets the weight to 0, age to 0, name to "noname", and color to "unknown" */ public Animal() { // TODO: complete this... weight = 0; height = 0; name = "noname"; color = "unknown"; } /** * Animal constructor setting all fields. * * @param color The animal color. * @param name The animal's name. * @param weight The animal's weight in pounds. * @param height The animal's height in inches. */ public Animal(String color, String name, double weight, double height) { // TODO: complete this... this.color = color; this.name = name; this.weight = weight; this.height = height; } // TODO: add getters... public String getColor() { return color; } public String getName() { return name; } public double getWeight() { return weight; } public double getHeight() { return height; } /** * When the user needs to print out info about an animal, present * the animal in order of color, name, weight, and height * * @return The string representation of the Animal class, formatted as: * "(name), a (color)-colored animal. (weight) pounds, (height) inches. " * Weight and height are formatted to one decimal place. */ public String toString() { return String.format("%s, a %s-colored animal. %.1f pounds, %.1f inches ", /* TODO: complete this...*/ name, color, weight, height); } }
I'm pretty sure I've done all of the above correctly. Here are the methods that I need help with. I've attempted it which may or may not be correct.
public class Zoo { /** * Array to keep track of all of the Zoo's animals. * * The size of the array is the capacity of the zoo. This should match the value of the * member variable "capacity". * * The number of animals in the zoo may be less than the capacity of the zoo. For example, * the zoo may be able to hold 5 animals at a given point (array size/capacity = 5), but only * contain 2 animals (numAnimals=2). * * When an animal is added, it should be added in the first free index. I.e. if there are 2 * animals in the array, when a third is inserted it should be inserted in index 2, as arrays * start from 0. * * When an animal should be added but the zoo is full, the zoo must be expanded to increase * capacity. */ private Animal[] animals;
/** * The number of animals present in the zoo. */ private int numAnimals; /** * The number of animals that the zoo can currently hold. * Should always be equal to the size of the animals array. */ private int capacity;
/** * Add a single animal to the zoo. Adds the animal into the animals array and updates the number of animals. * Expands the animals array if there is not enough room to store a new animal (zoo capacity is exceeded). * * @param ani The animal to be added to the zoo. */ public void addAnimal(Animal ani) { // TODO: complete this... animals[numAnimals] = ani; }
/** * Loop over all the Animals in the zoo to compute the total height. * * @return The sum of height of animals in the zoo. */ public double getTotalHeight() { // TODO: complete this... int totalHeight; for (int i = 0; i <= capacity; i++) { } return totalHeight; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
