Source of Calculator.java


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