| CSCI 240 | Spring 2026 |
For this assignment, 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, output operations can be performed, and comparisons between banks can be made.
The cpp file that is submitted for grading must be named assign9.cpp.
The PiggyBank class has four private data members. They are:
an integer that holds the number of pennies in the piggy bank
an integer that holds the number of nickels in the piggy bank
an integer that holds the number of dimes in the piggy bank
an integer that holds the number of quarters in the piggy bank
The constructor for the PiggyBank class is a default constructor, meaning that it takes no arguments. It should simply initialize the 4 integer data members to 0.
The alternate constructor for the PiggyBank class is used to create an object with a specific number of coins.
It takes 4 arguments: an integer that holds the initial number of pennies, an integer that holds the initial number of nickels, an integer that holds the initial number of dimes, and an integer that holds the initial number of quarters.
This version of the constructor should call the emptyTheBank and addCoins methods that are described below to initialize the object.
This constructor is the copy constructor for the PiggyBank class. It is used to create an object that is a copy/duplicate of an existing PiggyBank object.
It takes 1 argument: a reference to a constant PiggyBank object that holds the values to copy into the object being created.
This version of the constructor should copy the data members from the passed in PiggyBank object (otherBank in the header above) into the corresponding data members of the object being created.
The following methods are required for the PiggyBank class.
This method displays the contents of a PiggyBank object. It takes no arguments and returns nothing.
It displays the contents of the object on a single line. Each numeric value is displayed in a field 7 characters. End the display with a single newline character.
Pennies 7 Nickels 9 Dimes 3 Quarters 4
This method prints the contents and value of a PiggyBank object.
It takes 1 argument: a string that holds a label/title. It returns nothing.
This method should display the passed in string argument (and a single newline character) and then call the print and printValue methods to display the object. Finish the method by displaying two newline characters.
For example, if method is passed the string value "bank1 object contains", this method should display:
bank1 object contains Pennies 7 Nickels 9 Dimes 3 Quarters 4 $1.82
assuming the object that called the method contains 7 pennies, 9 nickels, 3 dimes, and 4 quarters.
This method prints the monetary value of a PiggyBank object. It takes no arguments and returns nothing.
It displays the monetary value of the object with two digits after the decimal point and a leading dollar sign. Use the calcValue method.
Do not display any newline characters.
This method sets a PiggyBank object so that it no longer contains any money. It takes no arguments and returns nothing.
It sets each of the integer data members to 0. This method should be used by the constructor.
This method adds coins to a PiggyBank object.
It takes four integer arguments: an integer that holds the number of pennies to add to the PiggyBank object, an integer that holds the number of nickels to add to the PiggyBank object, an integer that holds the number of dimes to add to the PiggyBank object, and an integer that holds the number of quarters to add to the PiggyBank object. It returns nothing.
Before adding a value to a data member, verify that the value is not negative. If it is not negative, add the value to the appropriate data member. If the value is negative, display an error message and do not make any changes to the data member.
Error message format:
*** Error: cannot add a negative number of [coin] ***
where [coin] is replaced by the type of coin. The message should end with two newline characters.
This method adds pennies to a PiggyBank object.
It takes one integer argument: an integer that holds the number of pennies to add to the PiggyBank object. It returns nothing.
Before adding a value to the data member that holds the number of pennies, verify that the passed in value is not negative. If it is not negative, add the value to the data member. If the value is negative, display the error message "*** Error: cannot add a negative number of pennies ***" and do not change the data member. The error message should end with two newline characters.
These methods are just like the addPennies method, but they work with the other data members that hold the number of nickels, dimes, and quarters, respectively.
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, this method should return the value 2.23.
This method should be used to help in the coding of the printValue method.
This method combines the contents of two piggybanks.
It takes 4 arguments: an integer that holds the number of pennies in the piggybank to combine with the current instance, an integer that holds the number of nickels in the piggybank to combine with the current instance, an integer that holds the number of dimes in the piggybank to combine with the current instance, and an integer that holds the number of quarters in the piggybank to combine with the current instance.
It returns a PiggyBank object: an object that holds the combined contents of the two piggybanks.
The two piggybanks that are being added are the "current instance", which is the PiggyBank object that calls the addBanks method. This means that the current instance can be accessed by using the data members that hold the number of pennies, nickels, dimes, and quarters.
The other piggybank is represented by the four passed-in arguments.
The method should create a PiggyBank object to hold the combined piggybanks. The four data members of this object should be set by adding corresponding values from the current instance and passed in arguments. Once the banks have been combined, the object should be returned.
Make sure the current instance is NOT CHANGED when the banks are combined.
This method does the same thing as the other addBanks method. The difference is that the second piggy bank is passed in as a PiggyBank object rather than four separate integer values.
It is strongly recommended to only formally implement one of the two addBanks methods and to then call that method in the other version of addBanks.
This method compares the contents of two piggybanks to determine if they are equal.
It takes 4 arguments: an integer that holds the number of pennies in the piggybank to compare with the current instance, an integer that holds the number of nickels in the piggybank to compare with the current instance, an integer that holds the number of dimes in the piggybank to compare with the current instance, and an integer that holds the number of quarters in the piggybank to compare with the current instance.
It returns a bool: true if the piggybanks are equal, false if the piggybanks are not equal.
Two piggybanks are considered to be equal if the monetary values of the piggybanks are equal. Use the calcValue method to find the monetary value of a piggybank.
This method does the same thing as the other isEqual method. The difference is that the second piggy bank is passed in as a PiggyBank object rather than four separate integer values.
It is strongly recommended to only formally implement one of the two isEqual methods and to then call that method in the other version of isEqual.
This method compares the contents of two piggybanks to determine if one is less than the other.
It takes 4 arguments: an integer that holds the number of pennies in the piggybank to compare with the current instance, an integer that holds the number of nickels in the piggybank to compare with the current instance, an integer that holds the number of dimes in the piggybank to compare with the current instance, and an integer that holds the number of quarters in the piggybank to compare with the current instance.
It returns a bool: true if the current instance piggybank is less than the piggybank represented by the passed in arguments, false if the current instance piggybank is not less than the piggybank represented by the passed in arguments.
A piggybank is considered to be less than another piggybank if the monetary value of the piggybank is less than the monetary value of the other piggybank. Use the calcValue method to find the monetary value of a piggybank.
This method does the same thing as the other isLessThan method. The difference is that the second piggy bank is passed in as a PiggyBank object rather than four separate integer values.
It is strongly recommended to only formally implement one of the two isLessThan methods and to then call that method in the other version of isLessThan.
This method is an accessor method that can return the value of a single data member in a PiggyBank object.
It takes 1 argument: an integer that represents the data member thats value shoule be returned.
It returns an integer: the value of the data member specified by the passed in index or -1 if the passed in value is invalid.
The following table describes the correspondence between the passed in integer value and the data members.
| coinIndex value | Value to return |
|---|---|
| 0 | number of pennies |
| 1 | number of nickels |
| 2 | number of dimes |
| 3 | number of quarters |
For any other coinIndex value, return -1.
This function displays a title. It is used to help separate the output that is produced by the program. It is NOT a member of the PiggyBank class.
It takes 1 argument: a string that holds the title that should be displayed. It returns nothing.
As mentioned, the goal is to try to separate the output that is produced by the program so that it is a little easier to read. Display a line of 65 dashes followed on the next line by the passed in title.
Display two newline characters before the line of dashes and after displaying the passed in title.
For this assignment, along with adding the methods to the PiggyBank class, int main() must also be coded.
Create 3 PiggyBank objects. The first bank should use the alternate constructor to create an object that has 12 pennies, 34 nickels, 56 dimes, and 78 quarters. The second bank should use the alternate constructor to create an object that has 23 pennies, -5 nickels, -10 dimes, and 31 quarters. The third object should use the copy constructor to create a copy of the first object.
The rest of main() will perform various tasks to test the class methods.
Task 1
Task 2
Task 3
Task 4
Task 5
Task 6
Task 7
Display format for first through fourth PiggyBank objects:
Bank [bank_num] contains [amount] [coin]
where [bank_num] is replaced by 1 through 4, [amount] is replaced by the value returned from getCoin, and [coin] is replaced by the type of coin (pennies, nickels, dimes or quarters). The messages should all end with a single newline character.
Display format for fifth PiggyBank object:
getCoin returned [amount] with invalid index
where [amount] is replaced by the value returned from getCoin. The message should start and end with a single newline character.
Each constructor and method must have a documentation box like a function. However, it is okay to use a single documentation box to describe the overloaded method pairs, but it must include the name of both methods that it describes.
For example, one documentation box can cover the two isEqual methods.
Hand in a copy of the source code on the autograder and Blackboard.
*** Error: cannot add a negative number of nickels *** *** Error: cannot add a negative number of dimes *** ----------------------------------------------------------------- Initial values in the PiggyBank objects Bank 1 object Pennies 12 Nickels 34 Dimes 56 Quarters 78 $26.92 Bank 2 object Pennies 23 Nickels 0 Dimes 0 Quarters 31 $7.98 Bank 3 object Pennies 12 Nickels 34 Dimes 56 Quarters 78 $26.92 ----------------------------------------------------------------- Using the addBanks method with 4 arguments Initial Bank 4 value Pennies 0 Nickels 0 Dimes 0 Quarters 0 $0.00 Bank 2 values after using addBanks method Pennies 23 Nickels 0 Dimes 0 Quarters 31 $7.98 Bank 4 values after using addBanks method Pennies 27 Nickels 27 Dimes 45 Quarters 38 $15.62 ----------------------------------------------------------------- Using the addBanks method with PiggyBank object Initial Bank 1 value Pennies 12 Nickels 34 Dimes 56 Quarters 78 $26.92 Initial Bank 2 value Pennies 23 Nickels 0 Dimes 0 Quarters 31 $7.98 Initial Bank 5 value Pennies 0 Nickels 0 Dimes 0 Quarters 0 $0.00 Bank 1 values after using addBanks method Pennies 12 Nickels 34 Dimes 56 Quarters 78 $26.92 Bank 2 values after using addBanks method Pennies 23 Nickels 0 Dimes 0 Quarters 31 $7.98 Bank 5 values after using addBanks method Pennies 35 Nickels 34 Dimes 56 Quarters 109 $34.90 ----------------------------------------------------------------- Current values in the PiggyBank objects Bank 1 object Pennies 12 Nickels 34 Dimes 56 Quarters 78 $26.92 Bank 2 object Pennies 23 Nickels 0 Dimes 0 Quarters 31 $7.98 Bank 3 object Pennies 12 Nickels 34 Dimes 56 Quarters 78 $26.92 Bank 4 object Pennies 27 Nickels 27 Dimes 45 Quarters 38 $15.62 Bank 5 object Pennies 35 Nickels 34 Dimes 56 Quarters 109 $34.90 ----------------------------------------------------------------- Using the isEqual methods Test 1: banks are equal Test 2: banks are not equal Test 3: second and fifth banks are not equal Test 4: first and third banks are equal ----------------------------------------------------------------- Using the isLessThan methods Test 1: Bank 1 is less than other bank Test 2: Bank 2 is not less than other bank Test 3: Bank 3 is less than Bank 5 Test 4: Bank 4 is not less than Bank 2 ----------------------------------------------------------------- Using the getCoin method Bank 1 contains 12 pennies Bank 2 contains 0 nickels Bank 3 contains 56 dimes Bank 4 contains 38 quarters getCoin returned -1 with invalid index