-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicIaD.java
More file actions
255 lines (203 loc) · 9.39 KB
/
Copy pathBasicIaD.java
File metadata and controls
255 lines (203 loc) · 9.39 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
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
public class BasicIaD extends JFrame {
private JTextField arrayField;
private JTextArea stepsArea;
private int[] array;
public BasicIaD() {
setTitle("Basic Insertion and Deletion");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.setBackground(Color.WHITE);
JLabel arrayLabel = new JLabel("Array:");
arrayField = new JTextField(20);
JButton okButton = new JButton("OK");
okButton.setBackground(Color.GRAY);
okButton.addActionListener(new OKButtonListener());
JButton insertionButton = new JButton("Insertion");
insertionButton.setBackground(Color.GREEN);
insertionButton.addActionListener(new InsertionButtonListener());
JButton deletionButton = new JButton("Deletion");
deletionButton.setBackground(Color.RED);
deletionButton.addActionListener(new DeletionButtonListener());
JButton clearButton = new JButton("Clear");
clearButton.setBackground(Color.LIGHT_GRAY);
clearButton.addActionListener(new ClearButtonListener());
stepsArea = new JTextArea(15, 40);
stepsArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(stepsArea);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(arrayLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(arrayField, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
panel.add(okButton, gbc);
gbc.gridx = 3;
gbc.gridy = 0;
panel.add(insertionButton, gbc);
gbc.gridx = 4;
gbc.gridy = 0;
panel.add(deletionButton, gbc);
gbc.gridx = 5;
gbc.gridy = 0;
panel.add(clearButton, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 6;
gbc.anchor = GridBagConstraints.CENTER;
panel.add(scrollPane, gbc);
setContentPane(panel);
pack();
}
private class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String arrayText = arrayField.getText().trim();
if (arrayText.isEmpty()) {
JOptionPane.showMessageDialog(BasicIaD.this, "Please enter an array.",
"Invalid Input", JOptionPane.WARNING_MESSAGE);
return;
}
String[] values = arrayText.split("\\s+");
array = new int[values.length];
try {
for (int i = 0; i < values.length; i++) {
array[i] = Integer.parseInt(values[i]);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(BasicIaD.this, "Invalid array format. Please enter integers separated by spaces.",
"Invalid Input", JOptionPane.WARNING_MESSAGE);
return;
}
updateStepsArea(true);
}
}
private class InsertionButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (array == null || array.length == 0) {
JOptionPane.showMessageDialog(BasicIaD.this, "Please enter an array first.",
"Invalid Input", JOptionPane.WARNING_MESSAGE);
return;
}
String elementText = JOptionPane.showInputDialog(BasicIaD.this, "Enter the element for insertion:", "Insertion Element", JOptionPane.PLAIN_MESSAGE);
if (elementText == null) {
return;
}
try {
int element = Integer.parseInt(elementText);
String indexText = JOptionPane.showInputDialog(BasicIaD.this, "Enter the index for insertion:", "Insertion Index", JOptionPane.PLAIN_MESSAGE);
if (indexText == null) {
return;
}
int index = Integer.parseInt(indexText);
if (index < 0 || index > array.length) {
JOptionPane.showMessageDialog(BasicIaD.this, "Invalid index. Please enter a valid index.",
"Invalid Input", JOptionPane.WARNING_MESSAGE);
return;
}
int[] newArray = new int[array.length + 1];
System.arraycopy(array, 0, newArray, 0, index);
newArray[index] = element;
System.arraycopy(array, index, newArray, index + 1, array.length - index);
array = newArray;
updateStepsArea(true);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(BasicIaD.this, "Invalid value. Please enter a valid integer.",
"Invalid Input", JOptionPane.WARNING_MESSAGE);
}
}
}
private class DeletionButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (array == null || array.length == 0) {
JOptionPane.showMessageDialog(BasicIaD.this, "Please enter an array first.",
"Invalid Input", JOptionPane.WARNING_MESSAGE);
return;
}
String elementText = JOptionPane.showInputDialog(BasicIaD.this, "Enter the element for deletion:", "Deletion Element", JOptionPane.PLAIN_MESSAGE);
if (elementText == null) {
return;
}
try {
int element = Integer.parseInt(elementText);
int[] originalArray = Arrays.copyOf(array, array.length);
StringBuilder sb = new StringBuilder();
sb.append("Array: ").append(Arrays.toString(array)).append("\n");
sb.append("Steps:\n");
sb.append("- Deletion:\n");
int index = -1;
boolean found = false;
for (int i = 0; i < array.length; i++) {
sb.append(" - Step ").append(i + 1).append(": Comparing ").append(array[i]).append(" with ").append(element).append("\n");
if (array[i] == element) {
found = true;
index = i;
break;
}
}
if (!found) {
sb.append(" - Element not found in the array.\n");
stepsArea.setText(sb.toString());
return;
}
sb.append(" - Element found at index ").append(index).append(".\n");
sb.append(" - Step ").append(array.length + 1).append(": Delete ").append(array[index]).append(" at index ").append(index).append("\n");
int[] newArray = new int[array.length - 1];
System.arraycopy(array, 0, newArray, 0, index);
System.arraycopy(array, index + 1, newArray, index, array.length - index - 1);
array = newArray;
sb.append("Updated Array: ").append(Arrays.toString(array));
stepsArea.setText(sb.toString());
JOptionPane.showMessageDialog(BasicIaD.this, "Array after deletion: " + Arrays.toString(array),
"Deletion Result", JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(BasicIaD.this, "Invalid value. Please enter a valid integer.",
"Invalid Input", JOptionPane.WARNING_MESSAGE);
}
}
}
private class ClearButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
arrayField.setText("");
stepsArea.setText("");
}
}
private void updateStepsArea(boolean isInsertion) {
StringBuilder sb = new StringBuilder();
sb.append("Array: ").append(Arrays.toString(array)).append("\n");
sb.append("Steps:\n");
if (isInsertion) {
// Insertion steps
sb.append("- Insertion:\n");
for (int i = 0; i < array.length; i++) {
sb.append(" - Step ").append(i + 1).append(": Insert ").append(array[i]).append(" at index ").append(i).append("\n");
}
} else {
// Deletion steps
sb.append("- Deletion:\n");
for (int i = array.length - 1; i >= 0; i--) {
sb.append(" - Step ").append(i + 1).append(": Delete ").append(array[i]).append(" at index ").append(i).append("\n");
}
}
stepsArea.setText(sb.toString());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
BasicIaD basicIaD = new BasicIaD();
basicIaD.setVisible(true);
}
});
}
}