Question

Tosssing Coins for a Dollar (C++) For this assigment, you will create a game program using...

Tosssing Coins for a Dollar (C++)

For this assigment, you will create a game program using the Coin class from Programming Challenge 12 (Coin Toss Simuator). The prgram 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 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 a 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 reachers $1 or more. If your balance is exactly $1, you win the game. You lose if your balance exceeds $1.

Make sure to include comments in your code.

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

Thanks for the question.


Here is the completed code for this problem. Comments are included so that you understand whats going on. Let me know if you have any doubts or if you need anything to change.


Thank You !!
=============================================================================

#ifndef COIN_H
#define COIN_H


class Coin {
public:
Coin(double);
bool tossCoin(void);
double getFaceValue() const;

private:
double faceValue;
};

#endif // COIN_H

=============================================================================

#ifndef COIN_H
#define COIN_H


class Coin {
public:
Coin(double);
bool tossCoin(void);
double getFaceValue() const;

private:
double faceValue;
};

#endif // COIN_H

=============================================================================

#include <iostream>
#include<iomanip>
#include "Coin.h"

using namespace std;

int main() {
Coin quarter = Coin(0.25); // creating an object of Coin class with face value as 0.25
Coin dime = Coin(0.10); // creating an object of Coin class with face value as 0.10
Coin nickel = Coin(0.05); // creating an object of Coin class with face value as 0.05

double dollar =0.0; // will store the total amount collected after tossing the game
int round = 1; // just to jazz it up

cout<<fixed<<showpoint<<setprecision(2);
while(true) {

// code to simulate tossing a quarter coin
cout<<"Tossing Quarter..";
if(quarter.tossCoin()) {
cout<<"Its a Head. Adding $0.25 to the total"<<endl;
dollar+=quarter.getFaceValue(); // add the amount if its a head
} else {
cout<<"Its a Tail."<<endl;
}
// code to simulate tossing a dime coin
cout<<"Tossing Dime..";
if(dime.tossCoin()) {
cout<<"Its a Head. Adding $0.10 to the total"<<endl;
dollar+=dime.getFaceValue(); // add the amount if its a head
} else {
cout<<"Its a Tail."<<endl;
}
//// code to simulate tossing a nickel coin
cout<<"Tossing Nickel..";
if(nickel.tossCoin()) {
cout<<"Its a Head. Adding $0.05 to the total"<<endl;
dollar+=nickel.getFaceValue(); // add the amount if its a head
} else {
cout<<"Its a Tail."<<endl;
}

// print the total amount collected at every round
cout<<"You collected $"<<dollar<<" in round : "<<round++<<endl;

// validate to continue playing the game or stop
if(dollar<1.00) {
continue;
} else if(dollar>1.00) {
cout<<"Total amount exceeds $1.00. You lost the game."<<endl;
} else {
cout<<"You collected exactly $1.00. You won the game."<<endl;
break;
}
}


return 0;
}

=============================================================================

thanks !

Add a comment
Know the answer?
Add Answer to:
Tosssing Coins for a Dollar (C++) For this assigment, you will create a game program using...
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 programming language! 1. Tossing Coins for a Dollar For this assignment you will create a...

    Java programming language! 1. Tossing Coins for a Dollar For this assignment you will create a game program using the Coin class from Programming Challenge 12. 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 the simulated coins. When a coin is tossed, the value of the...

  • implement coinclass and driver program within one source file, i.e. do not separate class specifications with...

    implement coinclass and driver program within one source file, i.e. do not separate class specifications with implementation Its a c++ problem 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following member variable: • 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...

  • c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in...

    c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in a single program (Gaddis, p812, 9E). Scans of these problems are included below. Your program should have two sections that correspond to Problems 12 and 13: 1) As described in Problem 12, implement a Coin class with the specified characteristics. Run a simulation with 20 coin tosses as described and report the information requested in the problem. 2) For the second part of your...

  • 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...

  • # 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...

  • Suppose you have a six sided die. One face is printed with the number 1.  Two faces...

    Suppose you have a six sided die. One face is printed with the number 1.  Two faces are printed with the number 2. Three faces are printed with the number 3. You also have 3 coins:  C_1, C_2, and C_3. C_1 will land Heads with probability 1/5. C_2 will land Heads with probability 1/3. C_3 will land Heads with probability 1/2. You roll the die. If the die lands with a 1 face up, flip coin C_1 If the die lands with...

  • Suppose you have a six sided die. One face is printed with the number 1. Two...

    Suppose you have a six sided die. One face is printed with the number 1. Two faces are printed with the number 2. Three faces are printed with the number 3. You also have 3 coins: C_1, C_2, and C_3. C_1 will land Heads with probability 1/3. C_2 will land Heads with probability 1/5. C_3 will land Heads with probability 1/4. You roll the die. If the die lands with a 1 face up, flip coin C_1 If the die...

  • Suppose you have a six sided die. One face is printed with the number 1. Two...

    Suppose you have a six sided die. One face is printed with the number 1. Two faces are printed with the number 2. Three faces are printed with the number 3. You also have 3 coins: C_1, C_2, and C_3. C_1 will land Heads with probability 1/5. C_2 will land Heads with probability 1/3. C_3 will land Heads with probability 1/2. You roll the die. If the die lands with a 1 face up, flip coin C_1 If the die...

  • Suppose you have a six sided die. One face is printed with the number 1. Two...

    Suppose you have a six sided die. One face is printed with the number 1. Two faces are printed with the number 2. Three faces are printed with the number 3. You also have 3 coins: C_1, C_2, and C_3. C_1 will land Heads with probability 1/5. C_2 will land Heads with probability 1/3. C_3 will land Heads with probability 1/2. You roll the die. If the die lands with a 1 face up, flip coin C_1 If the die...

  • A box contains five coins. For each coin there is a different probability that a head...

    A box contains five coins. For each coin there is a different probability that a head will be obtained when the coin is tossed. (Some of the coins are not fair coins!) Let pi denote the probability of a head when the i th coin is tossed (i = 1, . . . , 5), and suppose that p1 = 0, p2 =1/4, p3 =1/2, p4 =3/4, p5 =1. The experiment we are interested in consists in selecting at random...

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