Semester 1 Final Exam

Code

    /// Name: John Huh
    /// Period: 6
    /// Program Name: Semester 1 Final Exam
    /// File Name: FinalExamS1.java
    /// Date Finished: 1/22/2016
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class FinalExamS1
    {
    	public static void main( String[] args )
    	{
    		Scanner keyboard = new Scanner(System.in);
            Random r = new Random();
            
            int times, heads, tails, flips, coin;
            
            //The integer "times" represents the number of times the user wants to flip the coin
            //I decided to define "heads" and "tails" with a predetermined integer and will count how many times i get either heads or tails
            //The "flips" integer will continue to repeat the 'while' command until it is equal to the given integer for "times"
            //The integer "coin" will be given the value of r with a domain of either 0 or 1
            
            heads = 0;
            tails = 0;
            coin = r.nextInt(2);
            flips = 0;
                
            System.out.println();
            System.out.println( "In order to determine the probability of getting heads or tails, you must input how many times the coin should be" );
            System.out.println( "flipped.  Please enter an integer greater than 0 and less than 2100000000." );
            System.out.print( "Number of time(s): " );
            times = keyboard.nextInt();
            System.out.println();
            
            while ( times > 2100000000 || times < 1 )
            {
                System.out.println( "I'm sorry, please enter a different integer that is both greater than 0 and less than 2100000000." );
                System.out.print( "Number of times: " );
                times = keyboard.nextInt();
                System.out.println();
            }
            
            //I used a 'while' statement here to filter out any numbers that do not fulfill the domain of the "times" value.
            
            do
            {
                coin = r.nextInt(2);
                
                if ( coin == 1 )
                {
                    heads++;
                }
                else if ( coin == 0 )
                {
                    tails++;
                }
                    
                //The 'if' statements decided if based on the given "coin" value, which side would increase by 1 in value
                
                flips++;    
            } while ( flips != times );
            
            //I decided to use a 'do while' statement here since the number of times can't be lower than one, therefore the 'do while' statement automatically does a coin flip.  I also used a 'do while' statement to cause this command to repeat the coin flip until the "flip" value equaled the "times" value.  In this while statement, I decided to have "coin"'s value repeated and that value would be used to determine if the coin on that particular flip would be heads or tails, therefore adding to their respective values.
            
            double probOfHeads = ( ( (double)heads / times ) * 100 );
            double probOfTails = ( ( (double)tails / times ) * 100 );
                
            System.out.println( "After flipping the coin " + times + " times, there were " + heads + " heads and " + tails + " tails." );
            System.out.println( "Of all the coin flips, " + probOfHeads + " percent were heads and " + probOfTails + " percent were tails." );
            System.out.println();
            
            //After testing different amount of flips, I have determined that the larger numbers present a closer 50 to 50 ratio between heads to tails than that of lower number of flips.  As a result, 100000000 seems to be quite consistent in presenting percentages very close to an exact 50/50.
        }
    }
    

Picture of the output

Assignment 1