1. Engineering
  2. Computer Science
  3. ive completed most of the following classes but im stuck...

Question: ive completed most of the following classes but im stuck...

Question details

I've completed most of the following classes, but I'm stuck on a few areas like toString() method in the zoo class. I'll paste the classes below and any help is much appreciated! Pardon all the comments.

public class Driver
{
/**
* The main method which does not utilize args.
* Simply instantiates two Animal objects, adds
* them to a zoo, and then prints out information about the zoo.
* @param args String array taking command-line arguments
*/
public static void main(String[] args)
{
/*
* You don't have to change any of this particular code however you may
* find it useful to play around with how the code works here.
*/
Animal a = new Animal("Green", "Perry", 350.0, 40.1);
Animal b = new Animal("Orange", "Tony", 200.5, 15.0);

// Create a zoo and add animals to it:
Zoo zoo = new Zoo(1);
zoo.addAnimal(a); // Gorilla
zoo.addAnimal(b); // Tiger

// Print out info about the zoo:
System.out.println(zoo);
}
}

public class Animal
{
/**
* The color of the animal
*/
private String color;
  
/**
* The name of the animal
*/
private String name;

/**
* The weight of the animal in pounds
*/
private double weight;

/**
* The height of the animal in inches
*/
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.\n"
* Weight and height are formatted to one decimal place.
*/
public String toString()
{
return String.format("%s, a %s-colored animal. %.1f pounds, %.1f inches\n", /* TODO: complete this...*/
       name, color, weight, height);
}
}

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;

/**
* Default Constructor
*
* Initializes the zoo capacity (and array size) to an initial value
* Sets the numAnimals to 0
*
* @param initialCapacity The initial number of animals that the zoo can hold.
*/
public Zoo(int intialCapacity)
{
// TODO: complete this...
  
   numAnimals = 0;
}

/**
* Doubles the capacity of the zoo (both the variable and the size of the array).
*
* Remember: an array's size is immutable - it cannot be changed. Instead, to "increase" the size of the
* animals array, you will need to create a new array with a larger size, copy over the data in the "animals"
* array, and update the reference of the "animals" array to the new array.
*/
private void expandZoo()
{
// TODO: complete this...
   Animal[] tempArray = new Animal[capacity * 2];
System.arraycopy(animals, 0, tempArray, 0, capacity);
   for(int i = 0; i < capacity; i++)
   {
       tempArray[i] = animals[i];
   }
}

/**
* 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...
   if(numAnimals == animals.length) {
Animal[] copy = new Animal[animals.length * 2];
for(int i = 0; i < numAnimals; i++) {
copy[i] = animals[i];
}
animals = copy;
capacity *= 2;
}
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...
   double totalHeight = 0;
   for (int i = 0; i <= numAnimals; i++)
   {
       totalHeight += animals[i].getHeight();
   }
   return totalHeight;
}
  
/**
* Loop over all the Animals in the zoo to compute the average weight.
*
* @return Average weight (rounded down) of all animals in the zoo.
*/
public int getAverageWeight()
{
// TODO: complete this...
   int counter = 0;
   double averageWeight = 0;
   for (int i = 0; i <= numAnimals; i++)
   {
       averageWeight += animals[i].getWeight();
       counter++;
   }
   averageWeight /= counter;
   return (int)averageWeight;
}
  
/**
* Loop over all the Animals in the zoo to compute the average weight over all animals that
* match the given color.
*
* (Be sure to divide by the number of animals that match the color, not the total number of animals)
*
* @param color The average weight is computed for animals of this color.
* @return Average weight (rounded down) of all animals of a certain color in the zoo.
*/
public int getAverageWeight(String color)
{
// TODO: complete this...
   int counter = 0;
   double averageWeight = 0;
   for (int i = 0; i <= numAnimals; i++)
   {
       Boolean colorTemp;
       if (colorTemp = Boolean.parseBoolean("Green"))
       {
           averageWeight += animals[i].getWeight();
       counter++;
       }
   }
   averageWeight /= counter;
   return (int)averageWeight;
}

/**
* Retrieve the array that constitutes the animals in the zoo.
*
* @return The Animal array representing the zoo.
*/
public Animal[] getAnimals()
{
return animals;
}
  
/**
* Get the zoo's capacity.
*
* @return The zoo's capacity.
*/
public int getCapacity()
{
   return capacity;
}

/**
* Print out formatted information about the animals in the zoo.
*
* @return A string representing the animals in the zoo. Formatted as:
* "These animals live in the zoo: \n" +
* for each animal in the animals array (starting from index 0), the toString of the animal
*/
public String toString()
{
// TODO: complete this...
   String s1 = "These animals live in the zoo: \n";
   for (int i = 0; i <= numAnimals; i++)
   {
  
   }
}
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution