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

In this assignment, implement a class called Purse that will be used to represen

ID: 3546698 • Letter: I

Question

In this assignment, implement a class called Purse 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.

The Purse class definition 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().

The Purse class should contain four private data members. They are:

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

The second constructor for the Purse class should take four integer arguments: the number of pennies, nickels, dimes, and quarters respectively. For each argument, if the passed in value is nonnegative, 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.

This method prints the contents of a Purse object. It takes no arguments and returns nothing. It should print the contents of the object on 4 lines in the format:

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

This method will add coins to a Purse 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 Purse object.

Before adding a value to a data member, verify that that the value is nonnegative. If it is nonnegative, 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.

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

Before adding a value to the pennies data member, verify that that the passed in value is nonnegative. If it is nonnegative, 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.

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

Before adding a value to the nickels data member, verify that that the passed in value is nonnegative. If it is nonnegative, 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.

This method will add dimes to a Purse object. It takes one integer argument and returns nothing. The argument passed to this method is the number of dimes to add to the Purse 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.

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

Before adding a value to the quarters data member, verify that that the passed in value is nonnegative. If it is nonnegative, 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.

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

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

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

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

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

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

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

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

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

For example, if the current Purse 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 printPurseValue methods, therefore it should be implemented as a private method.

The driver program can be found at:

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

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

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

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; printPurse and printPurseValue 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.

Hand in a copy of your source code using Blackboard.

Explanation / Answer

