In comparing fractions we need
to normalize them. A fraction is normalized if its denominator is positive
and numerator and denominator are relatively primes. For example,
18/-24, normalised is -3/4. We used Math.abs(..) [Question: is abs a
"static" or "instance" method?].
We used the gcd method to make two integers
relatively primes. We showed a method in the Fraction class that
computes the gcd of two numbers using the Euclidean algorithm:
// Precondition: a!=0, b!=0
public static int gcd(int a, int b){
int r = a%b; // 48%30=18, 48%30=-18, -48%30=-18, -48%-30=-18
// i.e. the sign is the sign of the dividend and
// the value, in absolute, is the same independent
// of sign. The divisor must be != 0, but dividend
// can be 0.
while (r != 0) {
a = b;
b = r;
r = a%b;
}
return Math.abs(b);
}