// Main.java
package flipcoins;
import java.util.*;
/***********************************************************************************
 * This class/program illustrates some random number techniques using Java.
 *<p>
 *
 * Author:  Marsha Zaidman <br>
 * Email:   marsha@umw.edu<br>
 * Language:    Java<br>
 *<p>
 **********************************************************************************/
public class Main {
    /******************************************************************************
     *Invariant of Main class
     *  Simulates and tracks the number of heads and tails outcomes
     *  The seed for the random generator will be different each execution
     *****************************************************************************/
    /** Creates a new instance of Main */
    public Main() {
    }
    /**HEAD is the value that represents the heads side of a coin */
    private static final int HEAD = 0;
    /**TAIL is the value that represents the tails side of a coin */
    private static final int TAIL = 1;
    
    /**************************************************************************
     * Simulates a specific number of coin flips.
     *<p>
     *<b>Precondition: </b None
     * <p>
     * <b>Post-Condition: </b> nrHeads holds the number of head flips 
     * <p>and nbrTails holds the number of tail flips.
     * @param   args    the user desired number of coin flips to simulate
     **************************************************************************/
    public static void main(String[] args) {
        /**nbrFlips is the number of coin flips to be simulated. */
        long nbrFlips = 1000;
        if (args.length > 1) {
            System.out.println("Too many arguments in Command Line ");
            System.exit(1);
        } else if (args.length == 1) {
            nbrFlips = Long.parseLong(args[0]);
        }
        /*nrHeads holds the number of head flips */
        long nbrHeads = 0;
        /*nbrTails holds the number of tail flips */
        long nbrTails = 0;
        /*date is current date information */
        Date date = new Date();
        /*millisec is the number of milliseconds since 1/1/1970*/
        long millisec = date.getTime();
        /*rnd is the initial seed to the generator */
        Random rnd = new Random( millisec );
        for (long i = 0; i <nbrFlips; i++) {
            /*side is the side of the coin (head or tail)*/
            int side = Math.abs(rnd.nextInt()) % 2;
            if (side == HEAD ){ ++nbrHeads;} else {++nbrTails;}
        }
        /*msg1 is the first part of the display of results*/
        String msg1 = "Number of heads = " + nbrHeads;
        msg1 += "(" + ((float) nbrHeads/nbrFlips * 100) + "%)\n";
        /*msg2 is the second part of the display of results */
        String msg2 = "Number of tails = " + nbrTails;
        msg2 += "(" + ((float)nbrTails/nbrFlips * 100) + "%)";
        System.out.println(msg1 + msg2);
    }
    
}

