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

/ First off, i am using dev C++ and i would like it if it ran smoothly in dev or

ID: 3546250 • Letter: #

Question

/First off, i am using dev C++ and i would like it if it ran smoothly in dev or quincy. Had some problems last time and spent hours trying to figure out why a function wouldnt work and apparently that was why.


Very simple concept but im lost with all these functions.


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...

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
{
public:
    Purse();
    Purse(int,int,int,int);
    void printPurse()const;
    void printPurseValue()const;
    void addCoins(int,int,int,int);
    void addPennies(int);
    void addNickels(int);
    void addDimes(int);
    void addQuarters(int);
    bool isEqual(const Purse&)const;
    bool isLessThan(const Purse&)const;
    bool isGreaterThan(const Purse&)const;
    Purse addPurses(const Purse&)const;
private:
    float purseValue()const;
private:
    int pennies;
    int nickels;
    int dimes;
    int quarters;
};
//****************************************************************************
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()
: pennies(0), nickels(0), dimes(0), quarters(0)
{
}

Purse::Purse(int pennies, int nickels, int dimes, int quarters)
: pennies(pennies), nickels(nickels), dimes(dimes), quarters(quarters)
{
    if (this->pennies < 0)
    {
        cout << "Constructor error: invalid number of pennies" << endl;
        this->pennies = 0;
    }
    if (this->nickels < 0)
    {
        cout << "Constructor error: invalid number of nickels" << endl;
        this->nickels = 0;
    }
    if (this->dimes < 0)
    {
        cout << "Constructor error: invalid number of dimes" << endl;
        this->dimes = 0;
    }
    if (this->quarters < 0)
    {
        cout << "Constructor error: invalid number of quarters" << endl;
        this->quarters = 0;
    }
}

void Purse::printPurse()const
{
    cout << "Pennies: " << setw(3) << pennies << endl
         << "Nickels: " << setw(3) << nickels << endl
         << "Dimes:    " << setw(3) << dimes << endl
         << "Quarters: " << setw(3) << quarters << endl;
}

void Purse::printPurseValue()const
{
    cout << "$" << fixed << setprecision(2) << purseValue() << endl;
}

void Purse::addCoins(int p, int n, int d, int q)
{
    addPennies(p);
    addNickels(n);
    addDimes(d);
    addQuarters(q);
}

void Purse::addPennies(int amount)
{
    if (amount < 0)
        cout << "Add error: invalid number of pennies" << endl;
    else
        this->pennies += amount;
}

void Purse::addNickels(int amount)
{
    if (amount < 0)
        cout << "Add error: invalid number of nickels" << endl;
    else
        this->nickels += amount;
}

void Purse::addDimes(int amount)
{
    if (amount < 0)
        cout << "Add error: invalid number of dimes" << endl;
    else
        this->dimes += amount;
}

void Purse::addQuarters(int amount)
{
    if (amount < 0)
        cout << "Add error: invalid number of quarters" << endl;
    else
        this->quarters += amount;
}

bool Purse::isEqual(const Purse& that)const
{
    return this->purseValue() == that.purseValue();
}

bool Purse::isLessThan(const Purse& that)const
{
    return this->purseValue() < that.purseValue();
}

bool Purse::isGreaterThan(const Purse& that)const
{
    return this->purseValue() > that.purseValue();
}

Purse Purse::addPurses(const Purse& that)const
{
    Purse p;
    p.pennies = this->pennies + that.pennies;
    p.nickels = this->nickels + that.nickels;
    p.dimes    = this->dimes    + that.dimes;
    p.quarters = this->quarters + that.quarters;
    return p;
}

float Purse::purseValue()const
{
    return (pennies + 5*nickels + 10*dimes + 25*quarters) / 100.0;
}