1 import java.applet.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.lang.Math;
5 import java.util.Arrays;
6
7 /**
8 * Applet to do basic set relations
9 * Creation date: (01/30/2002 8:35:19 AM)
10 * Modified date: (04/15/2002 2:55:00 PM)
11 * @author: Andrew Freed, arf132@psu.edu
12 */
13
14 /*
15 Development of this applet was sponsored by the Penn State Fund for
16 Excellence in Learning and Teaching (FELT), project "Java-based
17 Teaching of Mathematics in Information Sciences and Technology",
18 supervised by Frank Ritter and David Mudgett.
19 */
20
21 public class BasicSetApplet extends Applet {
22 private Label lblSizeOfA = new Label("Size of A:");
23 private Label lblSizeOfB = new Label("Size of B:");
24 private TextField txtSizeOfA = new TextField("10");
25 private TextField txtSizeOfB = new TextField("7");
26 private Button btnGo = new Button("Go!");
27 private Label lblHeaderA = new Label("A:");
28 private Label lblHeaderB = new Label("B:");
29 private Label lblSetA = new Label("Set A");
30 private Label lblSetB = new Label("Set B");
31 private Button btnAUnionB = new Button("A \\/ B");
32 private Button btnAIntersectB = new Button("A /\\ B");
33 private Button btnAMinusB = new Button("A - B");
34 private Button btnAComplement = new Button("Ac");
35 private Label lblAUnionB = new Label("A Union B");
36 private Label lblAIntersectB = new Label("A Intersect B");
37 private Label lblAMinusB = new Label("A Minus B");
38 private Label lblAComplement = new Label("A Complement");
39
40 private int setA[];
41 private int setB[];
42 private int AVAILABLE_INTS = 20; //Want to choose random numbers below this
43 private int sizeOfA=0;
44 private int sizeOfB=0;
45
46 /**
47 * Function that will get A intersected with B
48 * Assume sorted arrays!
49 */
50 private String getAIntersectB()
51 {
52 String result = "";
53
54 //If A is empty, we won't do any work (intersection is empty)
55 //Otherwise, iterate through both arrays, and add common elements
56 //to the intersection string
57 for( int idxA = 0; idxA < sizeOfA; idxA++ )
58 {
59 boolean found = false;
60 for( int idxB = 0; idxB < sizeOfB && setA[idxA] >= setB[idxB]; idxB++)
61 {
62 if( setA[idxA] == setB[idxB] )
63 found = true;
64 }
65 if( found )
66 result += ( " " + setA[idxA] + " " );
67
68 found = false;
69 }
70
71 lblAIntersectB.setText(result);
72 return result;
73 }
74
75 /**
76 * Function that will get A unioned with B
77 * Assume sorted sets A, B
78 */
79 private String getAUnionB()
80 {
81 String result = "";
82 int idxA = 0;
83 int idxB = 0;
84
85 //If either set is empty, the result is just the other set.
86 //If both are empty there the union is empty
87 boolean shouldLoop = true;
88 if( sizeOfA == 0 || sizeOfB == 0 )
89 {
90 shouldLoop = false;
91 if( sizeOfA == 0 && sizeOfB == 0 )
92 result = "";
93 else if( sizeOfA == 0 )
94 result = aryToString(setB, sizeOfB);
95 else // sizeOfB == 0
96 result = aryToString(setA, sizeOfA);
97 }
98
99 //Iterate through both arrays, and add every value that is in either set
100 //Note the use of comparisons to make sure that the union array is also
101 //in sorted order
102 while( idxA < sizeOfA && idxB < sizeOfB && shouldLoop)
103 {
104 if( setA[idxA] == setB[idxB] )
105 {
106 result += (" " + setA[idxA] + " ");
107 idxA++;
108 idxB++;
109 }
110 else if( setA[idxA] < setB[idxB] )
111 {
112 result += (" " + setA[idxA] + " ");
113 idxA++;
114 //If done with A's then append rest of B's
115 if( idxA == sizeOfA )
116 {
117 while( idxB < sizeOfB )
118 {
119 result += (" " + setB[idxB] + " ");
120 idxB++;
121 }
122 }
123 }
124 else // setA[idxA] > setB[idxB]
125 {
126 result += (" " + setB[idxB] + " ");
127 idxB++;
128 //If done with B's then append rest of A's
129 if( idxB == sizeOfB )
130 {
131 while( idxA < sizeOfA )
132 {
133 result += (" " + setA[idxA] + " ");
134 idxA++;
135 }
136 }
137 }
138 }
139 lblAUnionB.setText(result);
140 return result;
141 }
142
143 /**
144 * Function that will get A minus B
145 * Assume sorted sets A, B
146 */
147 private String getAMinusB()
148 {
149 String result = new String();
150
151 //For every element in A, see if it is in B. If it is not, then it is in A-B
152 for( int idxA = 0; idxA < sizeOfA; idxA++ )
153 {
154 if( notInArray(setB, sizeOfB, setA[idxA]) )
155 result += (" " + setA[idxA] + " ");
156 }
157 lblAMinusB.setText(result);
158 return result;
159 }
160
161 /**
162 * Function that will get A complement
163 */
164 private String getAComplement()
165 {
166 String result = new String();
167 if( sizeOfA > 0)
168 {
169 //Cycle through all the elements of U. Any element not in A is in Ac
170 for( int idx = 1; idx <= AVAILABLE_INTS; idx++ )
171 {
172 if( notInArray( setA, sizeOfA, idx ) )
173 result += (" " + idx + " ");
174 }
175 }
176 else
177 {
178 // Ac is all of U
179 result = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ";
180 }
181 lblAComplement.setText(result);
182 return result;
183 }
184
185 /**
186 * Function that creates random sets of A, B
187 */
188 private void createABSets()
189 {
190 try
191 {
192 //Text box must contain data
193 if( ( txtSizeOfA.getText() == null) || ( txtSizeOfB.getText() == null) )
194 {
195 lblSetA.setText("Error: Please a valid set size, less than " + AVAILABLE_INTS);
196 lblSetB.setText("Error: Please a valid set size, less than " + AVAILABLE_INTS);
197 sizeOfA = 0;
198 sizeOfB = 0;
199 resetAnswerLabels();
200 disableSetTheory();
201 return;
202 }
203
204 sizeOfA = Integer.parseInt(txtSizeOfA.getText());
205 sizeOfB = Integer.parseInt(txtSizeOfB.getText());
206
207 //Size of each set must be less than size of U
208 if( sizeOfA > AVAILABLE_INTS || sizeOfB > AVAILABLE_INTS )
209 {
210 //Fix labels
211 lblSetA.setText("Set A");
212 lblSetB.setText("Set B");
213 if (sizeOfA > AVAILABLE_INTS)
214 lblSetA.setText("Please choose a smaller set size");
215 if (sizeOfB > AVAILABLE_INTS)
216 lblSetB.setText("Please choose a smaller set size");
217
218 sizeOfA = 0;
219 sizeOfB = 0;
220 resetAnswerLabels();
221 disableSetTheory();
222 return;
223 }
224
225 //Negative set size not allowed
226 if( sizeOfA < 0 || sizeOfB < 0 )
227 {
228 lblSetA.setText("Set A");
229 lblSetB.setText("Set B");
230 if( sizeOfA < 0 )
231 lblSetA.setText("Please choose a valid set size");
232 if( sizeOfB < 0 )
233 lblSetB.setText("Please choose a valid set size");
234 sizeOfA = 0;
235 sizeOfB = 0;
236 resetAnswerLabels();
237 disableSetTheory();
238 return;
239 }
240
241 //Otherwise valid set size. Since I can't create an array of size 0,
242 //I create a dummy array, but report the size as 0
243 if( sizeOfA != 0)
244 setA = new int[sizeOfA];
245 else
246 {
247 setA = new int[1];
248 setA[0]=0;
249 }
250
251 if( sizeOfB != 0)
252 setB = new int[sizeOfB];
253 else
254 {
255 setB = new int[1];
256 setB[0]=0;
257 }
258
259 //Gather random numbers between 1 and AVAILABLE_INTS for each array.
260 //Make sure that the elements are unique
261 int idxA=0;
262 int myRand;
263 while( idxA < sizeOfA )
264 {
265 myRand = (int) ( AVAILABLE_INTS * java.lang.Math.random() + 1);
266 if( notInArray( setA, idxA, myRand ) )
267 setA[idxA++] = myRand;
268 }
269
270 int idxB=0;
271 while( idxB < sizeOfB )
272 {
273 myRand = (int) ( AVAILABLE_INTS * java.lang.Math.random() + 1);
274 if( notInArray( setB, idxB, myRand ) )
275 setB[idxB++] = myRand;
276 }
277
278 //Sort these arrays
279 sort(setA);
280 sort(setB);
281
282 //Print them for the user
283 lblSetA.setText( aryToString(setA, sizeOfA) );
284 lblSetB.setText( aryToString(setB, sizeOfB) );
285
286 resetAnswerLabels();
287 enableSetTheory();
288 }
289
290 //This is to keep the applet from "blowing up" on invalid textbox entry formats
291 catch(java.lang.NumberFormatException nfe)
292 {
293 lblSetA.setText("Error: Please a valid set size, less than " + AVAILABLE_INTS);
294 lblSetB.setText("Error: Please a valid set size, less than " + AVAILABLE_INTS);
295 txtSizeOfA.setText("");
296 txtSizeOfB.setText("");
297 sizeOfA = 0;
298 sizeOfB = 0;
299
300 resetAnswerLabels();
301 disableSetTheory();
302 }
303 }
304
305 private void resetAnswerLabels()
306 {
307 lblAUnionB.setText("A Union B");
308 lblAIntersectB.setText("A Intersect B");
309 lblAMinusB.setText("A Minus B");
310 lblAComplement.setText("A Complement");
311 }
312
313 private void disableSetTheory()
314 {
315 btnAUnionB.setEnabled(false);
316 btnAIntersectB.setEnabled(false);
317 btnAMinusB.setEnabled(false);
318 btnAComplement.setEnabled(false);
319 }
320
321 private void enableSetTheory()
322 {
323 btnAUnionB.setEnabled(true);
324 btnAIntersectB.setEnabled(true);
325 btnAMinusB.setEnabled(true);
326 btnAComplement.setEnabled(true);
327 }
328
329 private boolean notInArray( int[] ary, int pos, int value)
330 {
331 boolean result = true;
332 for(int idx=0; idx < pos && result; idx++)
333 {
334 if( ary[idx] == value )
335 result = false;
336 }
337 return result;
338 }
339
340 private String aryToString( int[] ary, int size )
341 {
342 String result = "";
343 if( size != 0 )
344 {
345 for( int idx=0; idx < size -1; idx++)
346 result += (ary[idx] + " ");
347
348 result += ary[ary.length -1];
349 }
350
351 return result;
352 }
353
354 //Simple selection sort
355 private void sort( int ary[] )
356 {
357 int size = ary.length;
358 int temp;
359 int smallVal;
360 int smallPos;
361 for( int i=0; i < size; i++ )
362 {
363 smallVal = ary[i];
364 smallPos = i;
365 for( int j=i; j < size; j++ )
366 {
367 if( ary[j] < smallVal )
368 {
369 smallVal = ary[j];
370 smallPos = j;
371 }
372 }
373 temp = ary[i];
374 ary[i] = smallVal;
375 ary[smallPos] = temp;
376 }
377 }
378
379
380 /**
381 * Initializes the applet.
382 */
383 public void init() {
384 try {
385 super.init();
386 setName("BasicSetApplet");
387 setLayout(null);
388 setSize(500, 350);
389
390 int baseX=10;
391 int baseY=40;
392 int xStep=55;
393 int yStep=30;
394
395 lblSizeOfA.setBounds(baseX, baseY, 50, 30);
396 lblSizeOfB.setBounds(baseX + 2*xStep, baseY, 50, 30);
397 txtSizeOfA.setBounds(baseX + xStep, baseY, 30, 30);
398 txtSizeOfB.setBounds(baseX + 3*xStep, baseY, 30, 30);
399 btnGo.setBounds(250, baseY, 40, 30);
400 lblHeaderA.setBounds(baseX + xStep, baseY + 3*yStep, 30, 30);
401 lblHeaderB.setBounds(baseX + xStep, baseY + 4*yStep, 30, 30);
402 lblSetA.setBounds(baseX + 2*xStep, baseY + 3*yStep, 300, 30);
403 lblSetB.setBounds(baseX + 2*xStep, baseY + 4*yStep, 300, 30);
404 btnAUnionB.setBounds(baseX + xStep, baseY + 5*yStep, 50, 30);
405 btnAIntersectB.setBounds(baseX + xStep, baseY + 6*yStep, 50, 30);
406 btnAMinusB.setBounds(baseX + xStep, baseY + 7*yStep, 50, 30);
407 btnAComplement.setBounds(baseX + xStep, baseY + 8*yStep, 50, 30);
408 lblAUnionB.setBounds(baseX + 2*xStep, baseY + 5*yStep, 350, 30);
409 lblAIntersectB.setBounds(baseX + 2*xStep, baseY + 6*yStep, 350, 30);
410 lblAMinusB.setBounds(baseX + 2*xStep, baseY + 7*yStep, 350, 30);
411 lblAComplement.setBounds(baseX + 2*xStep, baseY + 8*yStep, 350, 30);
412
413 btnGo.setActionCommand("btnGo");
414 btnGo.addActionListener(new java.awt.event.ActionListener(){
415 public void actionPerformed(java.awt.event.ActionEvent e)
416 {
417 createABSets();
418 btnAUnionB.requestFocus();
419 }
420 });
421 btnAIntersectB.setActionCommand("AIntersectB");
422 btnAIntersectB.addActionListener(new java.awt.event.ActionListener(){
423 public void actionPerformed(java.awt.event.ActionEvent e)
424 {
425 getAIntersectB();
426 btnAMinusB.requestFocus();
427 }
428 });
429
430 btnAUnionB.setActionCommand("AUnionB");
431 btnAUnionB.addActionListener(new java.awt.event.ActionListener(){
432 public void actionPerformed(java.awt.event.ActionEvent e)
433 {
434 getAUnionB();
435 btnAIntersectB.requestFocus();
436 }
437 });
438
439 btnAComplement.setActionCommand("AComplement");
440 btnAComplement.addActionListener(new java.awt.event.ActionListener(){
441 public void actionPerformed(java.awt.event.ActionEvent e)
442 {
443 getAComplement();
444 btnGo.requestFocus();
445 }
446 });
447
448 btnAMinusB.setActionCommand("AMinusB");
449 btnAMinusB.addActionListener(new java.awt.event.ActionListener(){
450 public void actionPerformed(java.awt.event.ActionEvent e)
451 {
452 getAMinusB();
453 btnAComplement.requestFocus();
454 }
455 });
456
457 txtSizeOfA.addActionListener(new java.awt.event.ActionListener(){
458 public void actionPerformed(java.awt.event.ActionEvent e)
459 {
460 createABSets();
461 btnAUnionB.requestFocus();
462 }
463 });
464 txtSizeOfB.addActionListener(new java.awt.event.ActionListener(){
465 public void actionPerformed(java.awt.event.ActionEvent e)
466 {
467 createABSets();
468 btnAUnionB.requestFocus();
469 }
470 });
471 add(lblSizeOfA);
472 add(lblSizeOfB);
473 add(txtSizeOfA);
474 add(txtSizeOfB);
475 add(btnGo);
476 add(lblHeaderA);
477 add(lblHeaderB);
478 add(lblSetA);
479 add(lblSetB);
480 add(btnAUnionB);
481 add(btnAIntersectB);
482 add(btnAMinusB);
483 add(btnAComplement);
484 add(lblAUnionB);
485 add(lblAIntersectB);
486 add(lblAMinusB);
487 add(lblAComplement);
488
489 disableSetTheory();
490 btnGo.requestFocus();
491 } catch (java.lang.Throwable Exc) {
492 handleException(Exc);
493 }
494 }
495
496 /**
497 * Called whenever the part throws an exception.
498 * @param exception java.lang.Throwable
499 */
500 private void handleException(java.lang.Throwable exception) {
501 //Currently exceptions are not handled since the applet does not have
502 //stdout or stderr available to it.
503 }
504
505 //main allows this applet to be run as a standalone application
506 /**
507 * main entrypoint - starts the part when it is run as an application
508 * @param args java.lang.String[]
509 */
510 public static void main(java.lang.String[] args) {
511 try {
512 Frame frame = new java.awt.Frame();
513 BasicSetApplet aBasicSetApplet;
514 Class iiCls = Class.forName("BasicSetApplet");
515 ClassLoader iiClsLoader = iiCls.getClassLoader();
516 aBasicSetApplet = (BasicSetApplet)java.beans.Beans.instantiate(iiClsLoader,"BasicSetApplet");
517 frame.add("Center", aBasicSetApplet);
518 frame.setSize(aBasicSetApplet.getSize());
519 frame.addWindowListener(new java.awt.event.WindowAdapter() {
520 public void windowClosing(java.awt.event.WindowEvent e) {
521 System.exit(0);
522 };
523 });
524 frame.show();
525 java.awt.Insets insets = frame.getInsets();
526 frame.setSize(frame.getWidth() + insets.left + insets.right, frame.getHeight() + insets.top + insets.bottom);
527 frame.setVisible(true);
528 } catch (Throwable exception) {
529 System.err.println("Exception occurred in main() of java.applet.Applet");
530 exception.printStackTrace(System.out);
531 }
532 }
533
534 }
535