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?