1 import java.awt.*;
2 import java.awt.event.*;
3
4 class Calc extends Frame{
5 private Display display;
6 private Operator operator;
7 private Context context;
8 private int register1;
9 private int register2;
10
11 private static class Display extends Label{
12 Display(){
13 setAlignment(Label.RIGHT);
14 }
15 void setNumber(int value){
16 setText(String.valueOf(value));
17 }
18 }
19
20 private Panel createKeyboard(){
21 Panel keyboard = new Panel();
22
23 keyboard.setLayout(new GridLayout(4, 4));
24
25 keyboard.add(new AllClearButton());
26 keyboard.add(new OperatorButton(Operator.DIVIDE));
27 keyboard.add(new OperatorButton(Operator.MULTIPLY));
28 keyboard.add(new OperatorButton(Operator.SUBTRACT));
29
30 keyboard.add(new NumberButton(7));
31 keyboard.add(new NumberButton(8));
32 keyboard.add(new NumberButton(9));
33 keyboard.add(new OperatorButton(Operator.ADD));
34
35 keyboard.add(new NumberButton(4));
36 keyboard.add(new NumberButton(5));
37 keyboard.add(new NumberButton(6));
38 keyboard.add(new ResultButton());
39
40 keyboard.add(new NumberButton(1));
41 keyboard.add(new NumberButton(2));
42 keyboard.add(new NumberButton(3));
43 keyboard.add(new NumberButton(0));
44
45 return (keyboard);
46 }
47
48 private void allClear(){
49 operator = Operator.ADD;
50 context = CONTEXT_OPERATOR;
51 register1 = 0;
52 register2 = 0;
53 display.setText("0");
54 }
55
56 /** Calculator class*/
57 private Calc(){
58 setSize(300, 300);
59 setLayout(new BorderLayout());
60 add(display = new Display(), BorderLayout.NORTH);
61 add(createKeyboard(), BorderLayout.CENTER);
62 allClear();
63 }
64
65 private static abstract class Operator{
66 abstract int apply(int left, int right);
67 abstract char symbol();
68
69 static final Operator ADD = new Operator(){
70 int apply(int left, int right){ return (left + right); }
71 char symbol() { return ('+'); }
72 };
73 static final Operator SUBTRACT = new Operator(){
74 int apply(int left, int right){ return (left - right); }
75 char symbol() { return ('-'); }
76 };
77 static final Operator MULTIPLY = new Operator(){
78 int apply(int left, int right){ return (left * right); }
79 char symbol() { return ('*'); }
80 };
81 static final Operator DIVIDE = new Operator(){
82 int apply(int left, int right){ return (left / right); }
83 char symbol() { return ('/'); }
84 };
85 }
86
87 private static abstract class Context{
88 abstract void inputNumber(int input_number);
89 abstract void inputOperator(Operator input_operator);
90 abstract void inputResult();
91 }
92
93 private final Context CONTEXT_NUMBER = new Context(){
94 void inputNumber(int input_number){
95 register1 = register1 * 10 + input_number;
96 display.setNumber(register1);
97 }
98 void inputOperator(Operator input_operator){
99 register2 = operator.apply(register2, register1);
100 display.setNumber(register2);
101 operator = input_operator;
102 }
103 void inputResult(){
104 register2 = operator.apply(register2, register1);
105 display.setNumber(register2);
106 }
107 };
108
109 private final Context CONTEXT_OPERATOR = new Context(){
110 void inputNumber(int input_number){
111 register1 = input_number;
112 display.setNumber(register1);
113 }
114 void inputOperator(Operator input_operator){
115 operator = input_operator;
116 }
117 void inputResult(){
118 }
119 };
120
121 private final Context CONTEXT_RESULT = new Context(){
122 void inputNumber(int input_number){
123 allClear();
124 register1 = input_number;
125 display.setNumber(register1);
126 }
127 void inputOperator(Operator input_operator){
128 operator = input_operator;
129 }
130 void inputResult(){
131 register2 = operator.apply(register2, register1);
132 display.setNumber(register2);
133 }
134 };
135
136 private static abstract class CalcButton extends Button implements ActionListener{
137 CalcButton(String label){
138 super(label);
139 addActionListener(this);
140 }
141 }
142
143 private class NumberButton extends CalcButton{
144 int number;
145
146 NumberButton(int this_number){
147 super(String.valueOf(this_number));
148 number = this_number;
149 }
150 public void actionPerformed(ActionEvent e){
151 context.inputNumber(number);
152 context = CONTEXT_NUMBER;
153 }
154 }
155
156 private class OperatorButton extends CalcButton{
157 Operator operator;
158
159 OperatorButton(Operator this_operator){
160 super(String.valueOf(this_operator.symbol()));
161 operator = this_operator;
162 }
163 public void actionPerformed(ActionEvent e){
164 context.inputOperator(operator);
165 context = CONTEXT_OPERATOR;
166 }
167 }
168
169 private class ResultButton extends CalcButton{
170 ResultButton(){
171 super("=");
172 }
173 public void actionPerformed(ActionEvent e){
174 context.inputResult();
175 context = CONTEXT_RESULT;
176 }
177 }
178
179 private class AllClearButton extends CalcButton{
180 AllClearButton(){
181 super("AC");
182 }
183 public void actionPerformed(ActionEvent e){
184 allClear();
185 }
186 }
187
188 public static void main(String[] args){
189 Calc c = new Calc();
190 try{
191 synchronized(c){
192 c.show();
193 c.wait();
194 }
195 }
196 catch(InterruptedException e){
197 System.out.println("abort");
198 }
199 }
200 }
2 import java.awt.event.*;
3
4 class Calc extends Frame{
5 private Display display;
6 private Operator operator;
7 private Context context;
8 private int register1;
9 private int register2;
10
11 private static class Display extends Label{
12 Display(){
13 setAlignment(Label.RIGHT);
14 }
15 void setNumber(int value){
16 setText(String.valueOf(value));
17 }
18 }
19
20 private Panel createKeyboard(){
21 Panel keyboard = new Panel();
22
23 keyboard.setLayout(new GridLayout(4, 4));
24
25 keyboard.add(new AllClearButton());
26 keyboard.add(new OperatorButton(Operator.DIVIDE));
27 keyboard.add(new OperatorButton(Operator.MULTIPLY));
28 keyboard.add(new OperatorButton(Operator.SUBTRACT));
29
30 keyboard.add(new NumberButton(7));
31 keyboard.add(new NumberButton(8));
32 keyboard.add(new NumberButton(9));
33 keyboard.add(new OperatorButton(Operator.ADD));
34
35 keyboard.add(new NumberButton(4));
36 keyboard.add(new NumberButton(5));
37 keyboard.add(new NumberButton(6));
38 keyboard.add(new ResultButton());
39
40 keyboard.add(new NumberButton(1));
41 keyboard.add(new NumberButton(2));
42 keyboard.add(new NumberButton(3));
43 keyboard.add(new NumberButton(0));
44
45 return (keyboard);
46 }
47
48 private void allClear(){
49 operator = Operator.ADD;
50 context = CONTEXT_OPERATOR;
51 register1 = 0;
52 register2 = 0;
53 display.setText("0");
54 }
55
56 /** Calculator class*/
57 private Calc(){
58 setSize(300, 300);
59 setLayout(new BorderLayout());
60 add(display = new Display(), BorderLayout.NORTH);
61 add(createKeyboard(), BorderLayout.CENTER);
62 allClear();
63 }
64
65 private static abstract class Operator{
66 abstract int apply(int left, int right);
67 abstract char symbol();
68
69 static final Operator ADD = new Operator(){
70 int apply(int left, int right){ return (left + right); }
71 char symbol() { return ('+'); }
72 };
73 static final Operator SUBTRACT = new Operator(){
74 int apply(int left, int right){ return (left - right); }
75 char symbol() { return ('-'); }
76 };
77 static final Operator MULTIPLY = new Operator(){
78 int apply(int left, int right){ return (left * right); }
79 char symbol() { return ('*'); }
80 };
81 static final Operator DIVIDE = new Operator(){
82 int apply(int left, int right){ return (left / right); }
83 char symbol() { return ('/'); }
84 };
85 }
86
87 private static abstract class Context{
88 abstract void inputNumber(int input_number);
89 abstract void inputOperator(Operator input_operator);
90 abstract void inputResult();
91 }
92
93 private final Context CONTEXT_NUMBER = new Context(){
94 void inputNumber(int input_number){
95 register1 = register1 * 10 + input_number;
96 display.setNumber(register1);
97 }
98 void inputOperator(Operator input_operator){
99 register2 = operator.apply(register2, register1);
100 display.setNumber(register2);
101 operator = input_operator;
102 }
103 void inputResult(){
104 register2 = operator.apply(register2, register1);
105 display.setNumber(register2);
106 }
107 };
108
109 private final Context CONTEXT_OPERATOR = new Context(){
110 void inputNumber(int input_number){
111 register1 = input_number;
112 display.setNumber(register1);
113 }
114 void inputOperator(Operator input_operator){
115 operator = input_operator;
116 }
117 void inputResult(){
118 }
119 };
120
121 private final Context CONTEXT_RESULT = new Context(){
122 void inputNumber(int input_number){
123 allClear();
124 register1 = input_number;
125 display.setNumber(register1);
126 }
127 void inputOperator(Operator input_operator){
128 operator = input_operator;
129 }
130 void inputResult(){
131 register2 = operator.apply(register2, register1);
132 display.setNumber(register2);
133 }
134 };
135
136 private static abstract class CalcButton extends Button implements ActionListener{
137 CalcButton(String label){
138 super(label);
139 addActionListener(this);
140 }
141 }
142
143 private class NumberButton extends CalcButton{
144 int number;
145
146 NumberButton(int this_number){
147 super(String.valueOf(this_number));
148 number = this_number;
149 }
150 public void actionPerformed(ActionEvent e){
151 context.inputNumber(number);
152 context = CONTEXT_NUMBER;
153 }
154 }
155
156 private class OperatorButton extends CalcButton{
157 Operator operator;
158
159 OperatorButton(Operator this_operator){
160 super(String.valueOf(this_operator.symbol()));
161 operator = this_operator;
162 }
163 public void actionPerformed(ActionEvent e){
164 context.inputOperator(operator);
165 context = CONTEXT_OPERATOR;
166 }
167 }
168
169 private class ResultButton extends CalcButton{
170 ResultButton(){
171 super("=");
172 }
173 public void actionPerformed(ActionEvent e){
174 context.inputResult();
175 context = CONTEXT_RESULT;
176 }
177 }
178
179 private class AllClearButton extends CalcButton{
180 AllClearButton(){
181 super("AC");
182 }
183 public void actionPerformed(ActionEvent e){
184 allClear();
185 }
186 }
187
188 public static void main(String[] args){
189 Calc c = new Calc();
190 try{
191 synchronized(c){
192 c.show();
193 c.wait();
194 }
195 }
196 catch(InterruptedException e){
197 System.out.println("abort");
198 }
199 }
200 }