Assignment: Semester One Final
Code
/// Name: Graham Pollock
/// Period: 5
/// Program Name: Final Project 1
/// File Name: FinalProj1.java
/// Date Finished: 1/20/2016/
import java.util.Random;
import java.util.Scanner;
public class FinalProj1
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random rng = new Random();
int AnotherOne = 0;
int flips, Heads, Tails;
Heads = 0;
Tails = 0;
//initialize integers
System.out.print( "How Many times would you like to flip? " );
flips = keyboard.nextInt();
while ( flips < 1 || flips > 2100000000 )
{
System.out.println("Sorry... Your number was ");
if ( flips < 1 )
{
System.out.println("too small.");
}
else if ( flips > 2100000000 )
{
System.out.println("too large.");
}
System.out.println("Please select a number between 1 and 2,100,000,000.");
flips = keyboard.nextInt();
}
do
{
int flip = rng.nextInt(2);
String coin;
if ( flip == 1 )
{
coin = "Heads";
AnotherOne++; //keep track of total flips
Heads++; //keep track of heads
}
else
{
coin = "Tails";
AnotherOne++; //keep track of total flips
Tails++; //keep track of heads
}
System.out.println ( "The coin flips and is... " + coin );
} while ( AnotherOne < (flips) ); //total flips can not be more than flips requested.
//while loop to repete the flips.
double probofheads = (double)Heads / flips;
double proboftails = (double)Tails / flips;
//find mathmatical probablility
double percentheads = (probofheads * 100);
double percenttails = (proboftails * 100);
//make probablitlity into percentage
System.out.println("There were " + Heads + " Heads flipped and " + Tails + " Tails flipped."); //count total heads and tails
System.out.println("The Probablity of getting Heads was " + percentheads +"%");
System.out.println("The Probablity of getting Tails was " + percenttails +"%");
//percentage heads and tails out of flips
//The consistant number or numbers needed to get as close to a 50 percent probability is 10 or are exponents of 10 due to the fact that the percentage of an occurance is out of 100.
}
}
Picture of the output