Semester 1 Final

Code

    /// Name: Rachel Smith
    /// Period: 5
    /// Program Name: Final Exam Program
    /// File Name: Final.java
    /// Date Finished: 1/20/2016
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class Final {
        
        public static void main(String[] args) {
            
            Scanner keyboard = new Scanner(System.in);
            Random r = new Random();
            
            int flips = 1, heads = 0, tails = 0;
            
            System.out.println();
            System.out.println( "Welcome to Rachel's Coin-Flipper!" );
            
            do {
                System.out.print( "How many times would you like to flip the coin? " );
                flips = keyboard.nextInt();
            } while ( flips <= 0 || flips > 2100000000 );
            
            for ( int i = 1; i <= flips; i++ ) {
                int side = 1;
                side = r.nextInt(2);
                
                if ( side == 0 ) {
                    heads++;
                } else {
                    tails++;
                }
            }
            
            double probHeads = 100 * heads / flips;
            double probTails = 100 * tails / flips;
            
            System.out.println( "You got + " + heads + " heads and " + tails + " tails." );
            System.out.println( "The probability of flipping heads was " + probHeads + "%" );
            System.out.println( "The probability of flipping tails was " + probTails + "%" );
            
        }
    }
    
    ///I used a for loop because it allows me to repeat a process for a specific number of repetitions.
    ///I used if statements because each time that the loop runs, they evaluate the flip and either add 1 to heads or tails.
    
    ///I found that for no number of flips would the probability come to 50% for both heads and tails. One always has more, 
    so it ends up as 50% and 49%. It was consistently reproducting this result by 10,000 flips. Even in the thousands of flips, 
    there would often be a two or three percent discrepancy.
    

Picture of the output

Assignment 1