-
Notifications
You must be signed in to change notification settings - Fork 0
/
Secondtry.java
304 lines (247 loc) · 8.01 KB
/
Secondtry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Calculator.java
/****
* This program implements a basic calculator
*
* @author Anna Ntenta
* @version 1.0 Last Modified 4/11/2014
*/
import javax.swing.*; // Swing components
import java.awt.*; // Colors, Fonts, etc.
import java.io.*; // File I/O
import java.awt.event.*; // ActionListener, etc.
import java.util.*;
import javax.swing.border.*;
public class Secondtry extends JFrame implements ActionListener
{
// number of digits (inclusive of decimal point) allowed in the display
public static final int CAPACITY = 15;
// private variables
private JFrame frame;
private JTextArea display;
// boolean set to true if number already has a decimal point
private Boolean decPoint = false;
// boolean set to true if an operator has been clicked
private Boolean operators;
// string representing addend, subtractor, multiplicator or numerator
private StringBuilder num1 = new StringBuilder();
// string representing addend, subtractor, multiplicator or denominator
private StringBuilder num2 = new StringBuilder();
// Double representing addend, subtractor, multiplicator or numerator
private Double firstNum = null;
// Double representing addend, subtractor, multiplicator or denominator
private Double secondNum = null;
// String representing an operator (+, -, *, /, or square root)
private String operator = null;
private Boolean oldOp = false;
// indicates whether the display should be cleared
private Boolean doClear;
// array of number buttons
private JButton [] jb = new JButton[10];
//operator buttons
private JButton clear, root, plus, minus, div, mult, equals, point;
/****
* Constructor. Sets up user interface and initializations
*/
public Secondtry()
{
operators = true;
doClear = false;
frame = new JFrame ("Calculator");
frame.setLayout( new BorderLayout( 10, 10 ));
display = new JTextArea();
display.setSize(245, 100);
display.setFont (new Font ("SansSerif", Font.BOLD, 28));
display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
// create the numeric buttons for calculator
for (int i = 0; i < 10; i++ )
{
jb[i] = new JButton ("" + (i));
jb[i].addActionListener(this);
jb[i].setFont (new Font ("SansSerif", Font.BOLD, 14));
}
// create the square root button
root = new JButton("\u221A");
root.addActionListener(this);
root.setFont (new Font ("SansSerif", Font.BOLD, 14));
// create the clear button
clear = new JButton("C");
clear.addActionListener(this);
clear.setFont (new Font ("SansSerif", Font.BOLD, 14));
// create the plus button
plus = new JButton("\u002B");
plus.addActionListener(this);
plus.setFont (new Font ("SansSerif", Font.BOLD, 14));
// create the minus button
minus = new JButton("\u002D");
minus.addActionListener(this);
minus.setFont (new Font ("SansSerif", Font.BOLD, 14));
// create the multiply button
mult = new JButton("\u002A");
mult.addActionListener(this);
mult.setFont (new Font ("SansSerif", Font.BOLD, 14));
// create the division button
div = new JButton("\u002F");
div.addActionListener(this);
div.setFont (new Font ("SansSerif", Font.BOLD, 14));
// create the equals button
equals = new JButton("\u003D");
equals.addActionListener(this);
equals.setFont (new Font ("SansSerif", Font.BOLD, 14));
equals.setPreferredSize(new Dimension(50, 60));
// create the point button
point = new JButton("\u002E");
point.addActionListener(this);
point.setFont (new Font ("SansSerif", Font.BOLD, 14));
// button grid
JPanel buttons = new JPanel(new GridLayout(5, 4, 4, 4));
// add buttons to grid
buttons.add(clear);
buttons.add(root);
buttons.add(div);
buttons.add(mult);
// add numeric buttons to grid
for (int i = 7; i < 10; i++)
buttons.add ( jb[i] );
buttons.add(minus);
// add numeric buttons to grid
for (int i = 4; i < 7; i++)
buttons.add ( jb[i] );
buttons.add(plus);
// add numeric buttons to grid
for (int i = 1; i < 4; i++)
buttons.add ( jb[i] );
buttons.add(equals);
buttons.add(jb[0]);
buttons.add(point);
frame.add (display, BorderLayout.CENTER);
frame.add (buttons, BorderLayout.SOUTH);
frame.setSize (300, 400);
frame.setVisible (true);
frame.setResizable(false);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed( ActionEvent ae )
{
// clear display if new number is to be entered
if(doClear)
display.setText(null);
// if decimal point is entered for the first time in number
if((ae.getSource() == point) && decPoint == false) {
display.append(".");
// disallow more decimal points
decPoint = true;
}
// if decimal point is the first in a number, display "0."
if (display.getText().equals("."))
display.setText("0.");
// check if a digit was clicked
for (int i = 0; i < 10; i++) {
// append digits to number while number is less than 15 digits long
if((ae.getSource() == jb[i]) && (display.getText().length() < 15)) {
display.append(((JButton)ae.getSource()).getText());
// disallow leading zeros
if (display.getText().equals("0"))
display.setText("0.");
// store as a Double
secondNum = Double.parseDouble(display.getText());
System.out.println("SecondNum is: " + secondNum);
doClear = false;
// only if you click another digit
oldOp = false;
}
}
// check if an operator was clicked
if((ae.getSource() == plus) || (ae.getSource() == minus) ||
(ae.getSource() == div) || (ae.getSource() == mult)) {
decPoint = false;
if(oldOp == false) {
// if this is the first time an operator was clicked
if (operators) {
// the number last entered becomes the first number
firstNum = secondNum;
System.out.println("firstNum is: " + secondNum);
}
else {
calc();
}
// store the operator that was clicked
JButton temp=(JButton)ae.getSource();
operator = temp.getText();
System.out.println("Operator is: " + operator);
operators = false;
doClear = true;
oldOp = true;
}
// if an operator was clicked without a digit having been entered between
else {
// store the operator that was clicked
JButton temp=(JButton)ae.getSource();
operator = temp.getText();
System.out.println("Operator is: " + operator);
display.setText(String.valueOf(firstNum));
}
}
if(ae.getSource() == root) {
squareRoot();
decPoint = false;
}
if(ae.getSource() == clear) {
clear();
}
if(ae.getSource() == equals) {
calc();
}
}
public void squareRoot() {
if (secondNum == 0.0) {
display.setText("ERROR");
firstNum = 0.0;
secondNum = 0.0;
operators = true;
doClear = true;
}
else {
secondNum = Math.sqrt(secondNum);
display.setText(String.valueOf(secondNum));
}
}
public void calc() {
if(operator== "+") {
firstNum += secondNum;
System.out.println("firstNum plus secondNum is: " + firstNum); }
if(operator == "-"){
firstNum -= secondNum;
System.out.println("firstNum minus secondNum is: " + firstNum);}
if(operator == "*") {
firstNum *= secondNum;
System.out.println("firstNum times secondNum is: " + firstNum);}
if((operator == "/") && secondNum != 0.0){
System.out.println("firstNum div by secondNum is: " + firstNum);
firstNum /= secondNum;}
if((operator == "/") && secondNum == 0.0){
display.setText("ERROR");
System.out.println(display.getText());
firstNum = 0.0;
secondNum = 0.0;
operators = true;
doClear = true;
}
if (!(display.getText().equals("ERROR"))) {
operators = true;
decPoint = false;
display.setText(String.valueOf(firstNum));
secondNum = firstNum;
System.out.println("SecondNum is: " + firstNum);
}
}
public void clear() {
firstNum = 0.0;
secondNum = 0.0;
operators = true;
display.setText(null);
}
public static void main(String args[])
{
Secondtry sec = new Secondtry ();
}
}