CS 420:
INTRODUCTION TO
PROGRAMMING IN
C++
Kalamazoo
College
PROGRAMMING
PROJECT
4
Due at the beginning of Lab 5
This programming project may be done individually or in groups of two.
Remember that you may not work with the same person on more than
two of the programming projects.
If you do work with a teammate, hand in one copy of your code with both
names on it.
It is okay to get help from
the TAs and/or the instructor if you get stuck,
but you should try to do it on your own first.
Part I: Enhance the Fraction program from Lab 4.
In this assignment you will be implementing comparison operators
for the Fraction class. This is difficult to do, however,
unless the fractions are in reduced (simplest) form.
To do this, we reduce the fractions to equivalent versions that do not
have any common divisor.
- Write a "free" (non-member) function, GCD, that takes two
integers as parameters and computes and returns the Greatest Common
Divisor of the two integers. Add the declaration for the function to
the bottom of fraction.h, after the class declaration for
Fraction. Add the definition for the function to the bottom
of fraction.cpp
A famous algorithm for doing this, called Euclid's algorithm, is given
below. Note that this is a pseudo-code description of the algorithm,
not the C++ code for the algorithm.
GCD (x, y)
- If y is zero, then the GCD is x (i.e., return x)
- Otherwise, the GCD is the same as GCD (y, x mod y). One way you
could do this is to call GCD again with the new parameters, capture
the return value into a variable, and return that variable. (Question:
How do you represent the mod function in C++?)
The reasoning behind why this algorithm works is not very easy to
follow.
If we have time in class, I will
present an explanation of why the algorithm works.
Fortunately we don't always have to fully understand why an
algorithm works in order to use it!
Be sure to test your GCD function.
- Add a new private member function, Reduce, to the
Fraction class. Reduce converts the fraction to
its reduced (simplest) form. For example, Reduce would
convert the fractions 4/2 and 8/4 to 2/1. To
do this, Reduce calculates the greatest common divisor of
the objects numerator and denominator and then divides both by the
greatest common divisor.
Reduce is a modifying function that takes no parameters and
has no return value. Remember that you need to declare it in the
Fraction class declaration as well as implement it in the
fraction.cpp file. Reduce has the pre-condition that
the denominator of the fraction not be zero. If it is zero, you can
just return immediately. (You may wish to print an error message first;
that's up to you.)
Be sure to test your Reduce function.
- Assume that a Fraction object should always be in reduced
form. Add a call to Reduce to any existing Fraction
member functions that need it in order to establish and preserve this
property. For example, the two-parameter constructor might construct
fractions that are not in reduced form, so must explicitly reduce them
using Reduce. For any other member function, you can assume that
the fraction is in reduced form as part of the pre-condition. Which
functions need to call Reduce to be able to guarantee that
the fraction is still in reduced form as part of the post-condition?
- Add a public member function, Equals, to the
Fraction class that compares the current object to another
Fraction object passed in as a parameter. Function
Equals returns true if the current
fraction is equal to the parameter fraction, and false
otherwise.
- Add a public member function, LessThan, to the
Fraction class that compares the current object to another
Fraction object passed in as a parameter. Function
LessThan returns true if the current
fraction is less than the parameter fraction, and false
otherwise.
- Implement free function operators (similar to operator+,
etc.) that perform the six comparison operations
(equals, not equals, less than, greater than, less than or equal to,
greater than or equal to).
Add the function declarations to
the bottom of fraction.h (with the other free function
operator declarations) and the function definitions to the
bottom of fraction.cpp
You can implement all of these operators using the two
public member functions LessThan and Equals, as in the
following function.
bool operator== (Fraction lhs, Fraction rhs)
{
return lhs.Equals (rhs);
}
(In fact, once you implement the operators for less than and equals
using the public member functions, you can implement all of the others
using those two operators.)
- Be sure to test all of your new functions as you complete them.
- Print the modified header file and implementation file for the
Fraction class. Hand in your printout at the beginning of Lab 5.
Part II: Enhance the Aquarium program:
Ascending and Descending Fish