Assignemnt Final Exam and Final Exam Program
Code
/// Name: Tristan Chung
/// Period: 5
/// Program Name: Final Exam
/// File Name: FinalExam.java
/// Date Finished: 1/20/2016
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("Welcome to the Coin Flip Simulation!");
System.out.println("How many coin flips would you like?(1- 2.1mill)");
System.out.print("> ");
int flips = keyboard.nextInt();
do
{
int flip = rng.nextInt(2);
if ( flip == 1 )
heads++;
else
tails++;
n++;
}
while ( n != flips);
System.out.println("You rolled " + heads + " heads and " + tails + " tails.");
System.out.println(" ");
double probOfHeads = (double)heads / flips;
double probOfTails = (double)tails / flips;
System.out.println("And the probability that you will get heads is " + probOfHeads*100 + "%.");
System.out.println("And the probability that you will get tails is " + probOfTails*100 + "%.");
// 1. I used a do while loop, because it will change the integer n before the condition is checked. Thus it will stop at the correct number given. You need 3 integer variables for the number of heads, numbger of tails, and number of coin flips
// 2. I concluded that 14 flips gets a consistently close probablitity to 50%.
}
}
Picture of the output