Assignemnt FallFinal and Semester 1 Final
Code
/// name: Leila Ordukhani
/// period: 6
/// program name: Final 1 Program
/// file name: FallFinal.java
/// date finished: 1/22/16
import java.util.Scanner;
import java.util.Random;
public class FallFinal
{
public static void main ( String[] args )
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
int again = 0;
int flips, heads, tails;
heads = 0;
tails = 0;
//initialize integers
System.out.print("How many flips do you want? ");
flips = keyboard.nextInt();
while ( flips < 1 || flips > 2100000)
{
System.out.println("Number is invalid. Choose ");
if ( flips < 1)
{
System.out.println("a positive number.");
}
else if ( flips > 2100000 )
{
System.out.println("a number less than 2,100,000");
}
System.out.println("Pick a number between 1 and 2,100,000");
flips = keyboard.nextInt();
}
do
{
int flip = r.nextInt(2);
String coin;
if ( flip == 1 )
{
coin = "heads";
again++; //keep track of total flips
heads++; //keep track of heads
}
else
{
coin = "tails";
again++; //keep track of total flips
tails++; //keep track of tails
}
System.out.println("The coin flip is " + coin);
} while ( again < (flips) ); //total flips can not be more than requested flips
//while loop to repeat the flips
double headprobability = (double)heads/flips;
double tailprobability = (double)tails/flips;
//find mathematical probability
double headpercent = (headprobability * 100);
double tailpercent = (tailprobability * 100);
//make probability a percentage
System.out.println("There were " + heads + " heads and " + tails + " tails.");
System.out.println("The probability of getting heads was " + headpercent);
System.out.println("The probability of getting tails was " + tailpercent);
//percentage of heads and tails based on the flips
//The consistant number needed to get close to 50 percent probability is 10 of multiples of 10 because percentage is out of 100
}
}
Picture of the output