-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
230 lines (206 loc) · 8.88 KB
/
App.java
File metadata and controls
230 lines (206 loc) · 8.88 KB
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
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
public class App {
public static void main(String[] args) {
JFrame window = new JFrame("App");
window.setSize(400, 400);
// panel
App app = new App();
ShowEquationType showEquationType = app.new ShowEquationType();
window.add(showEquationType);
ShowEquation showEquation = app.new ShowEquation();
showEquation.setEquation(showEquationType.equationType);
showEquation.equationLabel.setText(showEquation.equation);
window.add(showEquation);
ShowNotification showNotification = app.new ShowNotification();
window.add(showNotification);
ShowButtons showButtons = app.new ShowButtons();
showButtons.buttonActions(showEquationType, showEquation, showNotification, showButtons);
window.add(showButtons);
// update
window.getContentPane().setLayout(new GridLayout(4, 1));
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
// show equation label
public class ShowEquationType extends JPanel {
private String equationType;
private JLabel label;
public ShowEquationType() {
label = new JLabel("");
equationType = getEquationType();
label.setText(equationType);
add(label);
}
public String getEquationType() {
Random rand = new Random();
java.util.List<String> givenList = Arrays.asList("Addition", "Subtraction", "Multiplication", "Division");
int randomIndex = rand.nextInt(givenList.size());
equationType = givenList.get(randomIndex);
return equationType;
}
}
// show equation
public class ShowEquation extends JPanel {
private String equation;
private JLabel equationLabel;
private JTextField texbox;
public ShowEquation() {
equationLabel = new JLabel("");
add(equationLabel);
texbox = new JTextField(10);
add(texbox);
}
public Number[] createEquation() {
Number[] numberList = new Number[2];
Random rand = new Random();
if (rand.nextInt(2) == 1) {
double numberA = Math.round((rand.nextDouble() + rand.nextInt(-100, 100)) * 100.0) / 100.0;
double numberB = Math.round((rand.nextDouble() + rand.nextInt(-100, 100)) * 100.0) / 100.0;
numberList[0] = numberA;
numberList[1] = numberB;
} else {
float numberA = rand.nextFloat() * 200 - 100;
float numberB = rand.nextFloat() * 200 - 100;
numberList[0] = numberA;
numberList[1] = numberB;
}
return numberList;
}
public String setEquation(String equationType) {
Number[] numbers = createEquation();
if (equationType.equalsIgnoreCase("Addition")) {
equation = numbers[0] + " + " + numbers[1] + " = ";
} else if (equationType.equalsIgnoreCase("Subtraction")) {
equation = numbers[0] + " - " + numbers[1] + " = ";
} else if (equationType.equalsIgnoreCase("Multiplication")) {
equation = numbers[0] + " * " + numbers[1] + " = ";
} else if (equationType.equalsIgnoreCase("Division")) {
equation = numbers[0] + " / " + numbers[1] + " = ";
}
return equation;
}
}
// show buttons
public class ShowButtons extends JPanel {
private JButton submit;
private JButton skip;
private AtomicBoolean condition;
public ShowButtons() {
skip = new JButton("Skip");
add(skip);
submit = new JButton("Submit");
add(submit);
}
public Boolean buttonActions(ShowEquationType showEquationType, ShowEquation showEquation,
ShowNotification showNotification, ShowButtons showButtons) {
condition = new AtomicBoolean(false); // Use AtomicBoolean
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = showEquation.texbox.getText();
if (!text.isEmpty()) {
double num = Double.parseDouble(text);
String str1 = Double.toString(showNotification.evaluateEquation(showEquation.equation));
double result = showNotification.evaluateEquation(showEquation.equation);
showNotification.notificationLabel.setText(str1);
showNotification.compare(num, showNotification.evaluateEquation(showEquation.equation));
boolean value = showNotification.areSimilar(num, result, 0.001);
if (value) {
resetComponents(showEquationType, showEquation, showNotification, showButtons);
}
}
}
});
skip.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
resetComponents(showEquationType, showEquation, showNotification, showButtons);
}
});
return condition.get();
}
}
// notification item
public class ShowNotification extends JPanel {
private JLabel notificationLabel;
private java.util.List<Map.Entry<Double, Boolean>> dataList;
private JLabel notificationLabel_1;
public ShowNotification() {
notificationLabel = new JLabel("");
add(notificationLabel);
notificationLabel_1 = new JLabel("");
add(notificationLabel_1);
}
public double evaluateEquation(String equation) {
equation = equation.trim();
String[] parts = equation.split(" ");
// Extract the numbers and operator
double numberA = Double.parseDouble(parts[0]);
String operator = parts[1];
double numberB = Double.parseDouble(parts[2]);
// Evaluate the expression based on the operator
double result = 0.0;
switch (operator) {
case "+":
result = numberA + numberB;
break;
case "-":
result = numberA - numberB;
break;
case "*":
result = numberA * numberB;
break;
case "/":
// Check for division by zero
if (numberB != 0) {
result = numberA / numberB;
} else {
System.out.println("Error: Division by zero");
}
break;
default:
System.out.println("Error: Invalid operator");
}
return Math.round(result);
}
public static boolean areSimilar(double num1, double num2, double threshold) {
double difference = Math.abs(num1 - num2);
return difference <= threshold;
}
public void compare(double num1, double num2) {
dataList = new ArrayList<>();
dataList.add(Map.entry(num1, areSimilar(num1, num2, 0.001)));
notificationLabel_1.setText(dataList.toString());
}
}
// reset
public void resetComponents(ShowEquationType showEquationType, ShowEquation showEquation,
ShowNotification showNotification, ShowButtons showButtons) {
// Reset ShowEquationType
String item = showEquationType.getEquationType();
showEquationType.label.setText(item);
showEquationType.revalidate();
showEquationType.repaint();
// Reset ShowEquation
showEquation.setEquation(item);
showEquation.equationLabel.setText(showEquation.equation);
showEquation.revalidate();
showEquation.repaint();
// Reset ShowNotification
showNotification.notificationLabel.setText("");
showNotification.notificationLabel_1.setText("");
showNotification.revalidate();
showNotification.repaint();
// Reset ShowButtons
showButtons.revalidate();
showButtons.repaint();
}
}