Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment, implement and use the methods for a class called Coin. Coin

ID: 3709702 • Letter: F

Question

For this assignment, implement and use the methods for a class called Coin.

Coin class

The class will represent a two-sided coin with a specified monetary value.

Data Members

The data members for the class are:

a double that holds the monetary value of the coin

a character array with the capacity for 6 characters. It holds the side of the coin that is facing up( "heads" or "tails")

Constructors

This class has two constructors. The default constructor (the one that takes no arguments) should initialize the value of the coin to a penny (0.01) and the character array that holds the side of the coin should be initialized by calling the toss() method that is described below.

The other constructor takes 1 argument: a double that holds the initial value for the coin. The passed in argument should be used to initialize the value of the coin. No error checking is required. The character array that holds the side of the coin should be initialized by calling the toss() method that is described below.

Methods

void toss()

This is a public method that simulates the tossing of the coin. It takes no arguments and returns nothing.

The coin will be tossed by using the rand() function to generate a random value of either 1 or 2. A value of 1 indicates that the result of the coin toss was heads and that "heads" should be copied into the character array that holds the side of the coin. A value of 2 indicates that the result of the coin toss was tails and that "tails should be copied into the character array data member.

int getSide()

This is a public accessor method that returns the current side of the coin that is facing up. It takes no arguments and returns an integer.

If "heads" is the side of the coin that is up, return 1. If "tails" is the side of the coin that is up, return 2.

Note: The integer value is being returned because we don't know how to return a character array at this point in the class (and it won't be taught until the CSCI 241 class).

double getValue()

This is a public accessor method that returns the monetary value of the coin. It takes no arguments and returns a double.

Part 1: Testing the Coin class

Before using the Coin class as part of a larger project, it should be tested to make sure that all of the constructors and methods work. A short program has been written that will test each one of the methods individually.

The test program can be downloaded from http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240test9.cpp

The output that is produced by the test program:

If the output, using your Coin class, matches the above output, move on to part 2 of this assignment. Otherwise, fix the constructor/methods until the output does match.

Part 2: Using the Coin class

This is the part of the assignment that will be submitted for grading.

For this part of the program, create a game that uses the Coin class.

The concept of the game is fairly simple. A player will have three coins: a nickel, dime, and quarter and a starting balance of $0.00. During each round of the game, each one of the coins will be tossed. When a coin is tossed, if it lands heads-up, it's value will be added to the balance. If it lands tails-up, nothing will be added to the balance. The game will continue until the balance reaches $1.00 or more. If the balance is exactly $1.00, the player won the game. If the balance exceeds $1.00, the player lost the game.

Implementing the game

In main(), create 3 Coin objects: one that represents a nickel, one that represents a dime, and one that represents a quarter.

Set the seed value that is used by the random number generator by using the srand() function. Use a value of 11.

Write a loop that will execute as long as the current balance is less than $1.00. Inside of the loop:

Display the current balance with a leading dollar sign.

Toss the nickel. Display whether the nickel landed with heads up or tails up. If the nickel landed with heads up, add the value of the nickel to the balance.

Toss the dime. Display whether the dime landed with heads up or tails up. If the dime landed with heads up, add the value of the dime to the balance.

Toss the quarter. Display whether the quarter landed with heads up or tails up. If the quarter landed with heads up, add the value of the quarter to the balance.

Once the balance reaches $1.00 or more, display the final balance to the player and tell them whether they won or lost the game.

Programming Requirements

Make sure to add #include statements for the cstdlib and cstring libraries.

Each method must have a documentation box like a function.

Hand in a copy of the source code from part 2 of the assignment using Blackboard.

Output

Explanation / Answer

Hi Dear,

Please find my implementation.


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

Code for Part 1:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
//Put the Coin class definition after this line
class Coin
{
private:
double value;
char side[6];
public:
Coin();
Coin(double val);
void toss();
int getSide();
double getValue();
};

