Look at your method signature:
public Fraction divide(Fraction other ) throws FractionDivideByZeroException
It says it throws the exception. The main method doesn't catch it, so the compiler will complain.
Catch it in the method or declare that you throw it, but not both.
I think your logic is flawed. You should never be able to create a Fraction with a zero denominator - your constructor should check that.
Your divide() method should be checking to ensure that the numerator of the divisor is not zero. That's the only way to get a divide by zero error.
When you construct the new Fraction that divide returns, the constructor should throw an exception.
Don't catch it in your divide() method; leave the throws clause and remove the try/catch. If it's a checked exception your test case has to catch it.
Here's how I'd write it:
package fraction;
public class Fraction implements Comparable
{
private int numerator;
private int denominator;
public Fraction()
{
this(0);
}
public Fraction(int numerator)
{
this(numerator,1);
}
Fraction(int numerator, int denominator)
{
if (denominator == 0)
throw new IllegalArgumentException("denominator cannot be zero");
this.numerator = numerator;
this.denominator = denominator;
if (this.numerator*this.denominator < 0)
{
this.numerator = -Math.abs(this.numerator);
this.denominator = Math.abs(this.denominator);
}
this.normalize();
}
public Fraction add(Fraction other)
{
return new Fraction(this.numerator*other.denominator+other.numerator*this.denominator, this.denominator*other.denominator);
}
public Fraction sub(Fraction other)
{
return new Fraction(this.numerator*other.denominator-other.numerator*this.denominator, this.denominator*other.denominator);
}
public Fraction mul(Fraction other)
{
return new Fraction(this.numerator*other.numerator, this.denominator*other.denominator);
}
public Fraction div(Fraction other)
{
return new Fraction(this.numerator*other.denominator, this.denominator*other.numerator);
}
@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
Fraction fraction = (Fraction) o;
if (denominator != fraction.denominator)
{
return false;
}
if (numerator != fraction.numerator)
{
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = numerator;
result = 31 * result + denominator;
return result;
}
public int compareTo(Object o)
{
Fraction other = (Fraction) o;
int product1 = this.numerator*other.denominator;
int product2 = other.numerator*this.denominator;
return (product1-product2);
}
@Override
public String toString()
{
return numerator + "/" + denominator;
}
private void normalize()
{
int sign = 1;
if (this.numerator < 0)
{
sign = -1;
}
int gcd = greatestCommonDivisor(Math.abs(this.numerator), Math.abs(this.denominator));
this.numerator /= gcd;
this.denominator /= gcd;
this.numerator *= sign;
}
public static int greatestCommonDivisor(int m, int n)
{
int a = Math.max(m, n);
int b = Math.min(m, n);
if (a == 0)
return b;
if (b == 0)
return a;
while (a != b)
{
if (b > a)
b -= a;
else
a -= b;
}
return b;
}
}
And the partial unit test:
package fraction;
import org.junit.Assert;
import org.junit.Test;
public class FractionTest
{
@Test
public void testAdd()
{
Fraction x = new Fraction(3, 4);
Fraction y = new Fraction(5, 8);
Fraction expected = new Fraction(11, 8);
Assert.assertEquals(expected, x.add(y));
}
@Test
public void testSub()
{
Fraction x = new Fraction(3, 4);
Fraction y = new Fraction(5, 8);
Fraction expected = new Fraction(1, 8);
Assert.assertEquals(expected, x.sub(y));
}
@Test
public void testMul()
{
Fraction x = new Fraction(3, 4);
Fraction y = new Fraction(5, 8);
Fraction expected = new Fraction(15, 32);
Assert.assertEquals(expected, x.mul(y));
}
@Test
public void testDiv()
{
Fraction x = new Fraction(3, 4);
Fraction y = new Fraction(5, 8);
Fraction expected = new Fraction(6, 5);
Assert.assertEquals(expected, x.div(y));
}
@Test
public void testGreatestCommonDivisor()
{
Assert.assertEquals(Fraction.greatestCommonDivisor(48, 180), 12);
Assert.assertEquals(Fraction.greatestCommonDivisor(40902, 24140), 34);
Assert.assertEquals(Fraction.greatestCommonDivisor(2, 199), 1);
Assert.assertEquals(Fraction.greatestCommonDivisor(11, 8), 1);
Assert.assertEquals(Fraction.greatestCommonDivisor(1, 8), 1);
Assert.assertEquals(Fraction.greatestCommonDivisor(15, 32), 1);
Assert.assertEquals(Fraction.greatestCommonDivisor(6, 5), 1);
}
}
new Fraction(1, 0). You should probably check for that in the constructor. Also 0/1 / 0/1 should probably not give 0/0.)