You have been developing a Fraction class for Teacher’s Pet Software that contai
ID: 3879112 • Letter: Y
Question
You have been developing a Fraction class for Teacher’s 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 Fraction’s 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.
Explanation / Answer
package chegg; public class Example { public static void main(String[] args) { Fraction fraction = new Fraction(); System.out.println("Fraction Value in floating points"); System.out.println(fraction.getFraction()); System.out.println("the value as a fraction"); System.out.println(fraction.getFractionValue()); Fraction fraction1 = new Fraction(2); System.out.println("Fraction Value in floating points"); System.out.println(fraction1.getFraction()); System.out.println("the value as a fraction"); System.out.println(fraction1.getFractionValue()); Fraction fraction2 = new Fraction(3,2); System.out.println("Fraction Value in floating points"); System.out.println(fraction2.getFraction()); System.out.println("the value as a fraction"); System.out.println(fraction2.getFractionValue()); Fraction fraction3 = new Fraction(5,3); System.out.println("Fraction Value in floating points"); System.out.println(fraction3.getFraction()); System.out.println("the value as a fraction"); System.out.println(fraction3.getFractionValue()); Fraction fraction4 = new Fraction(4,7,5); System.out.println("Fraction Value in floating points"); System.out.println(fraction4.getFraction()); System.out.println("the value as a fraction"); System.out.println(fraction4.getFractionValue()); } } package chegg; public class Fraction { int wholeNumber; int numerator; int denominator; Fraction() { this.numerator = 0; this.denominator = 1; wholeNumber = getWholeNumberPortion(); } Fraction(int numerator) { this.numerator = numerator; this.denominator = 1; wholeNumber = getWholeNumberPortion(); } Fraction(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; wholeNumber = getWholeNumberPortion(); } int getWholeNumberPortion() { return numerator/denominator; } Fraction(int wholeNumber, int numerator, int denominator) { this.wholeNumber = wholeNumber; this.numerator = numerator; this.denominator = denominator; } int greatestCommonDenominator() { return gcd(numerator, denominator); } static int gcd(int numerator, int denominator) { if (numerator == 0 || denominator == 0) return 0; if (numerator == denominator) return numerator; if (numerator > denominator) return gcd(numerator-denominator, denominator); return gcd(numerator, denominator-numerator); } float getFraction () { return (float)numerator/(float)denominator; } String reduceFraction() { int gcd = greatestCommonDenominator(); int num = numerator/gcd; int den = denominator/gcd; return num + "/" + den; } String getFractionValue() { if(wholeNumber>0) { String fraction = wholeNumber + " "; if(numerator%denominator == 0) { return fraction; } int value = numerator%denominator; return fraction + value + "/" + denominator; } if(wholeNumber == 0 && getFraction() == 0) { return "0"; } return reduceFraction(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.