Coin.h (class specification file) Coin.cpp (class implementation file) 200_assig
ID: 3751463 • Letter: C
Question
Coin.h (class specification file)
Coin.cpp (class implementation file)
200_assign3.cpp (application program)
Your solution should use all three files listed above.
The Coin.h file (class specification file) should contain the class declaration, the declaration of the member variables, and prototypes for the constructor and the member functions. Nothing should be implemented in this file.
The Coin.cpp file (class implementation file) should contain the implementation of the constructor and the member functions.
The 200_assign3.cpp file (application program) should contain the game logic for the coin toss game.
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 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 named getSideUp that returns the value of the sideUp member variable.
UML Diagram for the Coin class:
Coin
-sideUp: string // Stores either “heads” or “tails”
<<Constructor>> +Coin()
+toss()
+getSideUp(): string
Game Logic (200_assign3.cpp):
The game works as follows (as you will see in the sample runs below):
The player (you) and the computer both begin with a starting balance of $0.00. For each round of the game, both the player and the computer toss a quarter, a nickel, and a dime. For each type of coin, if the coin lands on heads, the amount of the coin is added to the player’s balance. If the coin lands on tails, however, nothing is added to the player’s balance for that coin.
For example:
The player gets heads for the quarter, heads for the dime, and tails for the nickel. $0.25 + $0.10 = $0.35 is added to the players balance for that round.
The computer gets tails for the quarter, tails for the dime, and heads for the nickel. $0.05 is added to the computer’s balance for that round.
The maximum amount that can be added to a player’s balance in a single round is $0.25 + $0.10 + $0.05 = $0.40 (heads for all three coins). The minimum amount that can be added to a player’s balance in a single round is $0.00 (tails for all three coins).
The game ends when at least one player has a balance of $1.00 or more. The following are conditions for win/loss/tie:
- If both players have the same balance at the end, they tie (note that this means both players not only have the same score, but their shared score is $1.00 or more)
- If one player has a balance of less than $1.00 when the game ends, the other player who has a balance of $1.00 or more wins. For example, if the player has $0.90 and the computer has $1.05, the computer with $1.05 wins (remember it’s not possible for both players to have less than $1.00 when the game ends)
- If both players have a balance of $1.00 or more, the player with the lower score wins. For example if the player ends the game with $1.10, and the computer ends the game with $1.30, the player with $1.10 wins.
As you will see from the sample runs, there is NO user input for this game. Again, the game automatically ends when one or both players have a balance of $1.00 or more.
The output of your program should follow the same format shown in the sample runs below. In particular, at the beginning of the program, the starting balance for both the player and the computer should be displayed. Then after the three coins have been tossed for both the player and the computer, the balance for both the player and the computer should be displayed. At the end of the program, the ending balance for both the player and the computer should be displayed. Finally, the outcome of the game (player won, computer won, or tie) should be displayed.
NOTE: The first thing that you should do in your application program (200_assign3.cpp) is create three different Coin objects: one for the quarter, one for the dime, and one for the nickel. Remember that both the player and the computer toss the same coins, so only a total of three Coin objects are needed.
Sample Run 1:
Sample Run 2:
Sample Run 3:
Sample Run 4:
Sample Run 5:
Your starting balance: $e.e0 The computer's starting balance: $e.e0 Your balance after round 1: $0.40 The computer's balance after round 1: $0.15 Your balance after round 2: $0.40 The computer's balance after round 2: $0.20 Your balance after round 3: $0.45 The computer's balance after round 3: $e.35 Your balance after round 4: $0.55 The computer's balance after round 4: $0.7e Your balance after round 5: $e.70 The computer's balance after round 5: $0.70 Your balance after round 6: $0.85 The computer's balance after round 6: $e.80 Your balance after round 7: $1.1e The computer's balance after round 7: $1.10 Your ending balance: $1.10 The computer's ending balance: $1.10 Tie! Nobody wins. Press any key to continue .. .Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Coin.h
#ifndef coin_h
#define coin_h
#include<stdlib.h>
#include<time.h>
#include<iostream>
using namespace std;
//Coin class
class Coin{
//required variable to store coin's side
string sideUp;
public:
//public method prototypes
Coin();
void toss();
string getSideUp();
};
#endif
//Coin.cpp
#include "coin.h"
//implementation of all methods
Coin::Coin(){
//tossing the coin
toss();
}
//this method will toss the coin, update sideUp variable
void Coin::toss(){
//generating a random number (0 or 1)
int i=rand()%2;
//if i is 0, setting sideUp as heads, or else tails
if(i==0){
sideUp="heads";
}else{
sideUp="tails";
}
}
string Coin::getSideUp(){
return sideUp;
}
//200_assign3.cpp
#include "coin.h"
#include<iomanip>
int main(){
//seeding random number generator
srand(time(NULL));
//initializing variables
double playerBalance=0,computerBalance=0;
Coin quarter, dime, nickel; //denoting each type of coins
int round=1;
bool gameOver=false; //loop controller
//setting decimal point precision to 2
cout<<setprecision(2)<<fixed;
//displaying starting balance
cout<<"Your starting balance : $"<<playerBalance<<endl;
cout<<"The Computer's starting balance : $"<<computerBalance<<endl;
//looping until game is over
while(!gameOver){
//tossing three coins
quarter.toss();
dime.toss();
nickel.toss();
//updating player's balance if a coin draw heads
if(quarter.getSideUp()=="heads"){
playerBalance+=0.25;
}
if(dime.getSideUp()=="heads"){
playerBalance+=0.10;
}
if(nickel.getSideUp()=="heads"){
playerBalance+=0.05;
}
//tossing three coins again
quarter.toss();
dime.toss();
nickel.toss();
//updating computer's balance if a coin draw heads
if(quarter.getSideUp()=="heads"){
computerBalance+=0.25;
}
if(dime.getSideUp()=="heads"){
computerBalance+=0.10;
}
if(nickel.getSideUp()=="heads"){
computerBalance+=0.05;
}
//displaying balance after this round
cout<<"Your balance after round "<<round<<": $"<<playerBalance<<endl;
cout<<"The Computer's balance after round "<<round<<": $"<<computerBalance<<endl;
//checking if any of them has won
if(playerBalance>=1 || computerBalance>=1){
//game is over
gameOver=true;
}
round++;
}
//displaying ending balance
cout<<"Your ending balance : $"<<playerBalance<<endl;
cout<<"The Computer's ending balance : $"<<computerBalance<<endl;
//identifying the winner
if(playerBalance==computerBalance){
//tie
cout<<"Tie! Nobody wins"<<endl;
}else if(playerBalance>computerBalance){
//player
cout<<"Congratulations! You won"<<endl;
}else{
//computer
cout<<"Sorry! The computer won"<<endl;
}
}
//OUTPUT (multiple runs)
Your starting balance : $0.00
The Computer's starting balance : $0.00
Your balance after round 1: $0.30
The Computer's balance after round 1: $0.00
Your balance after round 2: $0.35
The Computer's balance after round 2: $0.35
Your balance after round 3: $0.75
The Computer's balance after round 3: $0.45
Your balance after round 4: $0.80
The Computer's balance after round 4: $0.80
Your balance after round 5: $1.20
The Computer's balance after round 5: $1.20
Your ending balance : $1.20
The Computer's ending balance : $1.20
Congratulations! You won
Your starting balance : $0.00
The Computer's starting balance : $0.00
Your balance after round 1: $0.00
The Computer's balance after round 1: $0.10
Your balance after round 2: $0.15
The Computer's balance after round 2: $0.10
Your balance after round 3: $0.25
The Computer's balance after round 3: $0.40
Your balance after round 4: $0.55
The Computer's balance after round 4: $0.80
Your balance after round 5: $0.90
The Computer's balance after round 5: $1.20
Your ending balance : $0.90
The Computer's ending balance : $1.20
Sorry! The computer won
Your starting balance : $0.00
The Computer's starting balance : $0.00
Your balance after round 1: $0.40
The Computer's balance after round 1: $0.40
Your balance after round 2: $0.55
The Computer's balance after round 2: $0.40
Your balance after round 3: $0.70
The Computer's balance after round 3: $0.70
Your balance after round 4: $0.85
The Computer's balance after round 4: $0.95
Your balance after round 5: $1.00
The Computer's balance after round 5: $1.00
Your ending balance : $1.00
The Computer's ending balance : $1.00
Tie! Nobody wins