Write a class named Coin. The Coin class should have the following field: A stri
ID: 3563639 • Letter: W
Question
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 sideUp field accordingly
A void method named toss that simulates the tossing of the coin. When the toss method is called, it randomly determines the side of the coin that is facing up " heads or tails: and sets the sideUp field accordingly
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 heads is facing up and the number of times tails is facing up and display those values after the loop is finished.
Then
Please create, a game program using the Coin class. The program should have 3 instances of the Coin class: one representing a quarter, one representng a dime, and one representing a nickel.
When the game begins you starting balance is $0. During each round of the game the program will toss the simulated coins. When a coin is tossed, the value of the coin is added to your balance if it lands heads up. For example, if the quarter lands heads up 25 cents is added to your balance. Nothing is added to your balance for coins that land tails up. The game is over when your balance reaches one dollar or more. If your balance is exactly one dollar you win the game. You lose if your balance exceeds one dollar.
Explanation / Answer
import java.util.Random; public class Coins { //constants private static final int HEADS = 0; private static final int TAILS = 1; //constants private final int n_coins; //instance variables private int n_heads; private int n_tails; private Random randomizer; //constructors public Coins() { n_tails = 0; n_heads = 0; n_coins = 3; randomizer = new Random(); } public Coins(int new_n_coins) throws Exception { if(new_n_coins < 1) throw new Exception("Coins constructor number of coins is less than 1"); n_coins = new_n_coins; n_tails = 0; n_heads = 0; randomizer = new Random(); } //custom method public void tossCoins() { n_tails = 0; n_heads = 0; for(int toss_counter = 1; toss_counterRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.