Question

The Coin Tossing simulation Coin.java The Coin class must be the enum class, defining the instances...

The Coin Tossing simulation Coin.java The Coin class must be the enum class, defining the instances HEADS and TAILS. CoinTossing.java The CoinTossing class should be a simple class (with a main method) that uses the values provided by a Coin class, and performs a simulation of the tossing of a coin.

There must be a separate (possibly static) flip method that takes no arguments and returns a value of the Coin class. The result returned from the flip method is the random result from the flipping of a coin. You may choose which class of which this method is to be a member. Write the CoinTossing class so that it may do as many tosses as the user requests at each iteration.

The user inputs an integral value saying how many additional tosses are to be performed each time (say, something like 1000000), and the program counts how many times each side of the coin appears. The program continues looping asking the user for additional tosss until the user enters “0”, indicating that the user wishes no more tosses of the coin. At this point, the program terminates. After each entry from the user, output the total cumulative count (adding up the total from the start of the execution of the program) of each of the possible outcomes, so that it may be determined whether the simulated coin appears to be “fair” (i.e., has approximately as many HEADS as TAILS). [The program will be tested by asking for something like one million tosses, then about another million to see what the result looks like after a total of near two million tosses, and so on.]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here are the two files Coin.java and CoinTossing.java, refer the screenshot for the sample output i ran using the code.

______________________________________________________________________________________________

public enum Coin {

    HEADS, TAILS
}

________________________________________________________________________________________________

import java.util.Scanner;
import java.util.Random;

public class CoinTossing {

    private static Random random = new Random();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.print("How many tosses you want to perform: ");
            int experiment = scanner.nextInt();
            if (experiment == 0) {
                System.exit(0);
            } else {

                int countHeads = 0, countTails = 0;
                for (int count = 1; count <= experiment; count++) {

                    if (flip() == Coin.HEADS) {
                        countHeads += 1;
                    } else {
                        countTails += 1;
                    }
                }

                System.out.println(String.format("%-10s%10d%10.2f%1s", "HEADS", countHeads, 100.0 * countHeads / experiment, "%"));
                System.out.println(String.format("%-10s%10d%10.2f%1s", "TAILS", countTails, 100.0 * countTails / experiment, "%"));
                System.out.println(String.format("%-10s%10d%10.2f%1s", "TOTAL", experiment, 100.0 * experiment / experiment, "%"));
            }
        }


    }

    public static Coin flip() {
        return random.nextInt(2) == 0 ? Coin.HEADS : Coin.TAILS;
    }
}

__________________________________________________________________________________________

thanks a lot !

please please a thumbs up : )

Add a comment
Know the answer?
Add Answer to:
The Coin Tossing simulation Coin.java The Coin class must be the enum class, defining the instances...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss...

    # JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss coin randomly and track the count of heads or tails. You need to write a program that can perform following operations: a. Toss a coin randomly. b. Track the count of heads or tails. c. Display the results. Design and Test Let's decide what classes, methods and variables will be required in this task and their significance: Write a class called Coin. The Coin...

  • Write a C++ program that simulates tossing a coin. Allow the user to enter the number...

    Write a C++ program that simulates tossing a coin. Allow the user to enter the number of tosses. Print the number of tosses that yielded heads and the number of tosses that yielded tails. use function

  • Write a C++ program that simulates coin tossing. For each toss of the coin the program...

    Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses.   The program should have the following functions as a minimum: void toss() - called from main() and will randomly toss the coin and set a variable equal to the...

  • Using c++ Write a function named coinToss that simulates tossing a coin. When you call the...

    Using c++ Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2. 1 represents heads and 2 represents tails. Exercise 1 (40 points) Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2.1 represents heads and 2 represents tails Use the function...

  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

  • In C++ please Create a coin-flipping game. Ask the user how many times to flip the...

    In C++ please Create a coin-flipping game. Ask the user how many times to flip the coin, and use the random function to determine heads or tails each time a coin is flipped. Assume the user starts with $50. Every time the coin is flipped calculate the total (heads +$10, tails -$10). Create another function to test if the user has gone broke yet (THIS FUNCTION MUST RETURN A BOOLEAN TRUE/FALSE VALUE). End the program when the user is broke...

  • 3. Suppose my friend and I are tossing a biased coin (the chance of the coin...

    3. Suppose my friend and I are tossing a biased coin (the chance of the coin landing heads is 0.48). I get one dollar each time the coin lands heads, and I have to pay one dollar to my friend each time it lands tails. I will stop playing if my net gain is three dollars. (a)What is the chance that I will stop after exactly three tosses? (b) What is the chance that I will stop after exactly four...

  • Problem 02 Design a finite-state machine that records the results of flipping a coin a certain nu...

    Problem 02 Design a finite-state machine that records the results of flipping a coin a certain number of times. The state machine takes as an input flip which is zero for heads and one for tails. The states are labeled so as to indicate the numbers of heads and the nurnber of tails, NONE. H. T, HH, TT HT, HHH, etc. The order that the heads or tails was flipped does not matter, only the total numbers of heads and...

  • You have a biased coin, where the probability of flipping a heads is 70%. You flip...

    You have a biased coin, where the probability of flipping a heads is 70%. You flip once, and the coin comes up tails. What is the expected number of flips from that point (so counting that as flip #0) until the number of heads flipped in total equals the number of tails?

  • Q.1 (25') Pony is playing coin tossing game with Yanny. They found the coin have 4...

    Q.1 (25') Pony is playing coin tossing game with Yanny. They found the coin have 4 heads and 6 tails in 10 flips. Let p be the probability for obtaining a head, based on the first 10 flips a) Can we conclude it is a biased or fair coin base on the result above? b) Plot the Bernoulli's PMF What is the probability for obtaining 6 heads in 10 flips using the same coin? d) What is the probability for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT