Question: I need assistance fixing the following java methods... they are resulting in a infintie loop which is causing me to get a stack over flow

I need assistance fixing the following java methods... they are resulting in a infintie loop which is causing me to get a stack over flow error in my test class.

/** * Determines the greatest common divisor between two integers

* @param a

* @param b

* @return an integer that is the greatest common divisor

*/

private int gcd(int a, int b) {

int result;

if (a % b == 0)

{ result = b; }

else { result = gcd(b, a % b); }

return result; }

/** * Puts the fraction into its lowest terms using the gcd method

* @return the lowest terms of a fraction

*/

private Fraction lowestTerms()

{ Fraction newFraction = new Fraction(numerator, denominator);

if (numerator == 0) {

newFraction = new Fraction(numerator, denominator);

return newFraction; }

else {

int GCD = gcd(Math.abs(numerator), Math.abs(denominator));

numerator = this.getNumerator() / GCD;

denominator = this.getDenominator() / GCD;

newFraction = new Fraction(numerator, denominator); }

return newFraction; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!