forked from ZhiqingXiao/java-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter13.java
413 lines (320 loc) · 9.28 KB
/
chapter13.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
// An enumeration of Transport varieties.
enum Transport {
CAR, TRUCK, AIRPLANE, TRAIN, BOAT
}
class EnumDemo {
public static void main(String[] args)
{
Transport tp;
tp = Transport.AIRPLANE;
// Output an enum value.
System.out.println("Value of tp: " + tp);
System.out.println();
tp = Transport.TRAIN;
// Compare two enum values.
if(tp == Transport.TRAIN)
System.out.println("tp contains TRAIN.\n");
// Use an enum to control a switch statement.
switch(tp) {
case CAR:
System.out.println("A car carries people.");
break;
case TRUCK:
System.out.println("A truck carries freight.");
break;
case AIRPLANE:
System.out.println("An airplane flies.");
break;
case TRAIN:
System.out.println("A train runs on rails.");
break;
case BOAT:
System.out.println("A boat sails on water.");
break;
}
}
}
// -----------------------------------------
// Use the built-in enumeration methods.
// An enumeration of Transport varieties.
enum Transport {
CAR, TRUCK, AIRPLANE, TRAIN, BOAT
}
class EnumDemo2 {
public static void main(String[] args)
{
Transport tp;
System.out.println("Here are all Transport constants");
// use values()
Transport[] allTransports = Transport.values();
for(Transport t : allTransports)
System.out.println(t);
System.out.println();
// use valueOf()
tp = Transport.valueOf("AIRPLANE");
System.out.println("tp contains " + tp);
}
}
// -----------------------------------------
// Use an enum constructor, instance variable, and method.
enum Transport {
CAR(65), TRUCK(55), AIRPLANE(600), TRAIN(70), BOAT(22);
private int speed; // typical speed of each transport
// Constructor
Transport(int s) { speed = s; }
int getSpeed() { return speed; }
}
class EnumDemo3 {
public static void main(String[] args)
{
Transport tp;
// Display speed of an airplane.
System.out.println("Typical speed for an airplane is " +
Transport.AIRPLANE.getSpeed() +
" miles per hour.\n");
// Display all Transports and speeds.
System.out.println("All Transport speeds: ");
for(Transport t : Transport.values())
System.out.println(t + " typical speed is " +
t.getSpeed() +
" miles per hour.");
}
}
// -----------------------------------------
// Demonstrate ordinal() and compareTo().
// An enumeration of Transport varieties.
enum Transport {
CAR, TRUCK, AIRPLANE, TRAIN, BOAT
}
class EnumDemo4 {
public static void main(String[] args)
{
Transport tp, tp2, tp3;
// Obtain all ordinal values using ordinal().
System.out.println("Here are all Transport constants" +
" and their ordinal values: ");
for(Transport t : Transport.values())
System.out.println(t + " " + t.ordinal());
tp = Transport.AIRPLANE;
tp2 = Transport.TRAIN;
tp3 = Transport.AIRPLANE;
System.out.println();
// Demonstrate compareTo()
if(tp.compareTo(tp2) < 0)
System.out.println(tp + " comes before " + tp2);
if(tp.compareTo(tp2) > 0)
System.out.println(tp2 + " comes before " + tp);
if(tp.compareTo(tp3) == 0)
System.out.println(tp + " equals " + tp3);
}
}
// -----------------------------------------
// Try This 13-1
// A simulation of a traffic light that uses
// an enumeration to describe the light's color.
// An enumeration of the colors of a traffic light.
enum TrafficLightColor {
RED, GREEN, YELLOW
}
// A computerized traffic light.
class TrafficLightSimulator implements Runnable {
private Thread thrd; // holds the thread that runs the simulation
private TrafficLightColor tlc; // holds the current 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) {
try {
switch(tlc) {
case GREEN:
Thread.sleep(10000); // green for 10 seconds
break;
case YELLOW:
Thread.sleep(2000); // yellow for 2 seconds
break;
case RED:
Thread.sleep(12000); // red for 12 seconds
break;
}
} 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();
}
}
// -----------------------------------------
// Demonstrate manual boxing and unboxing with a type wrapper.
class Wrap {
public static void main(String[] args) {
Integer iOb = new Integer(100);
int i = iOb.intValue();
System.out.println(i + " " + iOb); // displays 100 100
}
}
// -----------------------------------------
// Demonstrate autoboxing/unboxing.
class AutoBox {
public static void main(String[] args) {
Integer iOb = 100; // autobox an int
int i = iOb; // auto-unbox
System.out.println(i + " " + iOb); // displays 100 100
}
}
// -----------------------------------------
// Autoboxing/unboxing takes place with method parameters
// and return values.
class AutoBox2 {
// This method has an Integer parameter.
static void m(Integer v) {
System.out.println("m() received " + v);
}
// This method returns an int.
static int m2() {
return 10;
}
// This method returns an Integer.
static Integer m3() {
return 99; // autoboxing 99 into an Integer.
}
public static void main(String[] args) {
// Pass an int to m(). Because m() has an Integer
// parameter, the int value passed is automatically boxed.
m(199);
// Here, iOb receives the int value returned by m2().
// This value is automatically boxed so that it can be
// assigned to iOb.
Integer iOb = m2();
System.out.println("Return value from m2() is " + iOb);
// Next, m3() is called. It returns an Integer value
// which is auto-unboxed into an int.
int i = m3();
System.out.println("Return value from m3() is " + i);
// Next, Math.sqrt() is called with iOb as an argument.
// In this case, iOb is auto-unboxed and its value promoted to
// double, which is the type needed by sqrt().
iOb = 100;
System.out.println("Square root of iOb is " + Math.sqrt(iOb));
}
}
// -----------------------------------------
// Autoboxing/unboxing occurs inside expressions.
class AutoBox3 {
public static void main(String[] args) {
Integer iOb, iOb2;
int i;
iOb = 99;
System.out.println("Original value of iOb: " + iOb);
// The following automatically unboxes iOb,
// performs the increment, and then reboxes
// the result back into iOb.
++iOb;
System.out.println("After ++iOb: " + iOb);
// Here, iOb is unboxed, its value is increased by 10,
// and the result is boxed and stored back in iOb.
iOb += 10;
System.out.println("After iOb += 10: " + iOb);
// Here, iOb is unboxed, the expression is
// evaluated, and the result is reboxed and
// assigned to iOb2.
iOb2 = iOb + (iOb / 3);
System.out.println("iOb2 after expression: " + iOb2);
// The same expression is evaluated, but the
// result is not reboxed.
i = iOb + (iOb / 3);
System.out.println("i after expression: " + i);
}
}
// -----------------------------------------
// A simple annotation type.
@interface MyAnno {
String str();
int val();
}
// -----------------------------------------
// Give val() a default value
@interface MyAnno {
String str();
int val() default 42;
}
// -----------------------------------------
@interface MySingle {
int value();
}
// -----------------------------------------
// An example that uses @Deprecated.
// Deprecate a class.
@Deprecated
class MyClass {
private String msg;
MyClass(String m) {
msg = m;
}
// Deprecate a method within a class.
@Deprecated
String getMsg() {
return msg;
}
// ...
}
class AnnoDemo {
public static void main(String[] args) {
MyClass myObj = new MyClass("test");
System.out.println(myObj.getMsg());
}
}