Source of Calculator.java


  1: //Calculator.java
  2: 
  3: import java.util.Scanner;
  4: 
  5: /**
  6:  Simple line-oriented calculator program. The class
  7:  can also be used to create other calculator programs.
  8: */
  9: public class Calculator
 10: {
 11:     private double result;
 12:     private double precision = 0.0001; // Numbers this close to zero are
 13:                                        // treated as if equal to zero.
 14: 
 15:     public static void main(String[] args)
 16:     {
 17:         Calculator clerk = new Calculator();
 18: 
 19:         try
 20:         {
 21:             System.out.println("Calculator is on.");
 22:             System.out.print("Format of each line: ");
 23:             System.out.println("operator space number");
 24:             System.out.println("For example: + 3");
 25:             System.out.println("To end, enter the letter e.");
 26:             clerk.doCalculation();
 27:         }
 28:         catch(UnknownOpException e)
 29:         {
 30:             clerk.handleUnknownOpException(e);
 31:         }
 32:         catch(DivideByZeroException e)
 33:         {
 34:             clerk.handleDivideByZeroException(e);
 35:         }
 36:         System.out.println("The final result is " +
 37:                                                     clerk.getResult());
 38:         System.out.println("Calculator program ending.");
 39:     }
 40: 
 41:     public Calculator()
 42:     {
 43:         result = 0;
 44:     }
 45: 
 46:     public void reset()
 47:     {
 48:         result = 0;
 49:     }
 50: 
 51:     public void setResult(double newResult)
 52:     {
 53:         result = newResult;
 54:     }
 55: 
 56:     public double getResult()
 57:     {
 58:         return result;
 59:     }
 60: 
 61:     /**
 62:      * The heart of a calculator. This does not give
 63:      * instructions. Input errors throw exceptions.
 64:     */
 65:     public void doCalculation() throws DivideByZeroException,
 66:                                         UnknownOpException
 67:     {
 68:         Scanner keyboard = new Scanner(System.in);
 69: 
 70:         boolean done = false;
 71:         result = 0;
 72:         System.out.println("result = " + result);
 73:         while (!done)
 74:         {
 75:             char nextOp = (keyboard.next()).charAt(0);
 76:             if ((nextOp == 'e') || (nextOp == 'E'))
 77:                 done = true;
 78:             else
 79:             {
 80:                 double nextNumber = keyboard.nextDouble();
 81:                 result = evaluate(nextOp, result, nextNumber);
 82:                 System.out.println("result " + nextOp + " " +
 83:                                    nextNumber + " = " + result);
 84:                 System.out.println("updated result = " + result);
 85:             }
 86:         }
 87:     }
 88: 
 89:     /**
 90:      * Returns n1 op n2, provided op is one of '+', '-', '*',or '/'.
 91:      * Any other value of op throws UnknownOpException.
 92:     */
 93:     public double evaluate(char op, double n1, double n2)
 94:         throws DivideByZeroException, UnknownOpException
 95:     {
 96:         double answer;
 97:         switch (op)
 98:         {
 99:             case '+':
100:                 answer = n1 + n2;
101:                 break;
102:             case '-':
103:                 answer = n1 - n2;
104:                 break;
105:             case '*':
106:                 answer = n1 * n2;
107:                 break;
108:             case '/':
109:                 if ((-precision < n2) && (n2 < precision))
110:                     throw new DivideByZeroException();
111:                 answer = n1 / n2;
112:                 break;
113:             default:
114:                 throw new UnknownOpException(op);
115:         }
116:         return answer;
117:    }
118: 
119:     public void handleDivideByZeroException(DivideByZeroException e)
120:     {
121:         System.out.println("Dividing by zero.");
122:         System.out.println("Program aborted");
123:         System.exit(0);
124:     }
125: 
126:     public void handleUnknownOpException(UnknownOpException e)
127:     {
128:         System.out.println(e.getMessage());
129:         System.out.println("Try again from the beginning:");
130: 
131:         try
132:         {
133:             System.out.print("Format of each line: ");
134:             System.out.println("operator number");
135:             System.out.println("For example: + 3");
136:             System.out.println("To end, enter the letter e.");
137:             doCalculation();
138:         }
139:         catch(UnknownOpException e2)
140:         {
141:             System.out.println(e2.getMessage());
142:             System.out.println("Try again at some other time.");
143:             System.out.println("Program ending.");
144:             System.exit(0);
145:         }
146:         catch(DivideByZeroException e3)
147:         {
148:             handleDivideByZeroException(e3);
149:         }
150:     }
151: }
152: