c++ Part 1 Write a class named Coin. The Coin class should have the following me
ID: 3849932 • Letter: C
Question
c++
Part 1
Write a class named Coin. The Coin class should have the following member variables:
A string named sideUp. The sideUp member variable will hold either "heads" or "tails" indicating the side of the coin that is facing up.
The Coin class should have the following member functions:
A default constructor that randomly determines the side of the coin that is facing up ("heads" or "tails") and initializes the sideUp member variable accordingly.
A void member function named toss that simulates the tossing of the coin. When the toss member function is called, it randomly determines the side of the coin that is facing up ("heads" or "tails") and sets the sideUp member variable accordingly.
A member function names getSideUp that returns the value of the sideUp member variable.
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 finishes.
Part 2
Create a game program using the Coin class from previeus. The program should have three instances of the Coin class: one representing a quarter, one representing a dime, and one representing a nickel.
When the game begins, your starting balance is $0. During each round of the game, the program will toss each of the simulated coins. When a tossed coin lands heads-up, the value of the coin is added to your balance. For example, if the quarter lands heads-up, 25 cents is added to your balance. Nothing is added to your balance for coins that lands 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. If your balance exceeds one dollar, you lose.
Explanation / Answer
part 1:
class Coin {
private String sideUp;
/**
* this is the Default constructor
*/
public Coin() {
// initializes sideUp
toss();
}
/**
* This method will simulates the tossing of a coin.
It should set the
* sideUp field to either "heads" or "tails"...
*/
public void toss() {
Random rand = new Random();
// Gets a random value, 0 or 1.
int value = rand.nextInt(2);
if (value == 0) {
this.sideUp = "heads";
} else {
this.sideUp = "tails";
}
}
/**
*
* @returns The side of the coin facing up.
*/
public String getSideUp() {
return sideUp;
}
}
static final int NUMBER_OF_TOSSES = 20;
public static void main(String args[]) {
// Creates an instance of the Coin class.
CoinTossSimulator coinTossSimulator = new CoinTossSimulator();
Coin myCoin = coinTossSimulator.new Coin();
// Displays initial toss
System.out.println("The side initially facing up: "
+ myCoin.getSideUp());
// Tosses the coin repeatedly.
System.out.println("Now I will toss the coin " + NUMBER_OF_TOSSES
+ " times.");
int headCount = 0;
for (int i = 0; i < NUMBER_OF_TOSSES; i++) {
// Tosses the coin.
myCoin.toss();
// Displays the side facing up.
System.out.println("Toss: " + myCoin.getSideUp());
if ("heads".equals(myCoin.getSideUp())) {
headCount++;
}
}
System.out.println("Heads facing up: " + headCount);
System.out
.println("Tails facing up: " + (NUMBER_OF_TOSSES - headCount));
}
output:The side initially facing up: heads
Now I will toss the coin 20 times.
Toss: tails
Toss: heads
Toss: tails
Toss: heads
Toss: tails
Toss: tails
Toss: tails
Toss: tails
Toss: tails
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: heads
Toss: tails
Toss: heads
Toss: tails
Heads facing up: 11
Tails facing up: 9
part 2:
static final double PLAY_TO = 1.00;
static final double TWENTY_FIVE_CENTS = 0.25;
static final double TEN_CENTS = 0.10;
static final double FIVE_CENTS = 0.05;
public static void main(String args[]) {
TossingCoinsForADollar coinTossSimulator = new TossingCoinsForADollar();
Coin quarter = coinTossSimulator.new Coin();
Coin dime = coinTossSimulator.new Coin();
Coin nickel = coinTossSimulator.new Coin();
double balance = 0;
System.out.println("Ready? Set? Go!");
// Plays the game while the balance
// here less than the PLAY_TO constant
while (balance < PLAY_TO) {
// toss each coin
quarter.toss();
dime.toss();
nickel.toss();
// adds appropriate value to balance
if (isHeadsUp(quarter)) {
balance += TWENTY_FIVE_CENTS;
}
if (isHeadsUp(dime)) {
balance += TEN_CENTS;
}
if (isHeadsUp(nickel)) {
balance += FIVE_CENTS;
}
}
// Displays balance to user
System.out.printf("Balance: $%,1.2f ", balance);
// Displays whether or not they won based
// on the program requirement
if (balance == PLAY_TO) {
System.out.println("You win!");
} else {
System.out.println("You did not win.");
}
}
/**
* Method will determines if the coin is heads up
*
* @param coin
* @returns true if coin is heads up
*/
public static boolean isHeadsUp(Coin coin) {
if (coin.getSideUp().equals("heads")) {
return true;
} else {
return false;
}
}
output:Ready ? Set ? Go !
Balance: $1.22
You did not win.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.