Heads or Tails Game
This game is meant for two or more players. In this game, the players take turns flipping a coin. Before the coin is flipped, players should guess if the coin will land headsup or tailsup. If a player guesses correctly, then that player is awarded a point. If a player guesses incorrectly, then that player will lose a point. The first player to score five points is the winner.
Write a program that simulates the game being played by two players. Use the Coin class that you wrote as an assignment in Chapter 5 (Programming Challenge 20) to simulate the coin. Write a Player class to simulate the players.
Programming Challenge 20
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 calls the toss method, described next.
• A void method named toss that simulates the tossing of the coin. When the toss method is called, it generates a random number in the range of 0 through 1. If the random number is 0, then it sets the sideUp field to “heads”. If the random number is 1, then it sets the sideUp field to “tails”.
• A method named getSideUp that returns the value of the sideUp field.
Write a program that demonstrates the Coin class. The program should create an instance of the class and display the side that is initially facing up. Then, use a loop to toss the coin 20 times. Each time the coin is tossed, display the side that is facing up. The program should keep count of the number of times a head is facing up and the number of times a tail is facing up and display those values after the loop finishes.
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.