Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

Test 1: use the getSide method on all of the objects coin1 has tails up coin2 has tails up coin3 has heads up Test 2: use the getValue method on all of the objects coin1 has the value $0.01 coin2 has the value $0.25 coin3 has the value $0.10 Test 3: use the toss method 5 times on all of the objects coin1 coin2 coin3 ----------------------------- heads tails heads heads heads heads heads tails tails tails tails tails tails tails heads 

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

Your current balance is $0.00 Nickel: tails Dime: tails Quarter: tails ----------------------------- Your current balance is $0.00 Nickel: heads Dime: tails Quarter: heads ----------------------------- Your current balance is $0.30 Nickel: tails Dime: heads Quarter: tails ----------------------------- Your current balance is $0.40 Nickel: heads Dime: heads Quarter: tails ----------------------------- Your current balance is $0.55 Nickel: heads Dime: tails Quarter: heads ----------------------------- Your current balance is $0.85 Nickel: heads Dime: tails Quarter: heads ----------------------------- Your final balance is $1.15. You lost

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions