import java.util.*; public class Zoo { public static void main (String [] args) { // We'll use a "HashSet" to keep track of our kangaroos HashSet kangaroos = new HashSet (); // Let's read the initial population from the command line int initialPopulation = Integer.parseInt (args [0]); // Let's read the number of years to run the zoo from the command line int duration = Integer.parseInt (args [1]); // Let's fill our zoo. What assumption are we making here??? for (int rooIndex = 0; rooIndex < initialPopulation; rooIndex++) { kangaroos.add (new Kangaroo ()); } // Display the initial population System.out.println ("Year 0: " + initialPopulation + " kangaroos"); // Step forward, year by year for (int yearIndex = 0; yearIndex <= duration; yearIndex++) { // Let's keep track of how many births and deaths there have been this year int births = 0; int deaths = 0; // Here, we'll use an array (instead of the HashSet), for convenience Kangaroo [] roos = new Kangaroo [kangaroos.size ()]; kangaroos.toArray (roos); // Iterate through the kangaroos for (int rooIndex = 0; rooIndex < roos.length; rooIndex++) { Kangaroo kangaroo = roos [rooIndex]; // The kangaroo has aged one year. kangaroo.age (); // Did the kangaroo survive the year? if (kangaroo.survive ()) { // Did the kangaroo breed this year? Kangaroo offspring = kangaroo.breed (); if (offspring != null) { // Did the offfspring survive its first year? if (offspring.survive ()) { // Count the offspring as a birth, and add it to the zoo. births++; kangaroos.add (offspring); } } } else { // Add to the number of deaths, and remove the dead kangaroo from the zoo. deaths++; kangaroos.remove (kangaroo); } } // Display the births and deaths in the year, and the ending population. System.out.println ("Year " + (yearIndex + 1) + ": " + births + " births, " + deaths + " deaths, " + kangaroos.size () + " kangaroos"); } } }