Results 1 to 2 of 2

Thread: Calculator Applet Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Illinois
    Posts
    9

    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?

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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
  •  



Click Here to Expand Forum to Full Width