int main()
{
//Set the random number generator and the formatting for the output
srand( 1 );
cout << fixed << setprecision(2);
//Create objects using the two constructors
Coin coin1;
Coin coin2( 0.25 );
Coin coin3( 0.10 );
//Test 1: the getSide method
cout << "Test 1: use the getSide method on all of the objects" << endl << endl
<< "coin1 has " << ( (coin1.getSide() == 1)? "heads" : "tails" ) << " up" << endl
<< "coin2 has " << ( (coin2.getSide() == 1)? "heads" : "tails" ) << " up" << endl
<< "coin3 has " << ( (coin3.getSide() == 1)? "heads" : "tails" ) << " up" << endl;
//Test 2: the getValue method
cout << endl << endl
<< "Test 2: use the getValue method on all of the objects" << endl << endl
<< "coin1 has the value $" << coin1.getValue() << endl
<< "coin2 has the value $" << coin2.getValue() << endl
<< "coin3 has the value $" << coin3.getValue() << endl;
//Test 3: the toss method
cout << endl << endl
<< "Test 3: use the toss method 5 times on all of the objects" << endl << endl;
cout << "coin1 coin2 coin3" << endl
<< "-----------------------------" << endl;
for( int cnt = 1; cnt <= 5; cnt++ )
{
coin1.toss();
cout << ( (coin1.getSide() == 1)? "heads" : "tails" );
coin2.toss();
cout << ( (coin2.getSide() == 1)? " heads" : " tails" );
coin3.toss();
cout << ( (coin3.getSide() == 1)? " heads" : " tails" ) << endl;
}
return 0;
}
//Code the constructors and methods for the Coin class after this line
Coin::Coin()
{
value = 0.01;
toss();
}
Coin::Coin(double val)
{
value = val;
toss();
}
void Coin::toss()
{
int v = 1 + rand() % 2;
if(v == 1)
strcpy(side, "heads");
else
strcpy(side, "tails");
}
int Coin::getSide()
{
if(strcmp(side, "heads") == 0)
return 1;
else
return 2;
}
double Coin::getValue()
{
return value;
}


Code for Part 2:
================

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
//Put the Coin class definition after this line
class Coin
{
private:
double value;
char side[6];
public:
Coin();
Coin(double val);
void toss();
int getSide();
double getValue();
};

int main()
{
//Set the random number generator and the formatting for the output
srand( 11 );
cout << fixed << setprecision(2);
Coin nickle(0.05), dime(0.10), quarter(0.25);
double balance = 0;
while(balance < 1.0)
{
cout << "Your current balance: $" << balance << endl << endl;
nickle.toss();
cout << "Nickel: " << (nickle.getSide() == 1 ? "heads" : "tails") << endl;
if(nickle.getSide() == 1)
balance += nickle.getValue();
dime.toss();
cout << "Dime: " << (dime.getSide() == 1 ? "heads" : "tails") << endl;
if(dime.getSide() == 1)
balance += dime.getValue();
quarter.toss();
cout << "Quarter: " << (quarter.getSide() == 1 ? "heads" : "tails") << endl;
if(quarter.getSide() == 1)
balance += quarter.getValue();
cout << "-----------------------------" << endl << endl;
}
cout << "Your final balance is $" << balance ;
if(balance == 1.00)
cout << ". You won" << endl;
else
cout << ". You lost" << endl;
}
//Code the constructors and methods for the Coin class after this line
Coin::Coin()
{
value = 0.01;
toss();
}
Coin::Coin(double val)
{
value = val;
toss();
}
void Coin::toss()
{
int v = 1 + rand() % 2;
if(v == 1)
strcpy(side, "heads");
else
strcpy(side, "tails");
}
int Coin::getSide()
{
if(strcmp(side, "heads") == 0)
return 1;
else
return 2;
}
double Coin::getValue()
{
return value;
}

output

=====

Your current balance: $0.00

Nickel: tails
Dime: tails
Quarter: heads
-----------------------------

Your current balance: $0.25

Nickel: heads
Dime: tails
Quarter: heads
-----------------------------

Your current balance: $0.55

Nickel: tails
Dime: heads
Quarter: heads
-----------------------------

Your current balance: $0.90

Nickel: tails
Dime: heads
Quarter: tails
-----------------------------

Your final balance is $1.00. You won

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote