Question

In: Computer Science

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 java.util.Random.?

Solutions

Expert Solution

Answer :

here is the fiip method as per your requirement

Raw code:

//importing Random

import java.util.Random;

//main

class Main {

//method static flip

public static int flip(){

//creating random object

Random rand= new Random();

//getting random number between 0 and 1

int randomNumber=rand.nextInt(2);

//return the generated number

return randomNumber;

}

//main

public static void main(String[] args) {

//calling the static method

if(flip()==1){

System.out.println("tail");

}

else{

System.out.println("head");

}

}

}

Editor:

output:

Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.

"Please refer to the screenshot of the code to understand the indentation of the code".

Thank you! Do upvote.


Related Solutions

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.
Create a histogram on excel for a coin toss. Flip the coin 6 times, each time...
Create a histogram on excel for a coin toss. Flip the coin 6 times, each time moving down to the left (for heads) and to the right (for tails). Consider it a success if when flipping a coin, the coin lands with the head side facing up. Then the probability of a success is .5 (p=.5) and the probability of failure is .5 (q=.5) for each event of flipping a coin. In this lab you will repeat a procedure of...
Create a histogram on excel for a coin toss. Flip the coin 6 times, each time...
Create a histogram on excel for a coin toss. Flip the coin 6 times, each time moving down to the left (for heads) and to the right (for tails). Consider it a success if when flipping a coin, the coin lands with the head side facing up. Then the probability of a success is .5 (p=.5) and the probability of failure is .5 (q=.5) for each event of flipping a coin. In this lab you will repeat a procedure of...
Write a program that determines the probability of tossing a coin 10 times and getting exactly...
Write a program that determines the probability of tossing a coin 10 times and getting exactly 0, 1, 2, 3, etc. heads. This is the binomial probability distribution. Store the probability in an array. You could get 0 heads or 10 heads or anything inbetween. Use a for loop. The for loop will go from 0 to 10 inclusive. Use r as the number of successes. So r will go from 0 to 10. The probability of a success is...
I toss a fair coin 20 times. (a) Calculate the probability of tossing 18 or more...
I toss a fair coin 20 times. (a) Calculate the probability of tossing 18 or more heads exactly. (b) Now perform the same calculation, approximating the actual binomial distribution with a normal distribution, picking a proper random variable, and using the correct mean and variance. (c) Do the results reasonably agree?
For which of these coin-tossing scenarios are you most likely to get heads on every toss?...
For which of these coin-tossing scenarios are you most likely to get heads on every toss? Explain your answer. Toss a coin 3 times. Toss a coin 5 times. Toss a coin 10 times .
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.
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...
Toss a coin 5 times. Let X denote the number of tails appeared. a. Write down...
Toss a coin 5 times. Let X denote the number of tails appeared. a. Write down the probability mass function of X. b. Write down the cumulative distribution function of X. c. Graph the cumulative distribution function of X. d. Find the expectation of E[X] e. Find the variance Var[X]
hink about the following game: A fair coin is tossed 10 times. Each time the toss...
hink about the following game: A fair coin is tossed 10 times. Each time the toss results in heads, you receive $10; for tails, you get nothing. What is the maximum amount you would pay to play the game? Define a success as a toss that lands on heads. Then the probability of a success is 0.5, and the expected number of successes after 10 tosses is 10(0.5) = 5. Since each success pays $10, the total amount you would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT