Question: The Function ADT Class Start your work by completing the tests in Base fraction test file. In order to get these tests to pass you'll
The Function ADT Class
Start your work by completing the tests in Base fraction test file. In order to get these tests to pass you'll need to set up:
- A constructor that takes a numerator and denominator as arguments
- Getters for both the numerator and denominator.
- Utilize the reduce method I've provided for you.
- Throw an exception, with descriptive text, if a fraction is created with a denominator of zero.
Addition and Subtraction
You can do the remaining 3 tests in any order. Note they will not compile until you've added all the operator methods so you should start with that. For the Addition and Subtraction tests to pass you'll need to:
- Overload the "+" operator when adding to another fraction and to a number like a int or long (Hint: If you create the method for a long, then ints will work too)
- Overload the "-" operator when subtracting by another fraction and by a number like a int or long
Multiplication and Division
For the Multiplication and Division tests to pass you'll need to:
- Overload the "*" operator when multiplying to another fraction and to a number like a int or long
- Overload the "/" operator when dividing by another fraction and by a number like a int or long
Comparison
For the comparison tests to pass you need to:
- Overload the "==" operator
- Overload the "!=" operator
- Overload the ">" operator
- Overload the "<" operator
- Overload the ">=" operator
- Overload the "<=" operator
Note:You only need to compare with other fraction objects.
Use main.cpp as the driver, use Fraction.cpp as the implementation.
Fraction.h (Do NOT change the header file's existing variables. )
#include
#include
class Fraction {
public:
Fraction(long numerator, long denominator);
long getNumerator() const;
long getDenominator() const;
private:
void reduce();
long gcd(long a, long b)
long numerator;
long denominator;
};
Fraction.cpp
#include "Fraction.h"
#include
Fraction::Fraction(long numerator, long denominator) {
}
long Fraction::getNumerator() const {
return 0;
}
long Fraction::getDenominator() const {
return 0;
}
/*
* The reduce method reduces the fraction and simplifies
* the sign of the fraction resulting in only the numerator
* being positive or negative.
*/
void Fraction::reduce() {
// store if the fraction is positive or negative.
bool isNegative;
if ((denominator < 0 && numerator < 0) || (denominator > 0 && numerator > 0)) {
isNegative = false;
} else {
isNegative = true;
}
// reset the numerator and denominator to positive
numerator = abs(numerator);
denominator = abs(denominator);
// get the greatest common denominator
long greatest_common_denominator = gcd(numerator, denominator);
// reduce the fraction
numerator = numerator / greatest_common_denominator;
denominator = denominator / greatest_common_denominator;
// set the numerator to negative if the fraction was negative.
if (isNegative) numerator = numerator * -1;
}
/*
* The gcd method returns the greatest common denominator of two numbers.
* The method uses a beautiful, compact, little recursive algorithm that
* we'll discuss more when we get to recursion.
*/
long Fraction::gcd(long a, long b) {
if (a == 0) {return b;}
return gcd(b % a, a);
}
main.cpp
#include
int main() {
std::cout << "Use this for your driver" << std::endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
