Source of Calculator.java


  1: // Calculator.java

  3: import java.util.Scanner;

  5: /**
  6:  * Simple line-oriented calculator program.
  7:  */
  8: public class Calculator {

 10:     public static final double PRECISION = 0.000001;

 12:     private double result;  // current number on screen
 13:     private boolean done;   // whether the current calculator is finished
 14:     private Scanner keypad; // keypad for this calculator

 16:     public static void main(String[] args) {
 17:         Calculator clerk = new Calculator();
 18:         System.out.println("Calculator is on.");
 19:         System.out.print("Format of each line: ");
 20:         System.out.println("operator space number");
 21:         System.out.println("For example: + 3");
 22:         System.out.println("To end, enter the letter e.");
 23:         clerk.doCalculation();
 24:         System.out.println("The final result is " + clerk.getResult());
 25:         System.out.println("Calculator program ending.");
 26:     }

 28:     /**
 29:      * Build a default caclulator.
 30:      */
 31:     public Calculator() {
 32:         result = 0;
 33:         done = false;
 34:         keypad = new Scanner(System.in);
 35:     }

 37:     /**
 38:      * Reset calculator to zero.
 39:      */
 40:     public void reset() {
 41:         result = 0;
 42:     }

 44:     /**
 45:      * Change the result to a new value.
 46:      *
 47:      * @param newResult the new value
 48:      */
 49:     public void setResult(double newResult) {
 50:         result = newResult;
 51:     }

 53:     /**
 54:      * Return the current value on this calculator.
 55:      *
 56:      * @return the current value of this calculator
 57:      */
 58:     public double getResult() {
 59:         return result;
 60:     }

 62:     /**
 63:      * The heart of a calculator. This does not give
 64:      * instructions. Input errors throw exceptions.
 65:      */
 66:     public void doCalculation() {
 67:         while (!done) {
 68:             // show current value
 69:             System.out.println("result = " + result);
 70:             // process the next command
 71:             processInputLine();
 72:         }
 73:     }

 75:     /**
 76:      * Processes one line of input from the user
 77:      */
 78:     private void processInputLine() {
 79:         // get the operator (first character on the current line)
 80:         char nextOp = (keypad.next()).charAt(0);

 82:         // check for command to end program
 83:         if ((nextOp == 'e') || (nextOp == 'E')) {
 84:             done = true;
 85:         } else {
 86:             // get the operand for this operation
 87:             double operand = getNextOperand();
 88:             double oldValue = result;

 90:             // show the calculation and the answer
 91:             result = evaluate(nextOp, result, operand);
 92:             System.out.println(oldValue + " " + nextOp + " "
 93:                     + operand + " = " + result);
 94:         }
 95:         // clear any stray characters away
 96:         keypad.nextLine();
 97:     }

 99:     /**
100:      * Gets the number to add/subtract/etc from the user.
101:      *
102:      * @return the next number in the user's input.
103:      */
104:     public double getNextOperand() {
105:         double operand;

107:         operand = keypad.nextDouble();

109:         return operand;
110:     }

112:     /**
113:      * Returns n1 op n2, provided op is one of '+', '-', '*', and '/'.
114:      * Any other value of op is ignored.
115:      *
116:      * @param op the operator to apply (one of +, -, * or /.
117:      * @param n1 the left-hand operand
118:      * @param n2 the right-hand operand
119:      * @return the value of op appliied to n1 and n2
120:      */
121:     public double evaluate(char op, double n1, double n2) {
122:         double answer;
123:         switch (op) {
124:         case '+':
125:             answer = n1 + n2;
126:             break;
127:         case '-':
128:             answer = n1 - n2;
129:             break;
130:         case '*':
131:             answer = n1 * n2;
132:             break;
133:         case '/':
134:             if (Math.abs(n2) < PRECISION) {
135:                 System.out.println("You're trying to divide by zero!");
136:                 System.out.println("I can't deal with that!");
137:                 System.out.println("I'm quitting!");
138:                 System.exit(1);
139:             }
140:             answer = n1 / n2;
141:             break;
142:         default:
143:             System.out.println("I don't know that operator (" + op + ")");
144:             System.out.println("I'll pretend the result is unchanged.");
145:             answer = result;
146:             break;
147:         }
148:         return answer;
149:     }

151: }