Assignement: Final Semester 1 with Coin Flips

Code

 /// Name: Sean Harrison
    /// Period: 7
    /// Program name: FinalExam
    /// File Name: FinalExam.java
    /// Date Finished: 11/21/2015
    
import java.util.Scanner;
import java.util.Random;
public class FinalExam
{
    public static void main( String[] args )
    {
    Random r = new Random();
    Scanner keyboard = new Scanner(System.in);
    int flipNum, trials, result, tails, heads; ///Choose these variables due to their simplicity.
    
    trials = 0;
    tails = 0;
    heads = 0;
    System.out.println( "How many times would you like to flip the coin?" );
    flipNum = keyboard.nextInt();
    while ( flipNum <= 0 && flipNum >= 2100000000 ) /// These are the specified values to be used.
    {
        System.out.println( "Invalid number please choose between 1-2.1 billion");
        System.out.println( "How many times would you like to flip the coin? " );
        flipNum = keyboard.nextInt();
    }
    
    do /// used do and then a while to keep the loop going, basic essentials.
    {
     result = 1 + r.nextInt(2); /// only 2 possible varriables

    if (result == 1);
    {

        heads++;
        trials++;  ///counters for heads and trials if the result was 1
    }
    if (result == 2);
    {
    
        tails++;
        trials++;  /// counters for tails as well as the trails for 2
     }
     } while ( trials != flipNum ); /// made sure that the program would run as long as the user specified.
     
     System.out.println( "Out of the " + flipNum + " flips " + heads + " were heads. " );
     System.out.println( "Out of the " + flipNum + " flips " + heads + " were tails. " );
                
        
                double probOfHeads = (double)heads / flipNum * 100; 
                double probOfTails = (double)tails / flipNum * 100;
    System.out.println( "The probability of rolling heads was " + probOfHeads + ". ");
    System.out.println( "The probability of rolling tails was " + probOfTails + ". ");
    }
}
/// If I wanted to get as close to 50% as possible I'd either choose a small sample size. The larger the sample size the more likely you are to be off technically since theres more times for variance to take hold. although you could argue stastically you're more accurate with a sample size, but the stastical innaccuracy of a small sample size I believe would give you an easier chance of getting the desired 50%.
       
 

Picture of the output

Assignment 43