Source of Calculator.java


  1: 
  2: import java.util.*;
  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;
 12:     //Numbers this close to zero are 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.resultValue( ));
 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 resultValue( )
 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:         char nextOp;
 68:         double nextNumber;
 69:         Scanner keyboard = new Scanner(System.in);
 70: 
 71:         boolean done = false;
 72:         result = 0;
 73:         System.out.println("result = " + result);
 74:         while (! done)
 75:         {
 76:             nextOp = (keyboard.next( )).charAt(0);
 77:             if ((nextOp == 'e') || (nextOp == 'E'))
 78:                 done = true;
 79:             else
 80:             {
 81:                 nextNumber = keyboard.nextDouble( );
 82:                 result = evaluate(nextOp, result, nextNumber);
 83:                 System.out.println("result " + nextOp + " "
 84:                                + nextNumber + " = " + result);
 85:                 System.out.println("updated result = " + result);
 86:             }
 87:         }
 88:     }
 89: 
 90:     /**
 91:      Returns n1 op n2,
 92:      provided op is one of '+', '-', '*',or '/'.
 93:      Any other value of op throws UnknownOpException.
 94:     */
 95:     public double evaluate(char op, double n1, double n2)
 96:               throws DivideByZeroException, UnknownOpException
 97:     {
 98:         double answer;
 99:         switch (op)
100:         {
101:             case '+':
102:                 answer = n1 + n2;
103:                 break;
104:             case '-':
105:                 answer = n1 - n2;
106:                 break;
107:             case '*':
108:                 answer = n1 * n2;
109:                 break;
110:             case '/':
111:                 if ( (-precision < n2) && (n2 < precision))
112:                     throw new DivideByZeroException( );
113:                 answer = n1/n2;
114:                 break;
115:             default:
116:                 throw new UnknownOpException(op);
117:         }
118:         return answer;
119:    }
120: 
121:     public void
122:          handleDivideByZeroException(DivideByZeroException e)
123:     {
124:         System.out.println("Dividing by zero.");
125:         System.out.println("Program aborted");
126:         System.exit(0);
127:     }
128: 
129:     public void handleUnknownOpException(UnknownOpException e)
130:     {
131:         System.out.println(e.getMessage( ));
132:         System.out.println("Try again from the beginning:");
133: 
134:         try
135:         {
136:             System.out.print("Format of each line: ");
137:             System.out.println("operator number");
138:             System.out.println("For example: + 3");
139:             System.out.println("To end, enter the letter e.");
140:             doCalculation( );
141:         }
142:         catch(UnknownOpException e2)
143:         {
144:             System.out.println(e2.getMessage( ));
145:             System.out.println("Try again at some other time.");
146:             System.out.println("Program ending.");
147:             System.exit(0);
148:         }
149:         catch(DivideByZeroException e3)
150:         {
151:             handleDivideByZeroException(e3);
152:         }
153:     }
154: }