Two things I see right away: You are dividing num
by gcd()
twice, for each of the numerator and denominator. Also, once, you change the numerator, then the result of the call to gcd()
may change. Call "gcd" once, store its result, and use it later:
int gcd = gcd();
if (gcd > 1) {
this.num = this.num / gcd;
this.den = this.den / gcd;
}
Additionally, there are more efficient ways of obtaining the greatest common divisor: Wikipedia's page. See Euclid's algorithm on that page.