forked from ZhiqingXiao/java-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appendixC.java
1150 lines (900 loc) · 25.5 KB
/
appendixC.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Compute your weight on the moon.
Call this file Moon.java.
*/
class Moon {
public static void main(String[] args) {
double earthWeight; // weight on earth
double moonWeight; // weight on moon
earthWeight = 165;
moonWeight = earthWeight * 0.17;
System.out.println(earthWeight +
" earth-pounds is equivalent to " +
moonWeight + " moon-pounds.");
}
}
// -----------------------------------------
/*
This program displays a conversion
table of inches to meters.
Call this program InchToMeterTable.java.
*/
class InchToMeterTable {
public static void main(String[] args) {
double inches, meters;
int counter;
counter = 0;
for(inches = 1; inches <= 144; inches++) {
meters = inches / 39.37; // convert to meters
System.out.println(inches + " inches is " +
meters + " meters.");
counter++;
// every 12th line, print a blank line
if(counter == 12) {
System.out.println();
counter = 0; // reset the line counter
}
}
}
}
// -----------------------------------------
// Find prime numbers between 2 and 100.
class Prime {
public static void main(String[] args) {
int i, j;
boolean isprime;
for(i=2; i < 100; i++) {
isprime = true;
// see if the number is evenly divisible
for(j=2; j <= i/j; j++)
// if it is, then it's not prime
if((i%j) == 0) isprime = false;
if(isprime)
System.out.println(i + " is prime.");
}
}
}
// -----------------------------------------
// Count spaces.
class Spaces {
public static void main(String[] args)
throws java.io.IOException {
char ch;
int spaces = 0;
System.out.println("Enter a period to stop.");
do {
ch = (char) System.in.read();
if(ch == ' ') spaces++;
} while(ch != '.');
System.out.println("Spaces: " + spaces);
}
}
// -----------------------------------------
/* Use a for loop to generate the progression
1 2 4 8 16 32 64
*/
class Progress {
public static void main(String[] args) {
for(int i = 1; i < 100; i += i)
System.out.print(i + " ");
}
}
// -----------------------------------------
// Change case.
class CaseChg {
public static void main(String[] args)
throws java.io.IOException {
char ch;
int changes = 0;
System.out.println("Enter period to stop.");
do {
ch = (char) System.in.read();
if(ch >= 'a' & ch <= 'z') {
ch -= 32;
changes++;
System.out.println(ch);
}
else if(ch >= 'A' & ch <= 'Z') {
ch += 32;
changes++;
System.out.println(ch);
}
} while(ch != '.');
System.out.println("Case changes: " + changes);
}
}
// -----------------------------------------
// Average 10 double values.
class Avg {
public static void main(String[] args) {
double[] nums = { 1.1, 2.2, 3.3, 4.4, 5.5,
6.6, 7.7, 8.8, 9.9, 10.1 };
double sum = 0;
for(int i=0; i < nums.length; i++)
sum += nums[i];
System.out.println("Average: " + sum / nums.length);
}
}
// -----------------------------------------
// Demonstrate the Bubble sort with strings.
class StrBubble {
public static void main(String[] args) {
String[] strs = {
"this", "is", "a", "test",
"of", "a", "string", "sort"
};
int a, b;
String t;
int size;
size = strs.length; // number of elements to sort
// display original array
System.out.print("Original array is:");
for(int i=0; i < size; i++)
System.out.print(" " + strs[i]);
System.out.println();
// This is the bubble sort for strings.
for(a=1; a < size; a++)
for(b=size-1; b >= a; b--) {
if(strs[b-1].compareTo(strs[b]) > 0) { // if out of order
// exchange elements
t = strs[b-1];
strs[b-1] = strs[b];
strs[b] = t;
}
}
// display sorted array
System.out.print("Sorted array is:");
for(int i=0; i < size; i++)
System.out.print(" " + strs[i]);
System.out.println();
}
}
// -----------------------------------------
// An improved XOR cipher.
class SimpleCipher2 {
public static void main(String[] args) {
String msg = "This is a test";
String encMsg = "";
String decMsg = "";
String key = "abcdefgi";
int j;
System.out.print("Original message: ");
System.out.println(msg);
// encode the message
j = 0;
for(int i=0; i < msg.length(); i++) {
encMsg = encMsg + (char) (msg.charAt(i) ^ key.charAt(j));
j++;
if(j==8) j = 0;
}
System.out.print("Encoded message: ");
System.out.println(encMsg);
// decode the message
j = 0;
for(int i=0; i < msg.length(); i++) {
decMsg = decMsg + (char) (encMsg.charAt(i) ^ key.charAt(j));
j++;
if(j==8) j = 0;
}
System.out.print("Decoded message: ");
System.out.println(decMsg);
}
}
// -----------------------------------------
// Find the minimum and maximum values in an array.
class MinMax {
public static void main(String[] args) {
int[] nums = new int[10];
int min, max;
nums[0] = 99;
nums[1] = -10;
nums[2] = 100123;
nums[3] = 18;
nums[4] = -978;
nums[5] = 5623;
nums[6] = 463;
nums[7] = -9;
nums[8] = 287;
nums[9] = 49;
min = max = nums[0];
for(int v : nums) {
if(v < min) min = v;
if(v > max) max = v;
}
System.out.println("min and max: " + min + " " + max);
}
}
// -----------------------------------------
// Display a string backwards using recursion.
class Backwards {
String str;
Backwards(String s) {
str = s;
}
void backward(int idx) {
if(idx != str.length()-1) backward(idx+1);
System.out.print(str.charAt(idx));
}
}
class BWDemo {
public static void main(String[] args) {
Backwards s = new Backwards("This is a test");
s.backward(0);
}
}
// -----------------------------------------
class SumIt {
int sum(int ... n) {
int result = 0;
for(int i = 0; i < n.length; i++)
result += n[i];
return result;
}
}
class SumDemo {
public static void main(String[] args) {
SumIt siObj = new SumIt();
int total = siObj.sum(1, 2, 3);
System.out.println("Sum is " + total);
total = siObj.sum(1, 2, 3, 4, 5);
System.out.println("Sum is " + total);
}
}
// -----------------------------------------
// A subclass of TwoDShape for circles.
class Circle extends TwoDShape {
// A default constructor.
Circle() {
super();
}
// Construct Circle
Circle(double x) {
super(x, "circle"); // call superclass constructor
}
// Construct an object from an object.
Circle(Circle ob) {
super(ob); // pass object to TwoDShape constructor
}
double area() {
return (getWidth() / 2) * (getWidth() / 2) * 3.1416;
}
}
// -----------------------------------------
interface IVehicle {
// Return the range.
int range();
// Compute fuel needed for a given distance.
double fuelNeeded(int miles);
// Accessor methods
int getPassengers();
void setPassengers(int p);
int getFuelCap();
void setFuelCap(int f);
int getMpg();
void setMpg(int m);
}
// -----------------------------------------
/* Copy a text file, substituting hyphens for spaces.
This version uses byte streams.
To use this program, specify the name
of the source file and the destination file.
For example,
java Hyphen source target
*/
import java.io.*;
class Hyphen {
public static void main(String[] args)
{
int i;
FileInputStream fin = null;
FileOutputStream fout = null;
// First make sure that both files have been specified.
if(args.length !=2 ) {
System.out.println("Usage: Hyphen From To");
return;
}
// Copy file and substitute hyphens.
try {
fin = new FileInputStream(args[0]);
fout = new FileOutputStream(args[1]);
do {
i = fin.read();
// convert space to a hyphen
if((char)i == ' ') i = '-';
if(i != -1) fout.write(i);
} while(i != -1);
} catch(IOException exc) {
System.out.println("I/O Error: " + exc);
} finally {
try {
if(fin != null) fin.close();
} catch(IOException exc) {
System.out.println("Error closing input file.");
}
try {
if(fin != null) fout.close();
} catch(IOException exc) {
System.out.println("Error closing output file.");
}
}
}
}
// -----------------------------------------
/* Copy a text file, substituting hyphens for spaces.
This version uses character streams.
To use this program, specify the name
of the source file and the destination file.
For example,
java Hyphen2 source target
This code requires JDK 7 or later.
*/
import java.io.*;
class Hyphen2 {
public static void main(String[] args)
{
int i;
// First make sure that both files have been specified.
if(args.length !=2 ) {
System.out.println("Usage: CopyFile From To");
return;
}
// Copy file and substitute hyphens.
// Use the try-with-resources statement.
try (FileReader fin = new FileReader(args[0]);
FileWriter fout = new FileWriter(args[1]))
{
do {
i = fin.read();
// convert space to a hyphen
if((char)i == ' ') i = '-';
if(i != -1) fout.write(i);
} while(i != -1);
} catch(IOException exc) {
System.out.println("I/O Error: " + exc);
}
}
}
// -----------------------------------------
// Make the TickTock class actually keep time.
class TickTock {
String state; // contains the state of the clock
synchronized void tick(boolean running) {
if(!running) { // stop the clock
state = "ticked";
notify(); // notify any waiting threads
return;
}
System.out.print("Tick ");
// wait 1/2 second
try {
Thread.sleep(500);
} catch(InterruptedException exc) {
System.out.println("Thread interrupted.");
}
state = "ticked"; // set the current state to ticked
notify(); // let tock() run
try {
while(!state.equals("tocked"))
wait(); // wait for tock() to complete
}
catch(InterruptedException exc) {
System.out.println("Thread interrupted.");
}
}
synchronized void tock(boolean running) {
if(!running) { // stop the clock
state = "tocked";
notify(); // notify any waiting threads
return;
}
System.out.println("Tock");
// wait 1/2 second
try {
Thread.sleep(500);
} catch(InterruptedException exc) {
System.out.println("Thread interrupted.");
}
state = "tocked"; // set the current state to tocked
notify(); // let tick() run
try {
while(!state.equals("ticked"))
wait(); // wait for tick to complete
}
catch(InterruptedException exc) {
System.out.println("Thread interrupted.");
}
}
}
// -----------------------------------------
enum Tools {
SCREWDRIVER, WRENCH, HAMMER, PLIERS
}
class ShowEnum {
public static void main(String[] args) {
for(Tools d : Tools.values())
System.out.print(d + " has ordinal value of " +
d.ordinal() + '\n');
}
}
// -----------------------------------------
// An improved version of the traffic light simulation that
// stores the light delay in TrafficLightColor.
// An enumeration of the colors of a traffic light.
enum TrafficLightColor {
RED(12000), GREEN(10000), YELLOW(2000);
private int delay;
TrafficLightColor(int d) {
delay = d;
}
int getDelay() { return delay; }
}
// A computerized traffic light.
class TrafficLightSimulator implements Runnable {
private Thread thrd; // holds the thread that runs the simulation
private TrafficLightColor tlc; // holds the current traffic light color
boolean stop = false; // set to true to stop the simulation
boolean changed = false; // true when the light has changed
TrafficLightSimulator(TrafficLightColor init) {
tlc = init;
thrd = new Thread(this);
thrd.start();
}
TrafficLightSimulator() {
tlc = TrafficLightColor.RED;
thrd = new Thread(this);
thrd.start();
}
// Start up the light.
public void run() {
while(!stop) {
// Notice how this code has been simplified!
try {
Thread.sleep(tlc.getDelay());
} catch(InterruptedException exc) {
System.out.println(exc);
}
changeColor();
}
}
// Change color.
synchronized void changeColor() {
switch(tlc) {
case RED:
tlc = TrafficLightColor.GREEN;
break;
case YELLOW:
tlc = TrafficLightColor.RED;
break;
case GREEN:
tlc = TrafficLightColor.YELLOW;
}
changed = true;
notify(); // signal that the light has changed
}
// Wait until a light change occurs.
synchronized void waitForChange() {
try {
while(!changed)
wait(); // wait for light to change
changed = false;
} catch(InterruptedException exc) {
System.out.println(exc);
}
}
// Return current color.
synchronized TrafficLightColor getColor() {
return tlc;
}
// Stop the traffic light.
synchronized void cancel() {
stop = true;
}
}
class TrafficLightDemo {
public static void main(String[] args) {
TrafficLightSimulator tl =
new TrafficLightSimulator(TrafficLightColor.GREEN);
for(int i=0; i < 9; i++) {
System.out.println(tl.getColor());
tl.waitForChange();
}
tl.cancel();
}
}
// -----------------------------------------
/* A simple banner applet that uses parameters.
*/
import java.awt.*;
import java.applet.*;
/*
<applet code="ParamBanner" width=300 height=50>
<param name=message value=" I like Java! ">
<param name=delay value=500>
</applet>
*/
public class ParamBanner extends Applet implements Runnable {
String msg;
int delay;
Thread t;
boolean stopFlag;
// Initialize t to null.
public void init() {
String temp;
msg = getParameter("message");
if(msg == null) msg = " Java Rules the Web ";
temp = getParameter("delay");
try {
if(temp != null)
delay = Integer.parseInt(temp);
else
delay = 250; // default if not specified
} catch(NumberFormatException exc) {
delay = 250 ; // default on error
}
t = null;
}
// Start thread when the applet is needed.
public void start() {
t = new Thread(this);
stopFlag = false;
t.start();
}
// Entry point for the thread that runs the banner.
public void run() {
// Request a repaint at the specified interval.
for( ; ; ) {
try {
repaint();
Thread.sleep(delay);
if(stopFlag) break;
} catch(InterruptedException exc) {}
}
}
// Pause the banner.
public void stop() {
stopFlag = true;
t = null;
}
// Display the banner.
public void paint(Graphics g) {
char ch;
ch = msg.charAt(0);
msg = msg.substring(1, msg.length());
msg += ch;
g.drawString(msg, 50, 30);
}
}
// -----------------------------------------
// A simple clock applet.
import java.util.*;
import java.awt.*;
import java.applet.*;
/*
<object code="Clock" width=200 height=50>
</object>
*/
public class Clock extends Applet implements Runnable {
String msg;
Thread t;
Calendar clock;
boolean stopFlag;
// Initialize
public void init() {
t = null;
msg = "";
}
// Start thread when the applet is needed.
public void start() {
t = new Thread(this);
stopFlag = false;
t.start();
}
// Entry point for the clock.
public void run() {
// Request a repaint every second.
for( ; ; ) {
try {
repaint();
Thread.sleep(1000);
if(stopFlag) break;
} catch(InterruptedException exc) {}
}
}
// Pause the clock.
public void stop() {
stopFlag = true;
t = null;
}
// Display the clock.
public void paint(Graphics g) {
clock = Calendar.getInstance();
msg = "Current time is " +
Integer.toString(clock.get(Calendar.HOUR));
msg = msg + ":" +
Integer.toString(clock.get(Calendar.MINUTE));
msg = msg + ":" +
Integer.toString(clock.get(Calendar.SECOND));
g.drawString(msg, 30, 30);
}
}
// -----------------------------------------
// A version of the stopwatch for Try This 17-1 that
// uses a single push button.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class StopWatch implements ActionListener {
JLabel jlab;
long start; // holds the start time in milliseconds
JButton jbtnStartStop; // a start or stop button
StopWatch() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("A Simple Stopwatch");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(250, 90);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make one button.
jbtnStartStop = new JButton("Start");
// Add action listeners.
jbtnStartStop.addActionListener(this);
// Add the buttons to the content pane.
jfrm.add(jbtnStartStop);
// Create a text-based label.
jlab = new JLabel("Press Start to begin timing.");
// Add the label to the frame.
jfrm.add(jlab);
// Display the frame.
jfrm.setVisible(true);
}
// Handle button events.
public void actionPerformed(ActionEvent ae) {
// get the current system time
Calendar cal = Calendar.getInstance();
if(ae.getActionCommand().equals("Start")) {
// Store start time.
start = cal.getTimeInMillis();
jlab.setText("Stopwatch is Running...");
jbtnStartStop.setText("Stop");
}
else {
// Compute the elapsed time.
jlab.setText("Elapsed time is "
+ (double) (cal.getTimeInMillis() - start)/1000);
jbtnStartStop.setText("Start");
}
}
public static void main(String[] args) {
// Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new StopWatch();
}
});
}
}
// -----------------------------------------
// Menu demo program, final version.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MenuDemo implements ActionListener {
JLabel jlab;
JMenuBar jmb;
MenuDemo() {
// Create a new JFrame container.
JFrame jfrm = new JFrame("Menu Demo");
// Specify FlowLayout for the layout manager.
jfrm.setLayout(new FlowLayout());
// Give the frame an initial size.
jfrm.setSize(220, 200);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label that will display the menu selection.
jlab = new JLabel();
// Create the menu bar.
jmb = new JMenuBar();
// Create the File menu.
makeFileMenu();
// Create the Options menu.
makeOptionsMenu();
// Create the Help menu.
makeHelpMenu();
// Add the label to the content pane.
jfrm.add(jlab);
// Add the menu bar to the frame.
jfrm.setJMenuBar(jmb);
// Display the frame.
jfrm.setVisible(true);
}
// Create the File menu with mnemonics and accelerators.
void makeFileMenu() {
JMenu jmFile = new JMenu("File");
jmFile.setMnemonic(KeyEvent.VK_F);
JMenuItem jmiOpen = new JMenuItem("Open",
KeyEvent.VK_O);
jmiOpen.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_DOWN_MASK));
JMenuItem jmiClose = new JMenuItem("Close",
KeyEvent.VK_C);
jmiClose.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_DOWN_MASK));
JMenuItem jmiSave = new JMenuItem("Save",
KeyEvent.VK_S);
jmiSave.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_DOWN_MASK));
JMenuItem jmiExit = new JMenuItem("Exit",
KeyEvent.VK_E);
jmiExit.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_E,
InputEvent.CTRL_DOWN_MASK));
jmFile.add(jmiOpen);
jmFile.add(jmiClose);
jmFile.add(jmiSave);
jmFile.addSeparator();
jmFile.add(jmiExit);
jmb.add(jmFile);
jmiOpen.addActionListener(this);
jmiClose.addActionListener(this);
jmiSave.addActionListener(this);
jmiExit.addActionListener(this);
}
// Create the Options menu.
void makeOptionsMenu() {
// Create the Options menu.
JMenu jmOptions = new JMenu("Options");
// Create the Colors submenu.
JMenu jmColors = new JMenu("Colors");
// Use check boxes for colors. This allows
// the user to select more than one color.
// Notice that Red is initially selected.
JCheckBoxMenuItem jmiRed = new JCheckBoxMenuItem("Red", true);
JCheckBoxMenuItem jmiGreen = new JCheckBoxMenuItem("Green");
JCheckBoxMenuItem jmiBlue = new JCheckBoxMenuItem("Blue");
jmColors.add(jmiRed);
jmColors.add(jmiGreen);
jmColors.add(jmiBlue);
jmOptions.add(jmColors);
// Create the Priority submenu.
JMenu jmPriority = new JMenu("Priority");
// Use radio buttons for the priority setting.
// This lets the menu show which priority is used
// but also ensures that one and only one priority
// can be selected at any one time. Notice that
// the High radio button is initially selected.
JRadioButtonMenuItem jmiHigh =
new JRadioButtonMenuItem("High", true);
JRadioButtonMenuItem jmiLow =
new JRadioButtonMenuItem("Low");
jmPriority.add(jmiHigh);
jmPriority.add(jmiLow);
jmOptions.add(jmPriority);
// Create button group for the radio button menu items.
ButtonGroup bg = new ButtonGroup();
bg.add(jmiHigh);
bg.add(jmiLow);