Fraction Class

Write a Fraction class that overloads the arithmetic operators (+, -, *, /). Minimally, your fraction class should include the following methods that behave as directed:

Fraction()
This is a default constructor for the fraction class. It should load a value of 1 into the denominator and a value of 0 into the numerator

Fraction(int numerator, int denominator)
This is an overloaded version of the constructor. It accepts inputs for the numerator and denominator and creates a fraction that starts out with those values. Be mindful to sanitize the input to ensure a zero is not loaded into the denominator.

int getNumerator()
This is a getter method for the numerator.

void setNumerator()
This is a setter method for the numerator.

int getDenominator()
This is a getter method for the denominator.

void setDenominator()
This is a setter method for the denominator. Ensure the user enters valid values for this (no zeros allowed!).

void Reduce()
This method should reduce the contents of the fraction. The resultant fraction should be an equivalent, improper fraction in lowest terms (no mixed numbers).

Fraction operator+(Fraction addMe)
This method implements behavior for the addition operator. Your driver should clearly demonstrate the use of this operator.

Fraction operator-(Fraction subtractMe)
This method implements behavior for the subtraction operator. Your driver should clearly demonstrate the use of this operator.

Fraction operator*(Fraction multiplyMe)
This method implements the behavior for the subtraction operator. Your driver should clearly demonstrate the use of this operator.

Fraction operator/(Fraction divideMe)
This method implements behavior for the division operator. Your driver should clearly demonstrate the use of this operator.

Remember to call your reduction method every time you perform an operator on a fraction. This will ensure that the resultant Fraction is in lowest terms.
Remember to sanitize inputs for your Fractions. A denominator of zero should never be allowed at anytime. If the client code attempts to load a zero into a denominator, you should print a message informing the user that this has occurred. Additionally, you should make a decision about the action the program takes in terms of picking a value to assign to the denominator when such an errors occurs. The best course of action is probably to change nothing and just display a message to the user.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

×
New Download