Semester 1 Final
Code
/// Name: Drew Boxold
/// Period: 5
/// Program Name: FinalExam
/// Date: 1/20/16
import java.util.Random;
import java.util.Scanner;
public class FinalExam
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random rng = new Random();
int heads = 0;
int tails = 0;
int n = 0;
System.out.println("--------------------------------------");
System.out.println("Automatic Coin Flipper 9000: ");
System.out.println("How many coins will you like to flip?");
System.out.print(">");
int flips = keyboard.nextInt();
do
{
int flip = rng.nextInt(2);
if ( flip == 1 )
tails++;
else
heads++;
n++;
}
while ( n != flips);
System.out.println(" ");
System.out.println(" ");
System.out.println("Nice job!");
System.out.println(" ");
System.out.println("--------------------------------------");
System.out.println(" ");
System.out.println("Heads:" + heads + "");
System.out.println("Tails:" + tails + "");
System.out.println(" ");
System.out.println("--------------------------------------");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
double probOfTails = (double)tails / flips;
double probOfHeads = (double)heads / flips;
System.out.println("Probability Heads:" + probOfHeads*100 + " %");
System.out.println("Probability Tails:" + probOfTails*100 + " %");
}
}
/// I used a do while loop because it stops once it has the number of flips.
Picture of the output