Question

implement a class called PiggyBank that will be used to represent a collection of coins. Functionality...

implement a class called PiggyBank that will be used to represent a collection of coins. Functionality will be added to the class so that coins can be added to the bank and output operations can be performed. A driver program is provided to test the methods.

class PiggyBank

The PiggyBank class definition and symbolic constants should be placed at the top of the driver program source code file while the method implementation should be done after the closing curly brace for main().

class PiggyBank
{
public:
PiggyBank();
PiggyBank( int, int, int, int );

void printPiggyBank();
void printPiggyBankValue();

void emptyTheBank();

void addCoins( int, int, int, int );
void addPennies( int );
void addNickels( int );
void addDimes( int );
void addQuarters( int );


private:
int numPennies, numNickels, numDimes, numQuarters;

static const double PENNY, NICKEL, DIME, QUARTER;

double calcPiggyBankValue();
};

Data Members

The PiggyBank class should contain eight private data members. They are:

  • numPennies is an integer to hold the number of pennies in the piggy bank
  • numNickels is an integer to hold the number of nickels in the piggy bank
  • numDimes is an integer to hold the number of dimes in the piggy bank
  • numQuarters is an integer to hold the number of quarters in the piggy bank
  • PENNY is a symbolic constant of type double that represents the value of a single penny ($0.01)
  • NICKEL is a symbolic constant of type double that represents the value of a single nickel ($0.05)
  • DIME is a symbolic constant of type double that represents the value of a single dime ($0.10)
  • QUARTER is a symbolic constant of type double that represents the value of a single quarter ($0.25)

C++ 11 allows for symbolic constants to be initialized in the class definition. However, it's not something that is supported by all compilers because class definitions are treated as patterns for what the class should contain, and therefore, they don't take up any memory. To create a symbolic constant that will work for all compilers:

static const int PENNY;

const double PiggyBank::PENNY = 0.01;

Note the dropping of the keyword "static" and the inclusion of the "PiggyBank::" in front of the constant name. This lets the compiler know that the constant belongs to the PiggyBank class.

  1. In the class definition, define the symbolic constant with the keyword "static" before the definition. So:

  2. Before main, initialize the symbolic constant with its value. So:

Now, PENNY can be used in the class methods when needed.

Constructors

Constructor 1: PiggyBank()

The first constructor for the PiggyBank class (the default constructor) takes no arguments. It simply initializes the 4 integer data members to 0.

Constructor 2: PiggyBank( int initialPennies, int initialNickels, int initialDimes, int initialQuarters )

The second constructor for the PiggyBank class should take four integer arguments: the number of pennies, nickels, dimes, and quarters respectively.

For each argument, if the passed in value is not negative, it should be used to initialize the appropriate data member. If the passed in value is negative, initialize the corresponding data member to 0 and print an informative error message.

Methods

void printPiggyBank()

This method prints the contents of a PiggyBank object. It takes no arguments and returns nothing. It should print the contents of the object on a single line:

Pennies:   7    Nickels:   9    Dimes:     3    Quarters:  4

void printPiggyBankValue()

This method prints the value of a PiggyBank object. It takes no arguments and returns nothing. It should print the contents of the object in the format:

$1.82

void emptyTheBank()

This method sets a PiggyBank object so that it no longer contains any money. It takes no arguments and returns nothing. It should simply initialize each of the integer data members to 0.

void addCoins( int morePennies, int moreNickels, int moreDimes, int moreQuarters )

This method will add coins to a PiggyBank object. It takes four integer arguments and returns nothing. The arguments passed to this method are: the number of pennies, nickels, dimes, and quarters, respectively, to add to the PiggyBank object.

Before adding a value to a data member, verify that that the value is not negative. If it is not negative, add the value to the appropriate data member. If the value is negative, print an informative error message. Think about calling the add methods described below.

void addPennies( int morePennies )

This method will add pennies to a PiggyBank object. It takes one integer argument and returns nothing. The argument passed to this method is the number of pennies to add to the PiggyBank object.

