i am having trouble with this assignment (everything needed for the question is
ID: 3568953 • Letter: I
Question
i am having trouble with this assignment (everything needed for the question is in the link provided)
http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm10.htm
i completely understand how to to do most of the program in fact everything i have already works fine, but the part that asks for methods isEqual, isLessThan, and isGreaterThan confuses me, i have no idea how to implement a those methods into this program please help! all of my code is below and yes the instructor wants this all in one file
#include
#include
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);
bool isEqual(PiggyBank);
bool isLessThan(PiggyBank);
bool isGreaterThan(PiggyBank);
private:
int numPennies;
int numNickels;
int numDimes;
int numQuarters;
static const double PENNY_VAL;
static const double NICKEL_VAL;
static const double DIME_VAL;
static const double QUARTER_VAL;
void calcPiggyBankValue()
{
double BankValue = (numPennies * PENNY_VAL) + (numNickels * NICKEL_VAL) +
(numDimes * DIME_VAL) + (numQuarters * QUARTER_VAL);
}
};
const double PiggyBank::PENNY_VAL = .01;
const double PiggyBank::NICKEL_VAL = .05;
const double PiggyBank::DIME_VAL = .10;
const double PiggyBank::QUARTER_VAL = .25;
int main ()
{
//Test 1 -- default constructor
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 -- adding coins
cout << endl << endl << endl << "***** Test 3: addMethods and printPiggyBankValue *****" << 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 6 -- break the bank
cout << endl << endl << endl << "***** Test 6: 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 << endl << "Total: ";
bank3.printPiggyBankValue();
cout << endl << endl;
return 0;
}
PiggyBank::PiggyBank()
{
numPennies = 0;
numNickels = 0;
numDimes = 0;
numQuarters = 0;
}
PiggyBank::PiggyBank(int numP, int numN, int numD, int numQ)
{
if (numP < 0)
{
cout << "Constructor error: The number of pennies cannot be negative" < numPennies = 0;
}
else
numPennies = numP;
if (numN < 0)
{
cout << "Constructor error: The number of nickels cannot be negative" < numNickels = 0;
}
else
numNickels = numN;
if (numD < 0)
{
cout << "Constructor error: The number of dimes cannot be negative" < numDimes = 0;
}
else
numDimes = numD;
if (numQ < 0)
{
cout << "Constructor error: The number of quarters cannot be negative" < numPennies = 0;
}
else
numQuarters = numQ;
}
void PiggyBank::printPiggyBank()
{
cout << "Pennies: " << numPennies
<< " Nickels: " << numNickels
<< " Dimes: " << numDimes
<< " Quarters: " << numQuarters << endl;
}
void PiggyBank::printPiggyBankValue()
{
double value = (numPennies * PENNY_VAL) + (numNickels * NICKEL_VAL) +
(numDimes * DIME_VAL) + (numQuarters * QUARTER_VAL);
cout << "$" << fixed << setprecision(2) << value;
}
void PiggyBank::addCoins(int addP, int addN, int addD, int addQ)
{
addPennies(addP);
addNickels(addN);
addDimes(addD);
addQuarters(addQ);
}
void PiggyBank::addPennies(int addPen)
{
if (addPen < 0)
{
cout << "addPennies error: the number of additional pennies cannot be negative" << endl;
}
else
numPennies += addPen;
}
void PiggyBank::addNickels(int addNic)
{
if (addNic < 0)
{
cout << "addNickels error: the number of additional nickels cannot be negative" << endl;
}
else
numNickels += addNic;
}
void PiggyBank::addDimes(int addDim)
{
if (addDim < 0)
{
cout << "addDimes error: the number of additional nickels cannot be negative" << endl;
}
else
numDimes += addDim;
}
void PiggyBank::addQuarters(int addQuart)
{
if (addQuart < 0)
{
cout << "addQuarters error: the number of additional nickels cannot be negative" << endl;
}
else
numQuarters += addQuart;
}
void PiggyBank::emptyTheBank()
{
numPennies = 0;
numNickels = 0;
numDimes = 0;
numQuarters = 0;
}
Explanation / Answer
#include<iostream>
#include <iomanip>
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);
bool isEqual(PiggyBank);
bool isLessThan(PiggyBank);
bool isGreaterThan(PiggyBank);
PiggyBank sumPiggyBanks(PiggyBank);
private:
int numPennies;
int numNickels;
int numDimes;
int numQuarters;
static const double PENNY_VAL;
static const double NICKEL_VAL;
static const double DIME_VAL;
static const double QUARTER_VAL;
double calcPiggyBankValue()
{
return (numPennies * PENNY_VAL) + (numNickels * NICKEL_VAL) +
(numDimes * DIME_VAL) + (numQuarters * QUARTER_VAL);
}
};
const double PiggyBank::PENNY_VAL = .01;
const double PiggyBank::NICKEL_VAL = .05;
const double PiggyBank::DIME_VAL = .10;
const double PiggyBank::QUARTER_VAL = .25;
int main ()
{
//Test 1 -- default constructor
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 -- adding coins
cout << endl << endl << endl << "***** Test 3: addMethods and printPiggyBankValue *****" << 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 6 -- break the bank
cout << endl << endl << endl << "***** Test 6: 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 << endl << "Total: ";
bank3.printPiggyBankValue();
cout << endl << endl;
return 0;
}
PiggyBank::PiggyBank()
{
numPennies = 0;
numNickels = 0;
numDimes = 0;
numQuarters = 0;
}
PiggyBank::PiggyBank(int numP, int numN, int numD, int numQ)
{
if (numP < 0)
{
numPennies = 0;
cout << "Constructor error: The number of pennies cannot be negative" <<numPennies;
}
else
numPennies = numP;
if (numN < 0)
{
numNickels = 0;
cout << "Constructor error: The number of nickels cannot be negative" <<numNickels ;
}
else
numNickels = numN;
if (numD < 0)
{
numDimes = 0;
cout << "Constructor error: The number of dimes cannot be negative" << numDimes;
}
else
numDimes = numD;
if (numQ < 0)
{
numPennies = 0;
cout << "Constructor error: The number of quarters cannot be negative" <<numPennies;
}
else
numQuarters = numQ;
}
void PiggyBank::printPiggyBank()
{
cout << "Pennies: " << numPennies
<< " Nickels: " << numNickels
<< " Dimes: " << numDimes
<< " Quarters: " << numQuarters << endl;
}
void PiggyBank::printPiggyBankValue()
{
double value = (numPennies * PENNY_VAL) + (numNickels * NICKEL_VAL) +
(numDimes * DIME_VAL) + (numQuarters * QUARTER_VAL);
cout << "$" << fixed << setprecision(2) << value;
}
void PiggyBank::addCoins(int addP, int addN, int addD, int addQ)
{
addPennies(addP);
addNickels(addN);
addDimes(addD);
addQuarters(addQ);
}
void PiggyBank::addPennies(int addPen)
{
if (addPen < 0)
{
cout << "addPennies error: the number of additional pennies cannot be negative" << endl;
}
else
numPennies += addPen;
}
void PiggyBank::addNickels(int addNic)
{
if (addNic < 0)
{
cout << "addNickels error: the number of additional nickels cannot be negative" << endl;
}
else
numNickels += addNic;
}
void PiggyBank::addDimes(int addDim)
{
if (addDim < 0)
{
cout << "addDimes error: the number of additional nickels cannot be negative" << endl;
}
else
numDimes += addDim;
}
void PiggyBank::addQuarters(int addQuart)
{
if (addQuart < 0)
{
cout << "addQuarters error: the number of additional nickels cannot be negative" << endl;
}
else
numQuarters += addQuart;
}
void PiggyBank::emptyTheBank()
{
numPennies = 0;
numNickels = 0;
numDimes = 0;
numQuarters = 0;
}
bool PiggyBank::isEqual(PiggyBank p2)
{
if(numPennies == p2.numPennies
&& numDimes == p2.numDimes
&& numNickels == p2.numNickels
&& numQuarters == p2.numQuarters)
return true;
else
return false;
}
bool PiggyBank::isLessThan(PiggyBank p2)
{
if(calcPiggyBankValue() < p2.calcPiggyBankValue())
return true;
else
return false;
}
bool PiggyBank::isGreaterThan(PiggyBank p2)
{
if(calcPiggyBankValue() > p2.calcPiggyBankValue())
return true;
else
return false;
}
PiggyBank PiggyBank::sumPiggyBanks(PiggyBank p2)
{
return PiggyBank(numPennies+p2.numPennies, numNickels+p2.numNickels, numDimes+p2.numDimes, numQuarters+p2.numQuarters);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.