Source of Calculator.java


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