/*******************************
 * Playing with analyzing coin tosses.
 *
 *   The bug from version 1.0 is fixed.
 *
 * Challenge: modify this program to count toss 20 coins many
 * times, and count how many of those have 1 head, 2 heads, ...
 * (Leave out all the the detailed 1:head printing I have below.)  
 * These numbers will form what's called a "Bell curve."  
 * And for a *really* good time, graph the resulting bell curve
 * with spaces and * chars, on its side so it looks something like
 *   1  *
 *   2   *
 *   3     *
 *   ... etc.
 * where the number of spaces before the "*" depends on how many
 * times that number of heads turned up.
 *
 * @author  Jim mahoney
 * @version 1.1, Oct 7, 10pm
 *
 ****/

class CoinFlipping {

    public static void main(String[] args){

        int howMany = 20;
        int counter = 1;
        double headCount = 0;
        print(" Tossing a coin " + howMany + " times.");
        while (counter<=howMany){
            boolean isHead = coinToss();
            if (isHead) { 
                headCount++; }
            print(" " + counter + ": "
                  + booleanToString(isHead) + "." );
            counter++;
        }
        print(" Total number of heads = " + headCount + ".");

        // Debugging - why are we getting average = 0 ?
        System.out.println(" headCount = " + headCount );
        System.out.println(" howMany = " + howMany );

        print(" Average number = " + headCount/howMany + ".");
    }

    // convert a boolean (true or false) to a String ("heads" or "tails")
    public static String booleanToString(boolean x){
        if (x) {
            return "heads";
        }
        else {
            return "tails";
        }
    }

    public static boolean coinToss(){
        // Can you find the Math.random documentation in the Java API ?
        // How about trying the "ask Google" method?
        double randomNumber = Math.random(); // between 0 and 1
        if (randomNumber>0.5) {
            return true;
        }
        else {
            return false;
        }
    }

    // Print a line of dashes, i.e. " - - - - - - - - - "
    public static void line(){
        int iDashCount=20;
        System.out.print(">");
        while (iDashCount>0){
            System.out.print(" -");
            iDashCount--;
        }
        System.out.println(" ");
    }

    // I get tired of typing "System.out.println".
    // So this lets me just type "print".
    public static void print(String s){
        System.out.println("> "+s);
    }

}


syntax highlighted by Code2HTML, v. 0.9.1