PDA

Click to See Complete Forum and Search --> : Calculator Applet Question


kitsune
Dec 10th, 2003, 10:36 AM
I'm working on a calculator applet. When I try to divide by 0 (like, 1/0), I want it to output a message (like, "You cannot divide by 0"). At the moment, if I try to divide by 0, it outputs "Infinity". Here's my code:
class DivOperation extends MathOperation
{
public double operate(double operand1, double operand2)
{
return operand1 / operand2;
}
}
Since the return type of the method is double, it won't let me return a String filled with the message. Any idea how I could do this?

Dillinger4
Dec 11th, 2003, 12:31 AM
For floating point operations. Division by 0.0 will produce INF (4.0/0.0). Just have a method throw an ArithmeticException. Catch the exception that is thrown then print a message.

public static double operate(double operand1, double operand2) throws ArithmeticException{
return operand1 / operand2;
}