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...
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 .
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...
Consider an experiment: tossing a coin three times, and observe head/tail facing up for each time....
Consider an experiment: tossing a coin three times, and observe head/tail facing up for each time. a) What is the sample space? b) List sample points in the event A that there are more tails than heads. c) List sample points in the event B that exact 2 tails face up. d) find the probabilities: P[A], P[B], P[A∩ B], P[AU B].
A coin is tossed twice. Let Z denote the number of heads on the first toss...
A coin is tossed twice. Let Z denote the number of heads on the first toss and W the total number of heads on the 2 tosses. If the coin is unbalanced and a head has a 40% chance of occurring, find the correlation between W and Z.
A coin is tossed twice. Let Z denote the number of heads on the first toss...
A coin is tossed twice. Let Z denote the number of heads on the first toss and W the total number of heads on the 2 tosses. If the coin is unbalanced and a head has a 40% chance of occurring, find the correlation between W and Z
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT