# 1st Semester Final; *FinalProg*

## Code

// Donovan Rich
// Period 6
// Final Exam
// FinalProg.java
// 1/22/2016
import java.util.Random;
import java.util.Scanner;
public class FinalProg
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int outcome, flips, heads=0, tails=0, bouts=0;
do
{
System.out.println( "How many coin flips would you like to do?" );
flips = keyboard.nextInt();
if ( flips < 1 || flips > 2100000000 )
System.out.println( "Your selected number is not valid. Try again." );
else
{
while ( bouts < flips )
{
outcome = 1 + r.nextInt(2);
if ( outcome == 1 )
heads++;
else if ( outcome == 2 )
tails++;
bouts++; // I chose to use the "bouts" variable because I wasn't sure how to get out of the do-while
} // loop using just the "flips" variable.
}
} while ( flips < 1 || flips > 2100000000 ); // I decided to use a do while because it made more sense
// to make this part run at least once.
System.out.println( "Flips complete.");
System.out.println( "Heads flipped=[" + heads + "]" );
System.out.println( "Tails flipped=[" + tails + "]" );
double headProb=(double)heads/flips*100, tailProb=(double)tails/flips*100;
System.out.println( "Probability of rolling heads=[" + headProb + "%]" );
System.out.println( "Probability of rolling tails=[" + tailProb + "%]" );
}
}
// Obviously, the closer your selected number is to the upper bound (2,100,000,000),
//the more precise the probabilities will be to 50% heads, 50% talis. However, I was able to get values accurate to ±0.1% using 1,000,000 flips.

### Picture of the output