Fraction extends Number implements FieldElement<Fraction>, Comparable<Fraction>, Serializable { public Fraction divide(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (fraction.numerator == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_FRACTION_TO_DIVIDE_BY, fraction.numerator, fraction.denominator); } return multiply(fraction.reciprocal()); } 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 testDivide() { Fraction a = new Fraction(1, 2); Fraction b = new Fraction(2, 3); assertFraction(1, 1, a.divide(a)); assertFraction(3, 4, a.divide(b)); assertFraction(4, 3, b.divide(a)); assertFraction(1, 1, b.divide(b)); Fraction f1 = new Fraction(3, 5); Fraction f2 = Fraction.ZERO; try { f1.divide(f2); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} f1 = new Fraction(0, 5); f2 = new Fraction(2, 7); Fraction f = f1.divide(f2); Assert.assertSame(Fraction.ZERO, f); f1 = new Fraction(2, 7); f2 = Fraction.ONE; f = f1.divide(f2); Assert.assertEquals(2, f.getNumerator()); Assert.assertEquals(7, f.getDenominator()); f1 = new Fraction(1, Integer.MAX_VALUE); f = f1.divide(f1); Assert.assertEquals(1, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); f1 = new Fraction(Integer.MIN_VALUE, Integer.MAX_VALUE); f2 = new Fraction(1, Integer.MAX_VALUE); f = f1.divide(f2); Assert.assertEquals(Integer.MIN_VALUE, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); try { f.divide(null); Assert.fail("MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) {} try { f1 = new Fraction(1, Integer.MAX_VALUE); f = f1.divide(f1.reciprocal()); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} try { f1 = new Fraction(1, -Integer.MAX_VALUE); f = f1.divide(f1.reciprocal()); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} f1 = new Fraction(6, 35); f = f1.divide(15); Assert.assertEquals(2, f.getNumerator()); Assert.assertEquals(175, f.getDenominator()); }