/******************************************************************************* * Třída Fraction * * @author Tomáš Pitner * @version 1.00, 10.8.2005 */ public class Fraction { private int numerator; private int denominator; /*************************************************************************** * */ public Fraction(int n, int d) { if(d == 0) { throw new IllegalArgumentException( "Cannot create fraction with denominator 0"); } numerator = n; denominator = d; } public int getNumerator() { return numerator; } public int getDenominator() { return denominator; } public static Fraction add(Fraction f1, Fraction f2) { Fraction result = new Fraction( f1.getNumerator() * f2.getDenominator() + f1.getDenominator() * f2.getNumerator(), f1.getDenominator()*f2.getDenominator()); return result; } public static Fraction multiply(Fraction f1, Fraction f2) { Fraction result = new Fraction( f1.getNumerator()*f2.getNumerator(), f1.getDenominator()*f2.getDenominator()); return result; } public static Fraction subtract(Fraction f1, Fraction f2) { Fraction result = new Fraction( f1.getNumerator() * f2.getDenominator() - f1.getDenominator() * f2.getNumerator(), f1.getDenominator()*f2.getDenominator()); return result; } public static Fraction divide(Fraction f1, Fraction f2) { try { Fraction result = new Fraction( f1.getNumerator()*f2.getDenominator(), f1.getDenominator()*f2.getNumerator()); return result; } catch(IllegalArgumentException iae) { throw new IllegalArgumentException("Cannot divide fraction by 0"); } } public String toString() { return ""+getNumerator()+"/"+getDenominator(); } }