Write a class called CoinToss . The CoinToss class should have the following fie
ID: 3547702 • Letter: W
Question
Write a class called CoinToss. The CoinToss class should have the following fields:
A private int named sideUp. The sideUp field will hold either 1 for heads or 2 for tails
A private Random object named tosser (see page 309 for information on the Random class).
A static int headCount that counts the number of tosses that resulted in heads.
A static int tailCount that counts the number of tosses that resulted in tails. Adding the two static fields together will give the total number of tosses, so we don't have to have a separate variable for that. If you want to make a static tossCount variable, you may.
The CoinToss class should have the following methods:
A no-arg constructor that creates a new Random object and assigns it to tosser. It then uses the nextInt method of the Random class to determine the side of the coin that is facing up by getting an integer 1 or 2 for heads or tails. It should initialize the sideUp field and the headCount and tailCount fields according to this initializing toss.
A void method called toss that simulates the tossing of the coin. When the toss method is called, it uses tosser to randomly determine the side of the coin that is facing up in a similar manner to the constructor and sets the sideUp, headCount and tailCount fields accordingly. It is very similar to the constructor.
A method called getSideUp that returns the value of the sideUp field.
A method called getStringSideUp that returns the String equivalent of the side that is up, "Heads" or "Tails".
Write a program that demonstrates the CoinToss class. The program should create an instance of the class and ask the user to guess heads or tails by entering a 1 or a 2. It should tell the user if their guess was correct or not and ask for another guess and toss the coin. Count the number of times the user guesses correctly.
If the user enters something other than 0, 1 or 2, display an error message and ask them to enter again.
At the end, tell the user how many coin tosses there were and how many were heads or tails (using the static class variables). Also tell the user how many guesses were correct.
A sample run might look something like the one below (feel free to improve on it). In the summary output, if there is only 1 Head or 1 Tail, the output should use correct English and say
Explanation / Answer
Write a class called CoinToss . The CoinToss class should have the following fie
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.