Final 1

Code

   
    /// Name: Afaf Nabeeha
    /// Period: 7
    /// Program Name: Final Exam
    /// File Name: Final1.java
    /// Date Finished: 1/21/2016 public class Final
    
    import java.util.Random;
    import java.util.Scanner;
    
            public class Final1
            {
                public static void main(String[] args) 
                {
                    Scanner keyboard = new Scanner(System.in);    
                    Random r = new Random();
                    int fNum, flip, heads, tails, HorT;
                    double pHeads, pTails;
                    heads = 0;
                    tails = 0;
                    flip = 0;
    
                    System.out.println( "How many times would you like to flip the coin?" );
                    System.out.print( "Flips: ");
                    fNum = keyboard.nextInt();
    
                    if (( fNum <= 0  ) && (fNum < 2100000000))// So that the user doesn't pick a negative number or goes beyond 2.1 Billion
                        {
                            System.out.println( "Sorry, you must pick a number greater than 0 and less than 2,100,000,000.  Try again");
                            fNum = keyboard.nextInt();
                        }
                    while ( flip < fNum )// Now that we have passed the conditional, the program will begin "flipping coins".
                    {
                            HorT = 1 + r.nextInt(2);
                            if ( HorT == 1 )
                            {
                                heads++;
                            }
                            else if ( HorT == 2 );
                            {
                                tails++;
                            }
                            flip++;// Ensures the continued flipping coins until the fNum = flip
                    }
    
                    System.out.println( "Number of heads: " + heads );
                    System.out.println( "Number of tails: " + tails );
                    
    
                    pHeads = ( (double)heads / fNum ) * 100;
                    pTails = ( (double)tails / fNum ) * 100;
    
                    System.out.println( "Probablity of heads: " + pHeads + "%" );
                    System.out.println( "Probability of tails: " + pTails + "%" );
                    // It is more likely that the probability will be 50% when the fNum is higher. It took multiple tries, but I found that the probability reaches 50% only when the number is higher than 100,000.
                }
            }

      
      



    

Picture of the output

Assignment Final1