I am making a fraction class and am having problems with the equals method that compares two fractions.
I am having trouble implementing my helper method gcd() within the methods getReducedFraction and the equals method.
In my reduced fraction method-- getting the passed in fraction to use the helper method to get it to reduced terms and
in my equals method-- I need to get the 2 fractions into lowest terms and and then compare them to see if they are equal.
I am having problems figuring out this logic...
AFTER you get it to work, the biggest problem with what you are doing is that there is a bounding problem, and your class won't have any definable bounds. This is because as the denominator gets bigger, the maximum number you can represent is going to shrink.
Your first problem lies in your reducedFraction method. This error is because you try to give a Fraction object an int value since your gcd method returns an int. If I understand you right what you want to do in this method, all you need to do is to divide the numerator and the denominator with their gcd which you get from your method gcd. So the code for reducedFraction would look like this:
public Fraction reducedFraction(){
int gcd = gcd();
int reducedNumerator = numerator / gcd;
int reducedDenominator = denominator / gcd;
Fraction reducedFraction = new Fraction();
reducedFraction.setNumerator(reducedNumerator);
reducedFraction.isDominator(reducedNumerator);
return reducedFraction;
}
That with using your code. I would make changes in your code and add a constructor that takes a numenator and a denominator so you can skip the last two lines in the above code.
Your second problem is your equals method. To compare the two Fractions you need to compare their fully reduced form. But since you now have the method to do so (see above) that shouldn't be too hard. Simply reduce both Fractions and compare them.