import java.util.*;
import java.text.*;

public class Zoo {
    
    public static void main (String [] args) {

        /**
         * We'll use a "HashSet" to keep track of our kangaroos; think of this
         * as a bag into which we could put almost any kind of object (in this 
         * case, we're only going to put kangaroos in the bag.
         */
        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]);

        /**
         * It might be interesting to tally the average kangaroo age at each
         * step. To do this, we'll actually total the ages, and divide by the
         * total number of kangaroos.
         */
        int totalAge = 0;
        double averageAge;
        
        // We'll set up a NumberFormat, for displaying the average age.
        DecimalFormat ageFormat = new DecimalFormat ();
        ageFormat.setMinimumFractionDigits (2);
        ageFormat.setMaximumFractionDigits (2);
        
        /**
         * Let's fill our zoo. Initially, we had an implicit assumption that all
         * kangaroos were 0 years old at the start, but that doesn't really make
         * sense. Now, we'll randomly pick an age between 0 and 6 years.
         */
        for (int rooIndex = 0; rooIndex < initialPopulation; rooIndex++) {
            int age = (int) (Math.random () * 7.0);
            totalAge += age;
            kangaroos.add (new Kangaroo (age));
        }
        // Calculate the average age.
        averageAge = (float) totalAge / initialPopulation;
        // Display the initial population
        System.out.println ("Year 0: " + initialPopulation + " kangaroos, average age = " + ageFormat.format (averageAge) + " years");

        // 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;
            
            // Reset the tally we use to calculate average age.
            totalAge = 0;
            
            // Here, we'll use an array, for convenience when iterating.
            Kangaroo [] roos = new Kangaroo [kangaroos.size ()];
            kangaroos.toArray (roos);

            // Iterate through the kangaroos
            for (int rooIndex = 0; rooIndex < roos.length; rooIndex++) {
                
                // Look at a single kangaroo
                Kangaroo kangaroo = roos [rooIndex];
                // The kangaroo has aged one year.
                kangaroo.age ();
                
                // Did the kangaroo survive the year?
                if (kangaroo.survive ()) {

                    // Add the age to our tally
                    totalAge += kangaroo.getAge ();
                    
                    // 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);                    
                }
            }
            // Calculate the average age.
            averageAge = (float) totalAge / kangaroos.size ();
            // Display the births and deaths in the year, ending population, and average age.
            System.out.println ("Year " + (yearIndex + 1) + ": " + births + " births, " + deaths + " deaths, " + kangaroos.size () + " kangaroos, average age = " + ageFormat.format (averageAge) + " years");
        }
    }
    
}