Before adding a value to the pennies data member, verify that that the passed in value is not negative. If it is not negative, add the value to the pennies data member. If the value is negative, print an informative error message and do not change the pennies data member.

void addNickels( int moreNickels )

This method will add nickels to a PiggyBank object. It takes one integer argument and returns nothing. The argument passed to this method is the number of nickels to add to the PiggyBank object.

Before adding a value to the nickels data member, verify that that the passed in value is not negative. If it is not negative, add the value to the nickels data member. If the value is negative, print an informative error message and do not change the nickels data member.

void addDimes( int moreDimes )

This method will add dimes to a PiggyBank object. It takes one integer argument and returns nothing. The argument passed to this method is the number of dimes to add to the PiggyBank object.

Before adding a value to the dimes data member, verify that that the passed in value is nonnegative. If it is nonnegative, add the value to the dimes data member. If the value is negative, print an informative error message and do not change the dimes data member.

void addQuarters( int moreQuarters )

This method will add quarters to a PiggyBank object. It takes one integer argument and returns nothing. The argument passed to this method is the number of quarters to add to the PiggyBank object.

Before adding a value to the quarters data member, verify that that the passed in value is not negative. If it is not negative, add the value to the quarters data member. If the value is negative, print an informative error message and do not change the quarter data member.

double calcPiggyBankValue()

This method calculates and returns the value (in dollars and cents) of a PiggyBank object. It takes no arguments and returns a double.

For example, if the current PiggyBank object contains 3 pennies, 4 nickels, 5 dimes, and 6 quarters, then this method should return the value 2.23.

This method will be used to help in the coding of the printPiggyBankValue method. It will never be called by anything other than members of the PiggyBank class, therefore it is implemented as a private method.

driver program:

#include <iostream>
#include <iomanip>

using namespace std;

//*************** Place the class description after this line ***************



//*************** Initialize the symbolic constants after this line **********




//****************************************************************************

int main()
{
//Test 1 -- default constructor and printPiggyBank

cout << "***** Test 1: Default Constructor and printPiggyBank *****" << endl << endl
     << "Default constructor produces bank1: " << endl << endl;

PiggyBank bank1;

bank1.printPiggyBank();



//Test 2 -- constructor with arguments

cout << endl << endl << endl << "***** Test 2: Constructor with Arguments *****" << endl << endl
     << "Constructor with 6, 8, 10, 12 produces bank2: " << endl << endl;

PiggyBank bank2( 6, 8, 10, 12 );

bank2.printPiggyBank();

cout << endl << endl << "Constructor with 4, 1, 0, 7 produces bank3:"
     << endl << endl;

PiggyBank bank3  = PiggyBank( 4, 1, 0, 7 );

bank3.printPiggyBank();

cout << endl << endl << "Constructor with 23, -8, -2, 29 produces bank4:"
     << endl << endl;

PiggyBank bank4  = PiggyBank( 23, -8, -2, 29 );

bank4.printPiggyBank();


//Test 3 -- printPiggyBankValue

cout << endl << endl << endl << "***** Test 3: printPiggyBankValue *****" << endl << endl
     << "bank1:" << endl;

bank1.printPiggyBank();

cout << endl << "Total: ";

bank1.printPiggyBankValue();

cout << endl << endl << endl << "bank2:" << endl;

bank2.printPiggyBank();

cout << endl << "Total: ";

bank2.printPiggyBankValue();

cout << endl << endl << endl << "bank3:" << endl;

bank3.printPiggyBank();

cout << endl << "Total: ";

bank3.printPiggyBankValue();

cout << endl << endl << endl << "bank4:" << endl;

bank4.printPiggyBank();

cout << endl << "Total: ";

bank4.printPiggyBankValue();


//Test 4 -- adding coins

cout << endl << endl << endl << "***** Test 4: add Methods *****" << endl << endl
     << "Adding 2 pennies, 47 nickels, 20 dimes, and 5 quarters to bank2 produces:"
     << endl << endl;

bank2.addCoins( 2, 47, 20, 5 );

bank2.printPiggyBank();

cout << endl << "Total: ";

bank2.printPiggyBankValue();


cout << endl << endl << endl << "Adding 95 pennies to bank3:" << endl << endl;

bank3.addPennies( 95 );

bank3.printPiggyBank();

cout << endl << "Total: ";

bank3.printPiggyBankValue();


cout << endl << endl << endl << "Adding -2 nickels to bank3:" << endl << endl;

bank3.addNickels( -2 );

bank3.printPiggyBank();

cout << endl << "Total: ";

bank3.printPiggyBankValue();


cout << endl << endl << endl << "Adding 41 dimes to bank4:" << endl << endl;

bank4.addDimes( 41 );

bank4.printPiggyBank();

cout << endl << "Total: ";

bank4.printPiggyBankValue();


cout << endl << endl << endl << "Adding 14 quarters to bank2: " << endl << endl;

bank2.addQuarters( 14 );

bank2.printPiggyBank();

cout << endl << "Total: ";

bank2.printPiggyBankValue();



//Test 5 -- empty the bank

cout << endl << endl << endl << "***** Test 5: Emptying a PiggyBank *****" << endl << endl
     << "It's time to empty the bank." << endl << endl;

cout << endl << "bank3 initially contains: " << endl << endl;

bank3.printPiggyBank();

cout << endl << "Total: ";

bank3.printPiggyBankValue();

bank3.emptyTheBank();

cout << endl << endl << endl << "bank3 now contains: " << endl << endl;

bank3.printPiggyBank();

cout << endl << "Total: ";

bank3.printPiggyBankValue();

cout << endl << endl;

return 0;
}


