2

I tried different ways of using throw new method to state that cannot be divide by zero.

public class FractionDemo
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        Fraction f1 = new Fraction(), f2 = new Fraction(); 

        System.out.print("Enter the numerator for the target fraction:"); 
        int a = scan.nextInt(); 

        System.out.print("Enter the denominator for the target fraction:"); 
        int b = scan.nextInt(); 

        System.out.print("Enter the numerator for the next fraction to test:"); 
        int n = scan.nextInt(); 

        System.out.print("Enter the denominator for the next fraction to test:"); 
        int d = scan.nextInt(); 

        if(f1.equals(f2))
            System.out.print("The fraction you just entered equals the first fraction of" + f1 + "."); 
        else 
            System.out.print("The fraction you just entered does not equal the first fraction of" + f1 + ".");
    }
}

and this one too

public class Fraction
{
    // set variables 
    private int numerator, denominator, fraction; 

    public void Fraction()
    {
        numerator = 0; 
        denominator = 1;
    }

    public void Fraction(int n, int d)
    {
        numerator = n; 
        denominator = d;
    }

    // Numerator
    public void setNumerator(int n)
    {
        numerator = n; 
    }

    public void setDenominator(int d)
    {
        denominator = d;

    }
    public void setCalculation(int n, int d)
    {
        fraction = numerator/denominator;
        if (denominator == 0)
        {
            throw new IllegalArgumentException("Argument 'divisor' is 0");
        }
    }   

    // Fraction
    public String toString()
    {
        return numerator + "/" + denominator; 
    }

    public boolean equals(Fraction f)
    {
        return ((numerator/denominator) == (f.numerator/f.denominator));
    }
}

when I run the program. I was getting "Exception in thread "main" java.lang.ArithmeticException: / by zero at Fraction.equals(Fraction.java:52) and at FractionDemo.main(FractionDemo.Java:31).

The first one direct me to public boolean. How can I fix that?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
blacklune
  • 43
  • 1
  • 7
  • 2
    You should expect an ArithmeticException as you can't divide by zero for integer division, so it doesn't make any sense to check the input after there has been an error, how about your check before the error would occur? – Peter Lawrey Feb 22 '16 at 10:47
  • What is it that you want from the program? – ifly6 Feb 22 '16 at 10:48
  • There was no errors while compiling. Only after compiling. – blacklune Feb 22 '16 at 10:51

1 Answers1

7

You should check for zero BEFORE dividing :

 public void setCalculation(int n, int d)
            {

               if (denominator == 0)
               {
                   throw new IllegalArgumentException("Argument 'divisor' is 0");
               }

               fraction = numerator/denominator;
            }  

Also, the same check needs to be done in the equals() method.

greenPadawan
  • 1,511
  • 3
  • 17
  • 30
Arnaud
  • 17,229
  • 3
  • 31
  • 44