Question

In: Computer Science

Write a program that simulates the flipping of a coin n times, where n is specified...

Write a program that simulates the flipping of a coin n times, where n is specified by the user. The program should use random generation to flip the coin and each result is recorded. Your program should prompt the user for the size of the experiment n, flip the coin n times, display the sequence of Heads and Tails as a string of H (for Head) and T (for Tail) characters, and display the frequencies of heads and tails in the experiment. Use functions for full credit.

Solutions

Expert Solution

import java.util.*;
class Main
{
        public void toss(int n)
        {
                int count1=0,count2=0;
                String a[]=new String[n];
                Random random = new Random(); 
                for(int i=0;i<n;i++)
                {
        int rnd = random.nextInt(2); 
        if(rnd==0)
        {
           a[i]="T";
           count1++;
        }
        if(rnd==1)
        {
                a[i]="H";
                count2++;
        }
                }
        for(int i=0;i<n;i++)
        {
        System.out.print(a[i]+" ");     
        }
        System.out.println("\nThe frequency of Heads is "+count2);
        System.out.println("The frequency of Tails is "+count1);
                
        }
        public static void main(String args[])
        {
                Main m=new Main();
                Scanner sc=new Scanner(System.in);
                System.out.print("Enter the number of time you want to toss a coin : ");
                int n=sc.nextInt();
                m.toss(n);
        }
}

Here is the program that uses random integer generator that will ask the user for n value and give it to a function then uses random value for n times and generate H and T and store them in the array and then finally print the freqencies of head and tails and then print the array of randomly generated H and T.

SCREENSHOT OF THE OUTPUT :

*************************************************PLEASE DO UPVOTE********************************************************


Related Solutions

Write a program that simulates flipping a coin repeatedly until three consecutive        heads are...
Write a program that simulates flipping a coin repeatedly until three consecutive        heads are tossed. The program should then display the total number of times the coin was        flipped. The user does not have to enter any information and we must use a while loop.
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Consider flipping nn times a coin. The probability for heads is given by pp where pp...
Consider flipping nn times a coin. The probability for heads is given by pp where pp is some parameter which can be chosen from the interval (0,1)(0,1). Write a Python code to simulate nn coin flips with heads probability pp and compute the running proportion of heads X¯nX¯n for nn running from 1 to 1,000 trials. Plot your results. Your plot should illustrate how the proportion of heads appears to converge to pp as nn approaches 1,000. In [ ]:...
Write an application that simulates coin tossing. Let the program toss a coin each time the...
Write an application that simulates coin tossing. Let the program toss a coin each time the user chooses the “Toss Coin” menu option. Count the number times each side of the coin appears. Display the results. The program should call a method flip( ) that takes no arguments and returns a zero to represent a tail or a one to represent a head. There is a Random class that will allow you to generate a random integer. Import it from...
An experiment consists of flipping a coin 5 times and noting the number of times that...
An experiment consists of flipping a coin 5 times and noting the number of times that a heads is flipped. Find the sample space SS of this experiment.
In three independent flips of a coin where there is a 54​% chance of flipping a...
In three independent flips of a coin where there is a 54​% chance of flipping a tail​, let A denote​ {first flip is a tail​}, BE denote​ {second flip is a tail​}, C denote​ {first two flips are tail​s}, and D denote​ {three tails on the first three​ flips}. Find the probabilities of​ A, B,​ C, and​ D, and determine​ which, if​ any, pairs of these events are independent. ​P(A)equals nothing ​(Round to two decimal places as​ needed.) ​P(B)equals nothing...
Consider flipping a coin 5 times, and define the following events: A = "The very first...
Consider flipping a coin 5 times, and define the following events: A = "The very first flip is tails" B ="The first three flips are tails" C = "The middle (third) flip is tails" D = "The last three flips are tails" E = "The very last flip is tails" (1) Which of the following collections of events (if any) are pairwise independent? (2) Which of the following collections of events (if any) are mutually independent? (3) Which of the...
Consider flipping a coin 5 times, and define the following events: A = "The very first...
Consider flipping a coin 5 times, and define the following events: A = "The very first flip is tails" B ="The first three flips are tails" C = "The middle (third) flip is tails" D = "The last three flips are tails" E = "The very last flip is tails" 1)Which of the following collections of events (if any) are pairwise independent? a) {A,B,C} b) {B,C,D} c) {B,D,E} d) None 2) Which of the following pairs of events (if any)...
[Counting and Probability] Consider the experiment of flipping a coin four times. a. Using a tree,...
[Counting and Probability] Consider the experiment of flipping a coin four times. a. Using a tree, determine the probability of one or two tails, with a biased coin with P(H) = 2/3. Compare to the probability with an unbiased coin b. [Bayes’ Rule] Using the results of the part a suppose we have two coins, one unbiased, and a biased coin with P(H) = 2/3. We select a coin at random, flip it three times, and observe either one or...
Write a program to toss a coin multiple times. Ask the user for how many times...
Write a program to toss a coin multiple times. Ask the user for how many times to toss the coin and then display the percentage of heads and tails (do not display each toss, just the totals). JAVA
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT