CSC 309 Object-Oriented Programming in C++
Homework #5
Due: Feb 7 [Loop], 8 [DL] (1 week)
Write a class called Fraction and an application program which tests
the class. Your job is to write 3 files, completely from scratch.
- Fraction.h -- header file
- Fraction.cpp -- implementation file
- FractApp.cpp -- application file
To set up the header file and implementation file (with macroguard and #include's),
review the previous homework assignments.
1. Class Fraction (in "Fraction.h" and "Fraction.cpp")
Fractions are of the form a / b, where a
is the numerator and b is the denominator, and a,b are integers
(and b is not 0). In addition to the two int data members, provide the
following methods (16 class methods and 1 regular function):
- Class methods:
- Fraction(int n = 0, int d = 1);
- int getNum() const;
- int getDenom() const;
- Fraction operator+(const Fraction& f2) const;
- Fraction operator-(const Fraction& f2) const; //
binary subtraction
- Fraction operator*(const Fraction& f2) const;
- Fraction operator/(const Fraction& f2) const;
- Fraction operator-() const; // unary minus
- bool operator==(const Fraction& f2) const;
- bool operator!=(const Fraction& f2) const;
- bool operator<(const Fraction& f2) const;
- bool operator<=(const Fraction& f2) const;
- bool operator>(const Fraction& f2) const;
- bool operator>=(const Fraction& f2) const;
- void reduce();
- void standardize();
- Regular functions:
- ostream& operator<<(ostream& out, const Fraction&
f);
Notes on the methods and functions:
- The constructor and two get methods should be
straight-forward.
As a reminder, the full definition of the constructor (in the implementation
file) should not have default values in the parameter.
- Arithmetic operations on fractions are defined by the following
rules:
- a / b + c / d = (ad + bc) / bd
- a / b - c / d = (ad - bc) / bd
- a / b * c / d = ac / bd
- (a / b) / (c / d) = ad / bc (where c/d is not 0)
- -(a / b) = (-a) / b
- Relational operators should be obvious, after understanding the
arithmetic operations.
NOTE: Do NOT convert a Fraction to a double in order to compare two
Fraction's. That's because a double can express only up to 15 decimal digits,
while a Fraction can express numbers of higher precisions.
- The reduce() method modifies 'this' object to a reduced form.
For example, 10/15 ==> 2/3.
To write this method, you will need to find the greatest common divisor (gcd)
of the numerator and denominator. You are welcome to use the
following. You can put it as a private method (since it will
only be called by other member functions, not from outside).
class Fraction
{
public:
...
private:
...
int gcd(int a, int b) const;
};
int Fraction::gcd(int a, int b) const
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
- The standardize() method modifies 'this' object to a standardized
form (with a positive denominator). For example,
2/-3 ==> -2/3
-2/-3 ==> 2/3
- The output operator (<<) should print the fraction as
"a/b" to the parameter out.
2. Application (in "FracApp.cpp")
Write an application which tests all the methods you wrote. The output
of the program should be:
1/3 + 5/8 = 23/24
1/3 - 5/8 = -7/24
1/3 * 5/8 = 5/24
1/3 / 5/8 = 8/15
-(1/3) = -1/3
24/21, after reduce(), 8/7
5/8, after reduce(), 5/8
2/-3, after standardize(), -2/3
-2/-3, after standardize(), 2/3
11/15 < 17/23 is true
11/15 <= 17/23 is true
11/15 > 17/23 is false
11/15 >= 17/23 is false
11/15 == 17/23 is false
11/15 != 17/23 is true
11/15 < 22/30 is false
11/15 <= 22/30 is true
11/15 > 22/30 is false
11/15 >= 22/30 is true
11/15 == 22/30 is true
11/15 != 22/30 is false
Write necessary code to produce this output. Of course you must create
appropriate Fraction objects and call methods to produce the results.
NOTE: There is no interaction with the user in the program.
Submission
- Zip all source files involved (.cpp and .h), and
submit the zip file through Course OnLine
(COL).