CS 202 Fall 2013  >  Assignment 4

CS 202 Fall 2013
Assignment 4

Assignment 4 is due at 2 p.m. Wednesday, October 9. It is worth 30 points.

Procedures

Complete each of the exercises below. Then turn in your work as follows.

  1. Run Check. Demonstrate the code you have written for the exercises below to either the instructor (Glenn G. Chappell) or the T.A. (Zak Williams). Get official approval.
  2. Submission. Submit your code using Blackboard, under Assignment 4 for this class. You should only submit code that has already passed the Run Check. Be sure you attach your source/header files; do not paste them into the text box. Also, send only source & headers; no project files or executables.

We may not look at your homework submission immediately. If you have questions, e-mail me.

Exercises (30 pts total)

Do each of the following exercises. Coding standards are as on Assignment 3.

Exercise A—Class with Operators

Finish class DollarCent, implemented in files dollarcent.h & dollarcent.cpp. Unfinished versions of these files are on the class webpage.

Class DollarCent keeps track of an amount of money, in dollars and cents. The dollar value must be an integer. If the amount of money is nonnegative, then the cent value must be an integer in the range \([0,99]\). If the amount of money is negative, then the cent value must be an integer in the range \([-99,0]\). For example, the value $3.29 would be represented by a dollar amount of \(3\) and a cent amount of \(29\). The value $−3.29 would be represented by a dollar amount of \(-3\) and a cent amount of \(-29\).

To finish the class, write the following operators, all of which should follow standard conventions.

+=
Takes two DollarCent objects. Adds the monetary value of the second to the first.
Binary +
Takes two DollarCent objects. Returns the sum of their monetarthy values, as a DollarCent object.
-=
Takes two DollarCent objects. Subtracts the monetary value of the second from the first.
Binary -
Takes two DollarCent objects. Returns the difference of their monetary values, as a DollarCent object.
Unary -
Takes one DollarCent object. Returns its monetary value with the sign flipped, as a DollarCent object.
Comparison operators ==, !=, <, <=, >, >=
Compare monetary values.
<<
Stream insertion, handled as usual. Examples of printed forms:
$1.20    $-8.39    $0.00    $1234.56    $0.16

Note that there are always exactly two digits after the decimal point and at least one digit before it. Other than the zeroes necessary to follow those rules, there are no leading or trailing zeroes. There are never leading or trailing blanks.

Test Program

A test program is available: dollarcent_test.cpp. If you compile and run this program (unmodified!) with your code, then it will test whether your code works properly.

Notes & Misc Requirements

Exercise B—Collection of Similar Classes

Write classes StarPrint, LinePrint, and BackPrint and a driver program that uses them.


CS 202 Fall 2013: Assignment 4 / Updated: 3 Oct 2013 / Glenn G. Chappell