Question
You have been developing a Fraction class for Teachers Pet Software that contains several fields and functions. Add another private field that stores any whole
You have been developing a Fraction class for Teachers Pet Software that contains several fields and functions. Add another private field that stores any whole number portion of a Fraction object.
Add two constructors to the class. The first accepts two integer values representing the numerator and denominator. If a single integer is passed to the constructor, use it as the numerator, and use a default value of 1 for the denominator. If no values are passed to the constructor, use a value of 0 for the numerator and 1 for the denominator. When any Fraction is constructed with a 0 argument for the denominator, force the denominator value to 1.
The second constructor requires three arguments: a whole number portion for a fraction, a numerator, and denominator. This constructor executes when any fraction abject is instantiated using three arguments.
Each constructor determines whether the Fraction value is greater than 1. if it is, compute the whole numbers and store it in the whole number field, and reduce the numerator of the Fraction accordingly. For example, a Fraction created as Fraction(2/7) stores 0 in the whole number field, 2 in the numerator, and 7 in the denominator. A Fraction created as Fraction(7/2) stores 3 in the whole number field, 1 in the numerator, and 2 in the denominator.
Add two new member functions to the Fraction class. The first function, called greatestCommonDenominator(), finds the largest number that can divide evenly into both the numerator and denominator. For example, the greatest common denominator of 8 and 12 is 4. the second function, named reduceFraction(), reduces a Fraction to its proper format. For example, the fraction 8/12 should be reduced to 2/3. To reduce a fraction, you find the greatest common denominator of both the numerator and the denominator, then divide both by that number. Whenever a Fraction object is constructed, automatically reduce the Fraction to the proper format.
Include a Fraction function that displays a Fractions value as a floating point number, and then displays the value as a fraction. Include the whole number portion, a space, the numerator, a slash, and the denominator (for example, 4 1/2). If the whole number is 0, do not display the whole number portion of the Fraction (for example, 5/7, not 0 5/7); if the numerator is 0, do not display the fraction portion of the Fraction. If the whole number is 0 and the numerator is 0, then simply display 0.
Write a main() program that declares several Fraction objects, and confirm that the class works correctly.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started