//*************** Implement the class methods after this line ***************
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

class PiggyBank
{
public:
PiggyBank();
PiggyBank( int, int, int, int );

void printPiggyBank();
void printPiggyBankValue();

void emptyTheBank();

void addCoins( int, int, int, int );
void addPennies( int );
void addNickels( int );
void addDimes( int );
void addQuarters( int );


private:
int numPennies, numNickels, numDimes, numQuarters;

static const double PENNY, NICKEL, DIME, QUARTER;

double calcPiggyBankValue();
};

const double PiggyBank::PENNY=0.01;
const double PiggyBank::NICKEL=0.05;
const double PiggyBank::DIME=0.10;
const double PiggyBank::QUARTER=0.25;


PiggyBank::PiggyBank()
{
       this->numPennies=0;
   this->numNickels=0;
   this->numDimes=0;
   this->numQuarters=0;
}
PiggyBank::PiggyBank( int P, int N, int D, int Q)
{
   if(P>0)
   this->numPennies=P;
   else
   {
       this->numPennies=0;
   }
   if(N>0)
   this->numNickels=N;
   else
   this->numNickels=0;  
  
   if(D>0)
   this->numDimes=D;
   else
       this->numDimes=0;
   if(Q>0)
   this->numQuarters=Q;
   else
       this->numDimes=0;
}

void PiggyBank::printPiggyBank()
{
   cout<<"Pennies :"<<numPennies<<endl;
   cout<<"Nickels :"<<numNickels<<endl;
   cout<<"Dimes :"<<numDimes<<endl;
   cout<<"Quarters :"<<numQuarters<<endl;
}
void PiggyBank::printPiggyBankValue()
{
   double tot=0;
   tot=PENNY*numPennies+NICKEL*numNickels+DIME*numDimes+QUARTER*numQuarters;
   cout<<"Total :$"<<tot<<endl;
}

void PiggyBank::emptyTheBank()
{
       this->numPennies=0;
   this->numNickels=0;
   this->numDimes=0;
   this->numQuarters=0;
}

void PiggyBank::addCoins( int P, int N, int D, int Q)
{
   if(P>0)
       this->numPennies+=P;
      
   if(N>0)
   this->numNickels+=N;
   if(D>0)
   this->numDimes+=D;
   if(Q>0)
   this->numQuarters+=Q;
}
void PiggyBank::addPennies( int P)
{
   if(P>0)
   this->numPennies+=P;
   else
   this->numPennies+=0;
}
void PiggyBank::addNickels( int N)
{
   if(N>0)
   this->numNickels+=N;  
   else
   this->numNickels+=0;  
}
void PiggyBank::addDimes( int D)
{
   if(D>0)
   this->numDimes+=D;
   else
       this->numDimes+=0;
}
void PiggyBank::addQuarters( int Q)
{
   if(Q>0)
   this->numQuarters+=Q;
   else
       this->numQuarters+=0;
}

