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.
- 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.
- 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 aDollarCent
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 aDollarCent
object. - Unary
-
- Takes one
DollarCent
object. Returns its monetary value with the sign flipped, as aDollarCent
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
- You may change the internal implementation of
class
DollarCent
any way you like. But the result must compile with the test program and pass all tests. - You are not being asked to write a program.
Do not write “
main
”! - Remember “DRY”. Good code only includes one description of how to perform any particular operation.
- Your code should not produce any output other than that required above.
- As we have done in class, I strongly suggest that you first get the class to compile with the test program. Only then should you make it work properly.
Exercise B—Collection of Similar Classes
Write classes
StarPrint
,
LinePrint
,
and
BackPrint
and a driver program that uses them.
- Each class should be implemented in a header/source
pair named as usual
(“
starprint.h
” & “starprint.cpp
”, etc.) - An object of each class should act as if it maintains a message, stored in a string, and a count: a positive integer.
- Each class should have the following member functions:
- Constructor from
string
,int
. Sets the message and the count to the given values. - Constructor from
string
. Set the message to the given value and the count to 1. - Default constructor. Sets the message to an empty string and the count to 1.
- Get and set functions, as usual:
getMessage
,setMessage
,getCount
,setCount
. - Function
print
, which takes an optionalostream
(defaults tocout
) and prints the message to the stream as specified below.
- Constructor from
- When printing:
- A
StarPrint
object should print count stars, a blank, the message, and then count stars again, followed by a newline. With a message of"abcde"
and a count of 3, it would print*** abcde ***
- A
LinePrint
object should print count lines of dashes, each the same length as the message, then the message on a separate line, then count lines of dashes again, ending with a newline. With a message of"abcde"
and a count of 3, it would print----- ----- ----- abcde ----- ----- -----
- A
BackPrint
object should print count copies of the message backwards, each on a separate line. ending with a newline. With a message of"abcde"
and a count of 3, it would printedcba edcba edcba
- A
- Your driver program
should create three
vector
s, one ofStarPrint
objects, one ofLinePrint
objects, and one ofBackPrint
objects. Eachvector
should contain at least two objects. The program should then go through each vector and call theprint
member function of each of the objects.