|
-
Dec 10th, 2003, 11:36 AM
#1
Thread Starter
New Member
Calculator Applet Question
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:
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?
-
Dec 11th, 2003, 01:31 AM
#2
Dazed Member
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.
Code:
public static double operate(double operand1, double operand2) throws ArithmeticException{
return operand1 / operand2;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|