// Adaptado de http://aleph0.clarku.edu/~djoyce/cs101/Resources/Fraction.java
public class FractionOld {
    private int numerator;
    private int denominator;
    FractionOld(int num, int den) {
        numerator = num;
        denominator = den;
    }
    public int getNumerator() {
        return numerator;
    }
    public int getDenominator() {
        return denominator;
    }
    public void setNumerator(int num) {
        numerator = num;
    }
    public void setDenominator(int den) {
        denominator = den;
    }
    // Les funcions que no es criden des de fora la clase son private
    // Aquestes son static perque no utilizen Fraction
    // Preconditions: denom1 and denom2 must be non-zero integer values
    private static int lcm(int denom1, int denom2) { // Minim Comu Multiple
        int factor = denom1;
        while ((denom1 % denom2) != 0) {
            denom1 += factor;
        }
        return denom1;
    }
    // Preconditions: denom1 and denom2 must be positive integer values
    // n1 is assumed to be greater than n2 (n1 > n2 > 0)
    private static int auxGcd(int n1, int n2) {
        int factor;
        while (n2 != 0) {
            factor = n2;
            n2 = n1 % n2;
            n1 = factor;
        }
        return n1;
    }
    private static int gcd(int a, int b) { // Maxim Comu Divisor
        if (a > b) {
            return auxGcd(a, b);
        } else if (a < b) {
            return auxGcd(b, a);
        } else {
            return a;
        }
    }
    // I aquestes dos no poden ser static (denominator i numerator d'un this)
    private FractionOld reduce() {
        int common;
        // get absolute values for numerator and denominator
        int num = Math.abs(numerator);
        int den = Math.abs(denominator);
        common = gcd(num, den);
        return new FractionOld(numerator / common, denominator / common);
    }
    private FractionOld convert(int common) {
        int factor = common / denominator;
        return new FractionOld(numerator * factor, common);
    }
    public FractionOld add(FractionOld b) {
        int common = lcm(denominator, b.denominator);
        // convert fractions to lcd (Least Common Denominator) (MCD)
        FractionOld commonA = this.convert(common);
        FractionOld commonB = b.convert(common);
        FractionOld sum = new FractionOld(commonA.numerator + commonB.numerator, common);
        // reduce the resulting fraction
        return sum.reduce();
    }
    public FractionOld substract(FractionOld b) {
        int common = lcm(denominator, b.denominator);
        // convert fractions to lcd
        FractionOld commonA = convert(common); // "this." implicit
        FractionOld commonB = b.convert(common);
        FractionOld diff = new FractionOld(commonA.numerator - commonB.numerator, common);
        return diff.reduce();
    }
    public FractionOld multiply(FractionOld b) {
        FractionOld product = new FractionOld(numerator * b.numerator,
                denominator * b.denominator);
        return product.reduce();
    }
    public FractionOld divide(FractionOld b) {
        FractionOld result = new FractionOld(numerator * b.denominator,
                denominator * b.numerator);
        return result.reduce();
    }
    @Override
    public String toString() {
        return numerator + "/" + denominator;
    }
}
