Semester One Projetc and Final Exam Program

Code

    /// Name: Brendan Baird
    /// Period: 6
    /// Program Name: Final Exam Program
    /// File Name: FinalExamProject.java
    /// Date Finished: 1/22/2016
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class FinalExamProject
    {
        public static void main(String[] args)
        {
            Scanner k = new Scanner(System.in);
            Random r = new Random();
            
            int flips;
            int numberOfHeads = 0;
            int numberOfTails = 0; // these will be used to count how many times it lands on heads and tails
            
            do
            {
                System.out.println();
                System.out.print("How many times should I flip this coin, dear Guildenstern? ");
                flips = k.nextInt(); // this is the max limit of how many times the following program will run
            
                if ( flips <= 0 )
                    System.out.println("Oh don't be like that, loyal Guildenstern! C'mon, how many times?");
            
                else if ( flips > 2100000000 )
                    System.out.println("I'll be flipping  this coin all day! How many times should I actually flip this coin?"); // these are error messages if the user puts in a limit too small or too high to run
            
            } while ( flips <= 0 || flips > 2100000000 ); // this will loop until user enters a usable limit
            
            for ( int i = 1 ; i <= flips ; i++ ) // loop for random results of coin flip
            {
                int landFace = 1 + r.nextInt(2); // lnadFace is a random variable with two possible values: 1 or 2, heads or tails
                
                if ( landFace == 1 ) // two if statements are used for the two possible values of landFace
                {
                    numberOfHeads++; // the number of heads flipped increases every time landFace equals 1
                }
                
                else if ( landFace == 2 )
                {
                    numberOfTails++; // number of tails flipped increases every time landFace equals 2
                }
            }
            
            System.out.println();
            System.out.println("So... It landed on heads " + numberOfHeads + " times.");
            System.out.println("That means it landed on tails " + numberOfTails + " times!"); //the collected values are shown
            
            double probOfHeads = ((double)numberOfHeads / flips) * 100;
            double probOfTails = ((double)numberOfTails / flips) * 100; //finds the probability of the coin landing on heads and tails by dividing the sum of faces divided by the total number of flips, and multiplying by 100 to get a percentage
            // uses casting
            
            System.out.println();
            System.out.println("A weaker man might be moved to reexamine his faith. If for nothing at least in the law of probability.");
            System.out.println("Consider. One. Probabilty is a factor which operates within natural forces. Two. Probability is not operating as a factor. Three. We are now held within un- sub- or super-natural forces. Discuss.");
            System.out.println("What?");
            
            //this is just a Rosencrantz and Guildenstern are Dead reference for good measure
            
            System.out.println();
            System.out.println("Anyway... The probability of landing on heads is " + probOfHeads + "%.");
            System.out.println("And the probability of landing on tails is " + probOfTails + "%.");
            System.out.println();
            // shows the probability/percentage of head or tails flipped
        }
    }
    
    // The more flips made, the closer the probability of landing heads or tails is to 50%. After running the program several times, I found that 200 flips is enough to achieve a 0.5% difference between probabilities.
    

Picture of the output

Semester 1 Project