CSCI 240 Fall 2014

Program 10
Classes
(100 points)


Friday, December 5 by 11:59 PM

Overview

In 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 arithmetic, relational, 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().

Data Members

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

Constructor

Constructor 1:

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:

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

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

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

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.

addCoins

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.

addPennies

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.

addNickels

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.

addDimes

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.

addQuarters

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.

isEqual

This method will determine if two PiggyBank objects are equal. It takes one argument: a PiggyBank object and returns a boolean value. The argument passed to this method is the second PiggyBank object that will be used in the comparison.

Two PiggyBank objects are considered equal if their values are equal.

isLessThan

This method will determine if one PiggyBank object is less than another PiggyBank object. It takes one argument: a PiggyBank object and returns a boolean value. The argument passed to this method is the second PiggyBank object that will be used in the comparison.

The current PiggyBank object is considered less than the passed in PiggyBank object if its value is less than the value of the passed in PiggyBank object.

isGreaterThan

This method will determine if one PiggyBank object is greater than another PiggyBank object. It takes one argument: a PiggyBank object and returns a boolean value. The argument passed to this method is the second PiggyBank object that will be used in the comparison.

The current PiggyBank object is considered greater than the passed in PiggyBank object if its value is greater than the value of the passed in PiggyBank object.

sumPiggyBanks

This method will combine the contents of two PiggyBank objects. For example, if the current PiggyBank object contains 1 penny, 2 nickels, 3 dimes and 4 quarters, and a second PiggyBank object contains 6 pennies, 7 nickels, 8 dimes and 9 quarters, then this method should produce a PiggyBank object that contains 7 pennies, 9 nickels, 11 dimes and 13 quarters.

This method takes one argument: a PiggyBank object and returns a PiggyBank object. The argument passed to this method should be added to the current PiggyBank object. Note: the current PiggyBank object should not be changed by this method.

calcPiggyBankValue

This method calculates and returns the value (in dollars and cents) of a PiggyBank object. It takes no arguments and returns a float or 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 isEqual, isLessThan, isGreaterThan, and printPiggyBankValue methods. It will never be called by anything other than members of the PiggyBank class, therefore it should be implemented as a private method.

Driver Program

The driver program can be found at:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm10.cpp

Output

The output produced when using the driver program above can be found at:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm10out.txt

Programming Notes

  1. Each method must have a documentation box like a function. However, since a lot of the methods perform similar tasks, one documentation box that gives a brief explanation for all of the related methods will suffice.

    For example, one documentation box can cover the addCoins, addPennies, addNickels, addDimes, and addQuarters; printPiggyBank and printPiggyBankValue can be combined into one documentation box; isEqual, isLessThan, and isGreaterThan can be combined into one box; the constructors can be combined into one box.

  2. Hand in a copy of your source code using Blackboard.