Source of PrelimCalculator.java


  1: //PrelimCalculator.java
  2: 
  3: import java.util.Scanner;
  4: 
  5: /**
  6:  PRELIMINARY VERSION without exception handling.
  7:  Simple line-oriented calculator program. The class
  8:  can also be used to create other calculator programs.
  9: */
 10: public class PrelimCalculator
 11: {
 12:     private double result;
 13:     private double precision = 0.0001; // Numbers this close to zero are
 14:                                        // treated as if equal to zero.
 15: 
 16:     public static void main(String[] args) throws DivideByZeroException,
 17:                                                   UnknownOpException
 18:     {
 19:         PrelimCalculator clerk = new PrelimCalculator();
 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:         System.out.println("The final result is " +
 28:                                                    clerk.getResult());
 29:         System.out.println("Calculator program ending.");
 30:     }
 31: 
 32:     public PrelimCalculator()
 33:     {
 34:         result = 0;
 35:     }
 36: 
 37:     public void reset()
 38:     {
 39:         result = 0;
 40:     }
 41: 
 42:     public void setResult(double newResult)
 43:     {
 44:         result = newResult;
 45:     }
 46: 
 47:     public double getResult()
 48:     {
 49:         return result;
 50:     }
 51: 
 52:     public void doCalculation() throws DivideByZeroException, UnknownOpException
 53:     {
 54:         Scanner keyboard = new Scanner(System.in);
 55:         boolean done = false;
 56:         result = 0;
 57:         System.out.println("result = " + result);
 58: 
 59:         while (!done)
 60:         {
 61:            char nextOp = (keyboard.next()).charAt(0);
 62:             if ((nextOp == 'e') || (nextOp == 'E'))
 63:                 done = true;
 64:             else
 65:             {
 66:                 double nextNumber = keyboard.nextDouble();
 67:                 result = evaluate(nextOp, result, nextNumber);
 68:                 System.out.println("result " + nextOp + " " +
 69:                                     nextNumber + " = " + result);
 70:                 System.out.println("updated result = " + result);
 71:             }
 72:         }
 73:     }
 74: 
 75:     /**
 76:      Returns n1 op n2, provided op is one of '+', '-', '*',or '/'.
 77:      Any other value of op throws UnknownOpException.
 78:     */
 79:     public double evaluate(char op, double n1, double n2)
 80:                throws DivideByZeroException, UnknownOpException
 81:     {
 82:         double answer;
 83:         switch (op)
 84:         {
 85:             case '+':
 86:                 answer = n1 + n2;
 87:                 break;
 88:             case '-':
 89:                 answer = n1 - n2;
 90:                 break;
 91:             case '*':
 92:                 answer = n1 * n2;
 93:                 break;
 94:             case '/':
 95:                 if ((-precision < n2) && (n2 < precision))
 96:                     throw new DivideByZeroException();
 97:                 answer = n1 / n2;
 98:                 break;
 99:             default:
100:                 throw new UnknownOpException(op);
101:        }
102:        return answer;
103:     }
104: }
105: