-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCalculatorWithGUI.java
138 lines (118 loc) · 3.5 KB
/
CalculatorWithGUI.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static java.lang.Double.parseDouble;
class CalculatorWithGUI extends Frame implements ActionListener {
JTextField textInput;
JPanel panel;
String[] btnString = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "C", "0", "=", "÷", "^", "√", "%", "\u232b"};
int n = 20; //number of buttons
JButton[] btn = new JButton[n];
double num1 = 0;
double num2 = 0;
String result = " ";
char charSymbol;
public CalculatorWithGUI() {
Font f = new Font("MONOSPACED", Font.BOLD, 18);
textInput = new JTextField(10);
textInput.setFont(f);
panel = new JPanel();
add(textInput, "North");
add(panel, "Center");
panel.setLayout(new GridLayout(5, 4));
for (int i = 0; i < n; i++) {
btn[i] = new JButton(btnString[i]);
btn[i].setFont(f);
btn[i].addActionListener(this);
panel.add(btn[i]);
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
switch (str) {
case "+" -> {
charSymbol = '+';
num1 = parseDouble(textInput.getText());
textInput.setText("");
}
case "-" -> {
charSymbol = '-';
num1 = parseDouble(textInput.getText());
textInput.setText("");
}
case "*" -> {
charSymbol = '*';
num1 = parseDouble(textInput.getText());
textInput.setText("");
}
case "÷" -> {
charSymbol = '÷';
num1 = parseDouble(textInput.getText());
textInput.setText("");
}
case "^"-> {
charSymbol = '^';
num1 = parseDouble(textInput.getText());
textInput.setText("");
}
case "√"-> {
charSymbol = '√';
textInput.setText("");
}
case "%" -> {
charSymbol = '%';
num1 = parseDouble(textInput.getText());
textInput.setText("");
}
case "\u232b" -> {
String theText = textInput.getText();
if (theText.length() == 0) {
result = "";
} else {
result = theText.substring(0, theText.length()-1);
}
textInput.setText(result + "");
result = " ";
}
case "=" -> {
num2 = parseDouble(textInput.getText());
switch (charSymbol) {
case '+' -> result = String.valueOf(num1 + num2);
case '-' -> result = String.valueOf(num1 - num2);
case '*' -> result = String.valueOf(num1 * num2);
case '÷' -> result = String.valueOf(num1 / num2);
case '^' -> result = Double.toString(Math.pow(num1, num2));
case '√' -> result = Double.toString(Math.sqrt(num2));
case '%' -> {
if(num2 == 0){
result ="DIVISOR IS 0";
}
else {
result = String.valueOf(Math.floorMod((long) num1, (long) num2));
}
}
}
textInput.setText(result + "");
result = " ";
}
case "C" -> {
textInput.setText("");
num1 = 0;
}
default -> textInput.setText(textInput.getText() + str);
}
}
public static void main(String[] args) {
CalculatorWithGUI m = new CalculatorWithGUI();
m.setTitle("Calculator using Java (AWT)");
m.setSize(250, 300);
m.setBackground(Color.CYAN);
m.setForeground(Color.DARK_GRAY);
m.setVisible(true);
}
}