Final Exam

Code

    /// Name: Ali Kurland
    /// Period: 6
    /// Program Name: Final Exam
    /// File Name: FinalExam.java
    /// Date Completed: 1/22/2016
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class FinalExam
    {
        public static void main( String[] args )
        {
            Scanner keyboard = new Scanner(System.in);
            
            int flips, numberOfHeads=0, numberOfTails=0; // Variables represent integer number of flips, number of heads, & number of tails.
            
            System.out.print( "How many times would you like to flip a coin? " );
            flips = keyboard.nextInt(); // User inputs number of flips
            
            while ( ( 0 >= flips ) || ( 2100000000 <= flips ) ) // While loop used to initially check whether the input number of flips 
            fits the parameters & if not, repeats until the user inputs a number that is within them.
            {
                System.out.print( "\nThe number must be between 0 and 2,100,000,000. Try again: " );
                flips = keyboard.nextInt();
            }
            
            for ( int n = 1 ; n <= flips ; n = n+1 ) // For loop used to repeat coin flip simulation for the number of flips desired by the user.
            {
                Random r = new Random(); //Random variable used to simulate random flipping of coin.
                int headsOrTails = 1 + r.nextInt(2); // Outputs the integer 1 (representing heads) or 2 (representing tails).
                
                if ( headsOrTails == 1 )
                    numberOfHeads = numberOfHeads + 1; // Increases total number of heads by one if headsOrTails was 1.
                else
                    numberOfTails = numberOfTails + 1; // Increases total number of tails by one if headsOrTails was 2.
            }
            
            // Prints number of heads and tails obtained in for loop sequence.
            System.out.println( "\nYou got heads " + numberOfHeads + " times and tails " + numberOfTails + " times." );
            
            double probOfHeads = (double)numberOfHeads / flips; //Calculates probability of getting heads.
            double probOfTails = (double)numberOfTails / flips; //Calculates probability of getting tails.
            
            // Prints probability of getting heads or tails.
            System.out.println( "\nThe probability of getting heads was " + probOfHeads + " and the probability of getting tails was " + probOfTails + ".\n" );
            
        }
    }
     
    /* 
    One million is a good number yielding fairly precise results, as at one million, the probabilities are consistently 
    very close to 50% (within  one one-thousandth or one ten-thousandth away from exactly .5 ).
    
    However, the degree of precision continues to increase as the number of flips increases. 
    At one hundred million, for example, the probability is consistantly within one ten-thousandth away from exactly .5; 
    at one billion, the probability is within one hundred-thousandth, and so on.
    */
    

Picture of the output

10 100 mil bil