

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A programming assignment for csci 270 students in the fall of 2004. The assignment requires students to create a new class named money to represent positive dollars-and-cents values. The class should provide operations for creating, inputting, assigning, outputting, adding, subtracting, multiplying, and dividing money objects using integer arithmetic. The class should also support comparisons between money objects. The assignment includes an extra credit opportunity to modify the class to represent negative money amounts.
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Fall 2004 Due: Tuesday, 14 Sept Emphasis on: Creating and testing a class Calculations involving monetary values can be a problem when your language offers only a floating-point type for representing fractional values. Fractional money values represented as type float are stored with extra digits of precision which can introduce errors into monetary calculations. Create a new class to represent positive dollars-and-cents values with operations that will allow accurate calculations using integer arithmetic rather than float. Your new class should define a single money object (so the client can declare as many of them as necessary for the particular client application) represented by private variables of an integer dollars value and an integer cents value. You should provide operations which allow a client to:
0.00 to +1000.00. No negative values are expected to be input or calculated.
Create a client program to test your class. Since we’re not writing a real application using this class, we just need to call each of the functions to make sure each one operates as it should. The program should read in from the keyboard values for two money objects and test the class functions, producing output similar to the following example (where a and b are the names of money objects). You don’t have to demonstrate all possible ways of declaring and assigning values to money variables; the client can either call the class function to input a money value (#3 in the list of required functions), or the client can input the value and call the class function to assign the value to the money variable (#4 in the list of required functions). For a = 29.99, dollars = 29 and cents = 99 For b = 14.95, dollars = 14 and cents = 95 29.99 + 14.95 = 44. 29.99 - 14.95 = 15. 29.99 * 5 = 149.95 Note: the 5 used here can be a constant 14.95 * 5 = 74. 29.99 / 3 = 9.99 Note: the 3 used here can be a constant 14.95 / 3 = 4. 29.99 < 14.95 is false 14.95 < 29.99 is true 29.99 = 14.95 is false 29.99 > 14.95 is true 14.95 > 29.99 is false Then prompt the user to input new values for a and b and repeat the sequence to produce similar output using the new values. Example of a partial client program: int main () { Money a, b, c; // a, b, and c are initialized to 0. cout << “Enter a dollars-and-cents value, like 14.75 => “; cin >> a; // or a.Input(cin); cout << “Enter another dollars-and-cents value => “; cin >> b; // or b.Input(cin); cout << “For a = “ << a << “, dollars = “ << a.GetDollars() << “ and cents = “ << a.GetCents() << endl; c = a + b; // or c = a.Add(b); cout << a << “ + “ << b << “ = “ << c << endl; // or if you don’t have the output operator overloaded: // a.Output(cout); // cout << “ + “; // b.Output(cout); // cout << “ = “; // c.Output(cout); // cout << endl; c = a * 5; // or c = a.Multiply(5); cout << a << “ * 5 = “ << c << endl; cout << a << “ < “ << b << “ is “ << (a < b) << endl; return 0; }