/** FlipCoinDemo synthesized in class, after you tried it on your own
 *  with paper. 
 *  Thu Feb 23 2006
 */
public class FlipCoinDemo
{
    public static void main( String[] args )
    {
        int i = 1;
        int heads = 0;
        int tails = 0;
        int limit = 1000000; 
	// This is a magic number, would be better to use a constant!

	// Note that we have a <= in the condition here, because we
	// started with i=1. We could have also started at 0, and then
	// the right test would be strictly less than. But if we set
	// i=0 and then test for i<= limit, we would be off by one!
        while ( i  <= limit) 
        {
            double result = Math.random();
            i++;
            if (result <= 0.5) {
                        tails++;
                } else if (result > .5) { 
		// } else {  
		// this else statement with no test would also be
		// correct! it's more concise than the full test
		// above, and arguably more clear also.
                        heads++;
                }
	    //i++; 
	    // We could also put increment here, instead of above the
	    // if statement. Anywhere inside the loop and outside the
	    // conditional would be safe. Of course, we only want the
	    // increment in one of these places!
        }
        System.out.println("Heads: "+heads + " tails " +tails);
    }
}

