Define a class called Fraction. This class is used to represent aration of two i
ID: 3619302 • Letter: D
Question
Define a class called Fraction. This class is used to represent aration of two integers.Please define at least the following methods:
1. a mutator function for user to set the numerator.
2. a mutator function for user to set the denominator.
3. a function to calculate the value of the fraction (numerator /denominator ) and
return it as type of double.
4. a function to output the value of the fraction reduced to lowestterms. (e.g.,
instead of outputting 20/60, the method should output 1/3). Thiswill require
finding the greatest common divisor for the numerator anddenominator, then
dividing both by that number Define a class called Fraction. This class is used to represent aration of two integers. Please define at least the following methods: 1. a mutator function for user to set the numerator. 2. a mutator function for user to set the denominator. 3. a function to calculate the value of the fraction (numerator /denominator ) and return it as type of double. 4. a function to output the value of the fraction reduced to lowestterms. (e.g., instead of outputting 20/60, the method should output 1/3). Thiswill require finding the greatest common divisor for the numerator anddenominator, then dividing both by that number
Explanation / Answer
publicclass Fraction { privateint numerator, denominator; publicFraction(int num, int den) { setNumerator(num); setDenominator(den); } publicvoid setNumerator(int num) { numerator = num; } publicvoid setDenominator(int den) { denominator = den; } publicdouble getValue() { return (double)numerator/denominator; } publicString toString() { int num =numerator; int den =denominator; for(int i= 2; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.