public class Calculator
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: /**
17: * Run this calculator.
18: */
19: public static void main(String[] args) {
20: Calculator clerk = new Calculator();
22: try {
23: System.out.println("Calculator is on.");
24: System.out.print("Format of each line: ");
25: System.out.println("operator space number");
26: System.out.println("For example: + 3");
27: System.out.println("To end, enter the letter e.");
28: clerk.doCalculation();
29: System.out.println("The final result is " + clerk.getResult());
30: } catch (DivideByZeroException e) {
31: System.out.println("Division by zero error.");
32: }
33: System.out.println("Calculator program ending.");
34: }
36: /**
37: * Create a calulator object.
38: */
39: public Calculator() {
40: result = 0;
41: done = false;
42: keypad = new Scanner(System.in);
43: }
45: /**
46: * Reset this calculator's contents top zero.
47: */
48: public void reset() {
49: result = 0;
50: }
52: /**
53: * Change this calculator's comntent to the new value.
54: *
55: * @param newResult the new value this calculator is holding
56: */
57: public void setResult(double newResult) {
58: result = newResult;
59: }
61: /**
62: * Get the content of this calculator.
63: *
64: * @return the current content of this calculator
65: */
66: public double getResult() {
67: return result;
68: }
70: /**
71: * The heart of a calculator. This does not give
72: * instructions. Input errors throw exceptions.
73: */
74: public void doCalculation() {
75: while (!done) {
76: try {
77: // show current value
78: System.out.println("result = " + result);
79: // process the next command
80: processInputLine();
81: } catch (UnknownOpException uoe) {
82: System.out.println(uoe);
83: System.out.println("Input line ignored.");
84: }
85: }
86: }
88: /**
89: * Processes one line of input from the user
90: */
91: private void processInputLine() {
92: // get the operator (first character on the current line)
93: char nextOp = (keypad.next()).charAt(0);
95: // check for command to end program
96: if ((nextOp == 'e') || (nextOp == 'E')) {
97: done = true;
98: } else {
99: // get the operand for this operation
100: double operand = getNextOperand();
101: double oldValue = result;
103: // show the calculation and the answer
104: result = evaluate(nextOp, result, operand);
105: System.out.println(oldValue + " " + nextOp + " "
106: + operand + " = " + result);
107: }
108: // clear any stray characters away
109: keypad.nextLine();
110: }
112: /**
113: * Gets the number to add/subtract/etc.
114: *
115: * @return the next operand (number) entered by the user
116: */
117: public double getNextOperand() {
118: double operand;
120: operand = keypad.nextDouble();
122: return operand;
123: }
125: /**
126: * Returns n1 op n2, provided op is one of '+', '-', '*', and '/'.
127: * Any other value of op throws UnknownOpException.
128: *
129: * @param op the operator to apply (must be known to this calculator)
130: * @param n1 the left-hand operand for the operator
131: * @param n2 the right-hand operand for the operator
132: * @return the value of the operator applied to its given operands
133: */
134: public double evaluate(char op, double n1, double n2) {
135: double answer;
136: switch (op) {
137: case '+':
138: answer = n1 + n2;
139: break;
140: case '-':
141: answer = n1 - n2;
142: break;
143: case '*':
144: answer = n1 * n2;
145: break;
146: case '/':
147: if (Math.abs(n2) < PRECISION) {
148: throw new DivideByZeroException();
149: }
150: answer = n1 / n2;
151: break;
152: default:
153: throw new UnknownOpException(op);
154: }
155: return answer;
156: }
157: }