0
    public class Fraction {
    private int numerator; //  
    private int denominator; // 

    Fraction(int nume, int denom)
    {
        numerator = nume;
        denominator = denom;
    }

     public Fraction divide(Fraction other ) throws FractionDivideByZeroException
    {       

         int nume=0, denom=0;
         try {              
                nume = (this.numerator * other.denominator);
                denom = (this.denominator*other.numerator);
                if(nume!=0 && denom==0) throw new FractionDivideByZeroException();
                return new Fraction(nume, denom);
            } catch (FractionDivideByZeroException e) {             
                e.printError();             
            }       
            return new Fraction(nume, denom);               
    }


}
class FractionDivideByZeroException extends Exception
{
    void printError()
    {
        System.out.println("You can not divide by zero!");
    }
}

this is the test class:

public class FractionTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Fraction frac1 = new Fraction(1,2);
        Fraction frac2 = new Fraction(1,3);

        Fraction frac3 = frac1.divide(frac2); // here's the error message saying "Unhandled exception type FractionDivideByZeroException"
        System.out.println(frac3);

    }



}
2
  • Can you tell us what do you expect from your code? Commented Nov 25, 2010 at 12:48
  • (Note that you appear to be allowing new Fraction(1, 0). You should probably check for that in the constructor. Also 0/1 / 0/1 should probably not give 0/0.) Commented Nov 25, 2010 at 12:55

5 Answers 5

2

Take out throws declaration from your divide method. You are throwing it and then handling it there itself.

Your FractionTest uses frac1.divide and the compiler is asking you to handle the exception that is declared in the divide method.

Sign up to request clarification or add additional context in comments.

Comments

1

You've said Divide throws FractionDivideByZeroException

public Fraction divide(Fraction other ) throws FractionDivideByZeroException

but written code to ensure it doesn't. The compiler doesn't know this so is complaining you/ve not handled the exception in the calling code

Comments

1

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);
    }
}

Comments

0

You stated that devide is a method that throws FractionDivideByZeroException.
your test function must catch it.
or... you function does not throw this exception so the throws FractionDivideByZeroException is redundant.

Comments

0

Add try/catch block to your main method. FractionDevidedByZeroException is a checked exception. You should surround them with try/catch. Otherwise you get this compilation error.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Fraction frac1 = new Fraction(1,2);
    Fraction frac2 = new Fraction(1,3);
    try { 
         Fraction frac3 = frac1.divide(frac2); 
         System.out.println(frac3);
    } catch (FractionDevidedByZeroException e) {
        e.printStackTrace();
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.