int main()
{
//Test 1 -- default constructor and printPiggyBank

cout << "***** Test 1: Default Constructor and printPiggyBank *****" << endl << endl
<< "Default constructor produces bank1: " << endl << endl;

PiggyBank bank1;

bank1.printPiggyBank();

//Test 2 -- constructor with arguments

cout << endl << endl << endl << "***** Test 2: Constructor with Arguments *****" << endl << endl
<< "Constructor with 6, 8, 10, 12 produces bank2: " << endl << endl;

PiggyBank bank2( 6, 8, 10, 12 );

bank2.printPiggyBank();

cout << endl << endl << "Constructor with 4, 1, 0, 7 produces bank3:"
<< endl << endl;

PiggyBank bank3 = PiggyBank( 4, 1, 0, 7 );

bank3.printPiggyBank();

cout << endl << endl << "Constructor with 23, -8, -2, 29 produces bank4:"
<< endl << endl;

PiggyBank bank4 = PiggyBank( 23, -8, -2, 29 );

bank4.printPiggyBank();


//Test 3 -- printPiggyBankValue

cout << endl << endl << endl << "***** Test 3: printPiggyBankValue *****" << endl << endl
<< "bank1:" << endl;

bank1.printPiggyBank();

cout << endl << "Total: ";

bank1.printPiggyBankValue();

cout << endl << endl << endl << "bank2:" << endl;

bank2.printPiggyBank();

cout << endl << "Total: ";

bank2.printPiggyBankValue();

cout << endl << endl << endl << "bank3:" << endl;

bank3.printPiggyBank();

cout << endl << "Total: ";

bank3.printPiggyBankValue();

cout << endl << endl << endl << "bank4:" << endl;

bank4.printPiggyBank();

cout << endl << "Total: ";

bank4.printPiggyBankValue();


//Test 4 -- adding coins

cout << endl << endl << endl << "***** Test 4: add Methods *****" << endl << endl
<< "Adding 2 pennies, 47 nickels, 20 dimes, and 5 quarters to bank2 produces:"
<< endl << endl;

bank2.addCoins( 2, 47, 20, 5 );

bank2.printPiggyBank();

cout << endl << "Total: ";

bank2.printPiggyBankValue();


cout << endl << endl << endl << "Adding 95 pennies to bank3:" << endl << endl;

bank3.addPennies( 95 );

bank3.printPiggyBank();

cout << endl << "Total: ";

bank3.printPiggyBankValue();


cout << endl << endl << endl << "Adding -2 nickels to bank3:" << endl << endl;

bank3.addNickels( -2 );

bank3.printPiggyBank();

cout << endl << "Total: ";

bank3.printPiggyBankValue();


cout << endl << endl << endl << "Adding 41 dimes to bank4:" << endl << endl;

bank4.addDimes( 41 );

bank4.printPiggyBank();

cout << endl << "Total: ";

bank4.printPiggyBankValue();


cout << endl << endl << endl << "Adding 14 quarters to bank2: " << endl << endl;

bank2.addQuarters( 14 );

bank2.printPiggyBank();

cout << endl << "Total: ";

bank2.printPiggyBankValue();

//Test 5 -- empty the bank

cout << endl << endl << endl << "***** Test 5: Emptying a PiggyBank *****" << endl << endl
<< "It's time to empty the bank." << endl << endl;

cout << endl << "bank3 initially contains: " << endl << endl;

bank3.printPiggyBank();

cout << endl << "Total: ";

bank3.printPiggyBankValue();

bank3.emptyTheBank();

cout << endl << endl << endl << "bank3 now contains: " << endl << endl;

bank3.printPiggyBank();

cout << endl << "Total: ";

bank3.printPiggyBankValue();

cout << endl << endl;

return 0;
}

