/** * Zoo.java * Maintains a collections of Animal objects. * Uses an array to keep the animal instances * * @author cs230 (SK) * @version Sept 2020 */ public class Zoo { // instance variables private final int initialCapacity; private Animal[] members; private int size; //number of actual animals in this zoo, at any point /** * Creates an empty Zoo * * @param initCap The initial capacity of this zoo */ public Zoo(int initCap) { // initialise instance variables initialCapacity = initCap; members = new Animal[initialCapacity]; size = 0; } /** * Gets the size of this zoo * * @return the number of animals in this zoo */ public int getSize() { return size; } /** * Adds the input animal to this zoo. If this zoo * is too small to accept it, (miracilously!:) it becomes bigger. * * @param a the Animal instance to be added to this zoo */ public void addAnimal(Animal a) { //enlarge the zoo, if need be if (size >= members.length) { extendCapacity(); } //now, safely add the new animal members[size] = a; size++; } /** * Helper, private, to be used in the addAnimal() method * */ private void extendCapacity() { //double the number of the containers Animal[] largerArray = new Animal[2*members.length]; //copy the animals from the old container, //to the new and larger for (int i=0; i