James is holding his annual coin flipping contest, and to prepare for it he woul
ID: 3633345 • Letter: J
Question
James is holding his annual coin flipping contest, and to prepare for it he would like you to help him practice. Write James a program that asks the user how many times he would like his coin to flip (he can flip up to 1000 times). Then randomly generate a number between 1 and ten, store all the numbers in an array. With your newly created array, supply James with the following information:- How many times each number out of 10 showed up.
-What number showed up the most.
- If even numbers are heads, and odd numbers are tails, what side of the coin came up the most?
Please ensure that the program is well commented.
***Also- Add another input for James Asking him how many times he would like to perform his run through the flips (example, 3 flips of 30, 2 flips of 100) your output will now have a comparison between each trial. Try to incorporate as many arrays as possible!!! Submit a README.txt file
p.s. I am using java 1.4.2 and I cannot use any newer. I cannot use classes like scanner and such. Please ensure that the program works with 1.4.2 an not any newer. Thank you
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class CoinFlips
{
public static void main(String[] args) throws NumberFormatException, IOException
{
//Maximum flips
final int MAX_FLIPS = 1000;
InputStreamReader inp=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(inp);
int nUserFlips = 0;
//Reading user input until he enters a valid input
while(true)
{
System.out.println("Enter no of Flips(<1000) :" );
nUserFlips = Integer.parseInt(input.readLine());
//Check whether the input values exceeding the max allowed flips or not
//If yes, throw an error and ask the user to qnother value
if(nUserFlips > MAX_FLIPS)
System.out.println("Error : No of flips should be less than "+MAX_FLIPS+".");
else
break;
}
Random rand = new Random();//Random class for generating the Random Numbers
//Random numbers should be in [1,10]
int min = 1, max = 10;
//Array for storing coin flip values
int[] flips = new int[nUserFlips];
//Frequency count for 1...10 numbers
int[] freq = new int[10];
int nEvenCount = 0, nOddCount = 0;
//loop for generating Number and count even number and odd numbers
for(int i=0;i<nUserFlips;i++)
{
rand = new Random();
flips[i] = min + rand.nextInt(max-min);
if(flips[i]%2 == 0)
nEvenCount++;//Even numbers
else
nOddCount++;//Odd Numbers
}
//Loop for calculating frequency
for(int i=1;i<=10;i++)
{
int temp = 0;//Initial Frequency count for each number
//Loop through the flips and count frequency for each number
for(int j=0;j<nUserFlips;j++)
{
if(flips[j] == i)
temp++;
}
freq[i-1] = temp;//Inserting freq to an array
System.out.println("The number "+i+" showed up "+temp+" Time(s).");
}
//Finding out the number with max occuring frequency
int biggestCount = 1;
int biggest = freq[0];
for(int i=1;i<10;i++)
{
if(biggest < freq[i])
{
biggest = freq[i];
biggestCount = (i+1);
}
}
System.out.println("number "+biggestCount+" showed up the most.");
if(nEvenCount > nOddCount)
{
System.out.println("heads came up the more");
}
else if(nEvenCount > nOddCount)
{
System.out.println("tails came up the more");
}
else
{
System.out.println("Both heads and tails came up the same no of times");
}
}
}
Got confused a little with this text so i haven;t done anything related to this part, ***Also- Add another input for James Asking him how many times he would like to perform his run through the flips (example, 3 flips of 30, 2 flips of 100) your output will now have a comparison between each trial.
Check and let me know, if i need to add something to the program.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.