Birthday.java


Below is the syntax highlighted version of Birthday.java.


/*********************************************************************
 *  Compilation:  javac Birthday.java
 *  Execution:    java Birthday D
 *  
 *  Reads an integer command-line argument D and simulates the number
 *  of people with random birthdays (among D days) that enter a room 
 *  until two share a common birthday.
 * 
 *   > java Birthday 365
 *   22                             Booksite Creative Exercise 1.4.35
 *********************************************************************/

public class Birthday { 
    public static void main(String[] args) {
        // number of days 
        int D = _________________________________________ ;
       
        // number of people who have entered the room 
        int people = 0;
       
        // days[d] = true if a person has birthday d; false otherwise
        // auto-initialized to false
        _______________[] days  =   new  _______________________ ;

        // repeat until two people have the same birthday
        while (true) {
            // increment number of people
            people _________ ;

            // random day between 0 and D-1
            int d = _________________________________________________ ;
           
            // if another person shares birthday d, break out of loop
            if ( ___________________________ )   ____________________ ;
           
            // update days[] to indicate person has birthday d
            days[ _____ ] = __________________ ;
        }

        // print result - How many people entered room to get duplication?
        System.out.println( _______________________________ );
    }
}