__________________________

Output:

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
implement a class called PiggyBank that will be used to represent a collection of coins. Functionality...
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
  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • Write a class encapsulation the concept of coins (Coins java), assuming that coins have the following...

    Write a class encapsulation the concept of coins (Coins java), assuming that coins have the following attributes: a number of quarters, a number of dims, a number of nickels, and a number of pennies. Include a constructor (accept 4 numbers that represent the number of coins for each coin type), mutator and accessot methods, and method tostring. The tostring method should return a string in the following format: Total Value: $5.50 10 quarters, 20 dimes, 19 nickels, and 5 pennies...

  • C++ Please Provide .cpp Overview For this assignment, implement and use the methods for a class...

    C++ Please Provide .cpp Overview For this assignment, implement and use the methods for a class called Player that represents a player in the National Hockey League. Player class Use the following class definition: class Player { public: Player(); Player( const char [], int, int, int ); void printPlayer(); void setName( const char [] ); void setNumber( int ); void changeGoals( int ); void changeAssists( int ); int getNumber(); int getGoals(); int getAssists(); private: char name[50]; int number; int goals;...

  • Step One This is the Measurable interface we saw in class; you will need to provide...

    Step One This is the Measurable interface we saw in class; you will need to provide this class; this is it in its entirety (this is literally all you have to do for this step): public interface Measurable double getMeasure(); Step Two This is the Comparable interface that is a part of the standard Java library: public interface Comparable int compareTo (Object otherObject); Finish this class to represent a PiggyBank. A PiggyBank holds quarters, dimes, nickels, and pennies. You will...

  • C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object...

    C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object from another of the same type. II. Copy an object to pass it as argument to a function. III. Copy an object to return it from a function. Read the following program and answer questions (20 points) #include <iostream> using namespace std; class Line public: int getLength( void); Line( int len // simple constructor Line( const Line &obj) 1/ copy constructor Line (); //...

  • Write a program called "ConvertPennies" to input an integer value representing a number of pennies and...

    Write a program called "ConvertPennies" to input an integer value representing a number of pennies and convert it to its equivalent number of Dollars, Quarters, Dimes, Nickels and Pennies. Following is the result of the execution of this program for 292 pennies. Enter the amount of pennies to convert: 292 pennies is equal to 2 one dollar bills 3 quarters 1 dimes 1 nickels 2 pennies Have a nice day! This is given: //********************************************************************** // Programmer: Your first and last...

  • #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured...

    #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured in feet and inches. */ class FeetInches { private: int feet; // The number of feet int inches; // The number of inches /** The simplify method adjusts the values in feet and inches to conform to a standard measurement. */ void simplify() { if (inches > 11) { feet = feet + (inches / 12); inches = inches % 12; } } /**...

  • C++ implement and use the methods for a class called Seller that represents information about a...

    C++ implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; }; Data Members The...

  • LW: Class Constructors Objectives Write a default constructor for a class. Note that this const...

    LW: Class Constructors Objectives Write a default constructor for a class. Note that this constructor is used to build the object anytime an argument is not provided when it is defined. Write a parameterized constructor that takes arguments that are used to initialize the class’s member variables Labwork Download the code associated with this lab: ColorConstructor.zip Main.cpp Color.h Color.cpp Compile and run the code. The first line of output of the program should display nonsensical integers for the values of...

  • CIT-111 Homework #5, Part A                                    Chapter 5, part 1

    CIT-111 Homework #5, Part A                                    Chapter 5, part 1 and alternate part 2 (page 341) A breakdown of the problem is as follows: Class name:                        Money Instance variables:           private, integer:                dollars, cents Constructor methods:    Money () – sets dollars and cents both to zero                                                 Money (int bucks) – sets dollars to bucks, cents to zero                                                 Money (int buck, int pennies) – sets dollars to bucks, cents to pennies Accessor methods:          getDollar () – returns value of dollars for...

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
ADVERTISEMENT