Fraction extends Number implements FieldElement<Fraction>, Comparable<Fraction>, Serializable { public Fraction add(Fraction fraction) { return addSub(fraction, true ); } Fraction(double value);  Fraction(double value, double epsilon, int maxIterations);  Fraction(double value, int maxDenominator); private  Fraction(double value, double epsilon, int maxDenominator, int maxIterations);  Fraction(int num);  Fraction(int num, int den); Fraction abs(); int compareTo(Fraction object); @Override double doubleValue(); @Override boolean equals(Object other); @Override float floatValue(); int getDenominator(); int getNumerator(); @Override int hashCode(); @Override int intValue(); @Override long longValue(); Fraction negate(); Fraction reciprocal(); Fraction add(Fraction fraction); Fraction add(final int i); Fraction subtract(Fraction fraction); Fraction subtract(final int i); Fraction multiply(Fraction fraction); Fraction multiply(final int i); Fraction divide(Fraction fraction); Fraction divide(final int i); double percentageValue(); static Fraction getReducedFraction(int numerator, int denominator); @Override String toString(); FractionField getField(); static final Fraction TWO; static final Fraction ONE; static final Fraction ZERO; static final Fraction FOUR_FIFTHS; static final Fraction ONE_FIFTH; static final Fraction ONE_HALF; static final Fraction ONE_QUARTER; static final Fraction ONE_THIRD; static final Fraction THREE_FIFTHS; static final Fraction THREE_QUARTERS; static final Fraction TWO_FIFTHS; static final Fraction TWO_QUARTERS; static final Fraction TWO_THIRDS; static final Fraction MINUS_ONE; }
@Test public void testAdd() { Fraction a = new Fraction(1, 2); Fraction b = new Fraction(2, 3); assertFraction(1, 1, a.add(a)); assertFraction(7, 6, a.add(b)); assertFraction(7, 6, b.add(a)); assertFraction(4, 3, b.add(b)); Fraction f1 = new Fraction(Integer.MAX_VALUE - 1, 1); Fraction f2 = Fraction.ONE; Fraction f = f1.add(f2); Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); f = f1.add(1); Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); f1 = new Fraction(-1, 13*13*2*2); f2 = new Fraction(-2, 13*17*2); f = f1.add(f2); Assert.assertEquals(13*13*17*2*2, f.getDenominator()); Assert.assertEquals(-17 - 2*13*2, f.getNumerator()); try { f.add(null); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) {} f1 = new Fraction(1,32768*3); f2 = new Fraction(1,59049); f = f1.add(f2); Assert.assertEquals(52451, f.getNumerator()); Assert.assertEquals(1934917632, f.getDenominator()); f1 = new Fraction(Integer.MIN_VALUE, 3); f2 = new Fraction(1,3); f = f1.add(f2); Assert.assertEquals(Integer.MIN_VALUE+1, f.getNumerator()); Assert.assertEquals(3, f.getDenominator()); f1 = new Fraction(Integer.MAX_VALUE - 1, 1); f2 = Fraction.ONE; f = f1.add(f2); Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); try { f = f.add(Fraction.ONE); Assert.fail("expecting MathArithmeticException but got: " + f.toString()); } catch (MathArithmeticException ex) {} f1 = new Fraction(Integer.MIN_VALUE, 5); f2 = new Fraction(-1,5); try { f = f1.add(f2); Assert.fail("expecting MathArithmeticException but got: " + f.toString()); } catch (MathArithmeticException ex) {} try { f= new Fraction(-Integer.MAX_VALUE, 1); f = f.add(f); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} try { f= new Fraction(-Integer.MAX_VALUE, 1); f = f.add(f); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} f1 = new Fraction(3,327680); f2 = new Fraction(2,59049); try { f = f1.add(f2); Assert.fail("expecting MathArithmeticException but got: " + f.toString()); } catch (MathArithmeticException ex) {} }