/******************************************************************************
CSCI 240         Program 8     Fall 2013
Programmer:
Section:
Date Due:
Purpose:
******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
//*************** Place your class description after this line ***************
class Purse
{
private:
int no_of_pennies;
int no_of_nickels;
int no_of_dimes;
int no_of_quarters;
float purseValue();
public:
Purse();
Purse(int p,int n,int d,int q);
void printPurse();
void printPurseValue();
void addCoins(int p,int n,int d,int q);
void addPennies(int p);
void addNickels(int n);
void addDimes(int d);
void addQuarters(int q);
bool isEqual( Purse another);
bool isLessThan( Purse another);
bool isGreaterThan( Purse another);
Purse addPurses(const Purse& another);
};
//****************************************************************************
int main()
{
//Test 1 -- default constructor
cout << "***** Test 1 *****" << endl << endl
     << "Default constructor produces purse1: " << endl << endl;
Purse purse1;
purse1.printPurse();
cout << endl << "Total: ";
purse1.printPurseValue();
//Test 2 -- constructor with arguments
cout << endl << endl << endl << "***** Test 2 *****" << endl << endl
    << "Constructor with 3, 4, 5, 6 produces purse2: " << endl << endl;
Purse purse2(3, 4, 5, 6);
purse2.printPurse();
cout << endl << "Total: ";
purse2.printPurseValue();
cout << endl << endl << endl << "Constructor with 7, 0, 9, 0 produces purse3:"
    << endl << endl;
Purse purse3 = Purse(7, 0, 9, 0);
purse3.printPurse();
cout << endl << "Total: ";
purse3.printPurseValue();
cout << endl << endl << endl << "Constructor with -7, 7, -9, 9 produces purse4:"
    << endl << endl;
Purse purse4 = Purse(-7, 7, -9, 9);
purse4.printPurse();
cout << endl << "Total: ";
purse4.printPurseValue();
//Test 3 -- adding coins
cout << endl << endl << endl << "***** Test 3 *****" << endl << endl
    << "Adding 2 pennies, 7 nickels, 11 dimes, and 2 quarters to purse2 produces:"
     << endl << endl;
purse2.addCoins( 2, 7, 11, 2 );
purse2.printPurse();
cout << endl << "Total: ";
purse2.printPurseValue();
cout << endl << endl << endl << "Adding 4 pennies to purse3:" << endl << endl;
purse3.addPennies( 4 );
purse3.printPurse();
cout << endl << "Total: ";
purse3.printPurseValue();
cout << endl << endl << endl << "Adding -10 nickels to purse4:" << endl << endl;
purse4.addNickels( -10 );
purse4.printPurse();
cout << endl << "Total: ";
purse4.printPurseValue();
cout << endl << endl << endl << "Adding 15 dimes to purse4:" << endl << endl;
purse4.addDimes( 15 );
purse4.printPurse();
cout << endl << "Total: ";
purse4.printPurseValue();
cout << endl << endl << endl << "Adding 4 quarters to purse2: " << endl << endl;
purse2.addQuarters( 4 );
purse2.printPurse();
cout << endl << "Total: ";
purse2.printPurseValue();
//Test 4 -- comparisons
cout << endl << endl << endl << "***** Test 4 *****" << endl << endl
     << "Comparing purse5 and purse 6:" << endl << endl;
Purse purse5(5, 1, 2, 1);
Purse purse6(0, 1, 0, 2);
cout << endl << "purse5 contains: " << endl << endl;
purse5.printPurse();
cout << endl << "Total: ";
purse5.printPurseValue();
cout << endl << endl << "purse6 contains: " << endl << endl;
purse6.printPurse();
cout << endl << "Total: ";
purse6.printPurseValue();
cout << endl << endl
     << "purse5 == purse6 is " << boolalpha << purse5.isEqual( purse6 ) << noboolalpha << endl
     << "purse5 < purse6 is " << boolalpha << purse5.isLessThan( purse6 ) << noboolalpha << endl
     << "purse5 > purse6 is " << boolalpha << purse5.isGreaterThan( purse6 ) << noboolalpha << endl;
cout << endl << endl << "After adding 1 penny to purse 5:" << endl;
purse5.addPennies( 1 );
cout << endl << "purse5 contains: " << endl << endl;
purse5.printPurse();
cout << endl << "Total: ";
purse5.printPurseValue();
cout << endl << endl << "purse6 contains: " << endl << endl;
purse6.printPurse();
cout << endl << "Total: ";
purse6.printPurseValue();
cout << endl << endl
     << "purse5 == purse6 is " << boolalpha << purse5.isEqual( purse6 ) << noboolalpha << endl
     << "purse5 < purse6 is " << boolalpha << purse5.isLessThan( purse6 ) << noboolalpha << endl
     << "purse5 > purse6 is " << boolalpha << purse5.isGreaterThan( purse6 ) << noboolalpha << endl;
//Test 5 -- addition
cout << endl << endl << "***** Test 5 *****" << endl << endl
    << "Add purse5 and purse 6. The result is in purse7:" << endl << endl;
Purse purse7;
purse7 = purse5.addPurses( purse6 );
cout << endl << "purse5 contains: " << endl << endl;
purse5.printPurse();
cout << endl << "Total: ";
purse5.printPurseValue();
cout << endl << endl << "purse6 contains: " << endl << endl;
purse6.printPurse();
cout << endl << "Total: ";
purse6.printPurseValue();
cout << endl << endl << "purse7 contains: " << endl << endl;
purse7.printPurse();
cout << endl << "Total: ";
purse7.printPurseValue();
return 0;
}
//*************** Implement your class methods after this line ***************
Purse::Purse()
{
no_of_pennies =0;
no_of_nickels =0;
no_of_dimes=0;
no_of_quarters=0;
}
Purse::Purse(int p,int n,int d,int q)
{
if(p<0)
{
cout <<"Constructor error: invalid number of pennies"<<endl;
no_of_pennies =0;
}
else
no_of_pennies =p;
if(n<0)
{
cout <<"Constructor error: invalid number of Nickels"<<endl;
no_of_nickels =0;
}
else
no_of_nickels =n;
if(d<0)
{
cout <<"Constructor error: invalid number of dimes"<<endl;
no_of_dimes =0;
}
else
no_of_dimes =d;
if(q<0)
{
cout <<"Constructor error: invalid number of Quarters"<<endl;
no_of_quarters =0;
}
else
no_of_quarters =q;
}

void Purse::printPurse()
{
cout <<"Pennies:   "<<no_of_pennies << endl;
cout <<"Nickels:   "<< no_of_nickels<< endl;
cout <<"Dimes:     "<<no_of_dimes << endl;
cout <<"Quarters: "<<no_of_quarters << endl;
}
void Purse::printPurseValue()
{
double total = 0.01*no_of_pennies+0.05*no_of_nickels+0.1*no_of_dimes+0.25*no_of_quarters;
cout <<"$" <<fixed<<setprecision(2)<< total << endl;
}
void Purse::addCoins(int p,int n,int d,int q)
{
addPennies(p);
addNickels(n);
addDimes(d);
addQuarters(q);
}
void Purse::addPennies(int p)
{
if(p<0)
cout <<"Add error: invalid number of pennies" <<endl;
else
no_of_pennies =no_of_pennies+p;
}
void Purse::addNickels(int n)
{
if(n<0)
cout <<"Add error: invalid number of nickels"<<endl;
else
no_of_nickels =no_of_nickels+n;
}
void Purse::addDimes(int d)
{
if(d<0)
cout <<"Add error: invalid number of dimes" <<endl;
else
no_of_dimes =no_of_dimes+d;
}
void Purse::addQuarters(int q)
{
if(q<0)
cout <<"Add error: invalid number of quarters"<<endl;
else
no_of_quarters =no_of_quarters+q;
}
bool Purse::isEqual(Purse another)
{
return (purseValue()==another.purseValue());
}
bool Purse::isLessThan(Purse another)
{
return (purseValue()<another.purseValue());
}
bool Purse::isGreaterThan(Purse another)
{
return (purseValue()>another.purseValue());
}
Purse Purse::addPurses(const Purse& another)
{
return Purse(no_of_pennies+another.no_of_pennies,
             no_of_nickels+another.no_of_nickels,
             no_of_dimes+another.no_of_dimes,
            no_of_quarters+another.no_of_quarters);
}
float Purse::purseValue()
{
return 0.01*no_of_pennies+0.05*no_of_nickels+0.1*no_of_dimes+0.25*no_of_quarters;
}