Semester 1 Final
code
///Name: Mutsu Osoegawa
///Period: 7
///Project Name: Final Exam Program
///FileName: Semester1.java
///Date: 1/21/2016
import java.util.Random;
import java.util.Scanner;
public class Semester1
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int flipNumber, flipCount, side, heads, tails;
double headsProbability, tailsProbability;
heads = 0;
tails = 0;
flipCount = 0;
System.out.println( "How many coin flips do you want? " );
System.out.print( "> " );
flipNumber = keyboard.nextInt();
if ((flipNumber <= 0 ) && (flipNumber < 2100000000)) //Prevents user from picking a negative number or a number bigger than the maximum integer value.
{
System.out.println( "You must pick a number between 0 to 2,100,000,000." );
flipNumber = keyboard.nextInt();
}
while ( flipCount < flipNumber ) //begins flipping coins.
{
side = 1 + r.nextInt(2); //Creats a random flip 1 or 2.
if ( side == 1 )
{
heads++;
}
else if ( side == 2 )
{
tails++;
}
flipCount++; //Keeps flipping until this counter reaches the same number as flipNumber.
}
System.out.println( "Total heads: " + heads );
System.out.println( "Total tails: " + tails );
headsProbability = ( (double)heads / flipNumber ) * 100;
tailsProbability = ( (double)tails / flipNumber ) * 100;
System.out.println( "Probablility of flipping heads: " + headsProbability + "%" );
System.out.println( "Probability of flipping tails: " + tailsProbability + "%" );
//The higher the value of the flipNumber, the closer the Probability of each value reaching 50%.
//A flipNumber value higher than 100,000 will approach closer to a 50% probability.
}
}