forked from ZhiqingXiao/java-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter23.java
426 lines (316 loc) · 10.6 KB
/
chapter23.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
class DoubleDemo {
public static void main(String[] args) {
Double d1 = new Double(3.14159);
Double d2 = new Double("314159E-5");
System.out.println(d1 + " " + d2);
}
}
// -----------------------------------------
// Demonstrate isInfinite() and isNaN()
class InfNaN {
public static void main(String[] args) {
Double d1 = new Double(1/0.0);
Double d2 = new Double(0/0.0);
System.out.println(d1 + ": " + d1.isInfinite() + ", " + d1.isNaN());
System.out.println(d2 + ": " + d2.isInfinite() + ", " + d2.isNaN());
}
}
// -----------------------------------------
// Demonstrate parseInt().
class ParseBinary {
public static void main(String[] args) {
int num;
String str = "10011101";
num = Integer.parseInt(str, 2);
System.out.println("Here is " + str + " in decimal: " + num);
}
}
// -----------------------------------------
// Convert an integer into binary, hexadecimal,
// and octal.
class StringConversions {
public static void main(String[] args) {
int num = 19648;
System.out.println(num + " in binary: " +
Integer.toBinaryString(num));
System.out.println(num + " in octal: " +
Integer.toOctalString(num));
System.out.println(num + " in hexadecimal: " +
Integer.toHexString(num));
}
}
// -----------------------------------------
// Demonstrate rotateLeft() and rotateRight()
class Rotations {
public static void main(String[] args) {
int num = -3356756;
System.out.println(Integer.toBinaryString(num));
num = Integer.rotateLeft(num, 2);
System.out.println(Integer.toBinaryString(num));
num = Integer.rotateRight(num, 2);
System.out.println(Integer.toBinaryString(num));
}
}
// -----------------------------------------
// Demonstrate several is... methods.
class IsDemo {
public static void main(String[] args) {
char[] a = {'a', 'b', '5', '?', 'A', ' ', '\t', '\n'};
for(int i=0; i<a.length; i++) {
if(Character.isDigit(a[i]))
System.out.println(a[i] + " is a digit.");
if(Character.isLetter(a[i]))
System.out.println(a[i] + " is a letter.");
if(Character.isLetterOrDigit(a[i]))
System.out.println(a[i] + " is a letter or digit.");
if(Character.isWhitespace(a[i])) {
String chStr = "";
if(a[i] == ' ') chStr = "<space>";
else if(a[i] == '\t') chStr = "<tab>";
else if(a[i] == '\n') chStr = "<newline>";
System.out.println(chStr + " is whitespace.");
}
if(Character.isSpaceChar(a[i])) {
String chStr = "";
if(a[i] == ' ') chStr = "<space>";
else if(a[i] == '\t') chStr = "<tab>";
else if(a[i] == '\n') chStr = "<newline>";
System.out.println(chStr + " is space.");
}
if(Character.isUpperCase(a[i]))
System.out.println(a[i] + " is uppercase.");
if(Character.isLowerCase(a[i]))
System.out.println(a[i] + " is lowercase.");
System.out.println();
}
}
}
// -----------------------------------------
// Demonstrate several Math functions.
class MathDemo {
public static void main(String[] args) {
// convert between radians and degrees
double theta = 120.0;
System.out.println(theta + " degrees is " +
Math.toRadians(theta) + " radians.");
theta = 1.312;
System.out.println(theta + " radians is " +
Math.toDegrees(theta) + " degrees\n");
// demonstrate sin() and asin()
theta = 1.0;
double sine = Math.sin(theta);
double asine = Math.asin(sine);
System.out.println("Sine of " + theta + " is " + sine);
System.out.println("Arc sine of " + sine + " is " + asine + "\n");
// find the hypotenuse of a right triangle
double h = Math.hypot(3.0, 4.0);
System.out.println("Hypotenuse is " + h + "\n");
// compute a power
double p = Math.pow(3.0, 3.0);
System.out.println("pow(3.0, 3.0) is " + p + "\n");
// use log10()
double lg = Math.log10(100.0);
System.out.println("log10(100.0) is " + lg + "\n");
// display E and PI
System.out.println("PI: " + Math.PI + "\n E: " + Math.E);
}
}
// -----------------------------------------
// Demonstrate ProcessBuilder.
import java.io.IOException;
class PBDemo {
public static void main(String[] args) {
try {
ProcessBuilder procBldr =
new ProcessBuilder("notepad", "testfile");
procBldr.start();
} catch (IOException exc) {
System.out.println("Error executing notepad.\n" + exc);
}
}
}
// -----------------------------------------
// Wait until the process is terminated.
import java.io.IOException;
class PBDemo2 {
public static void main(String[] args) {
try {
ProcessBuilder procBldr =
new ProcessBuilder("notepad", "testfile");
Process p = procBldr.start();
p.waitFor();
System.out.println("Notepad returned " + p.exitValue());
} catch (IOException exc) {
System.out.println("Error executing notepad.\n" + exc);
} catch (InterruptedException exc) {
System.out.println("Wait interrupted\n" + exc);
}
}
}
// -----------------------------------------
// Demonstrate destroy().
import java.io.IOException;
class PBDemo3 {
public static void main(String[] args) {
try {
ProcessBuilder procBldr =
new ProcessBuilder("notepad", "testfile");
Process p = procBldr.start();
try {
Thread.sleep(2000);
} catch (InterruptedException exc) {
System.out.println("Sleep interrupted\n" + exc);
}
// terminate the process.
p.destroy();
} catch (IOException exc) {
System.out.println("Error executing notepad.\n" + exc);
}
}
}
// -----------------------------------------
// Demonstrate totalMemory(), freeMemory() and gc().
class MemoryDemo {
public static void main(String[] args) {
Runtime r = Runtime.getRuntime();
long mem1, mem2;
Integer[] someints = new Integer[1000];
System.out.println("Total memory is: " +
r.totalMemory());
mem1 = r.freeMemory();
System.out.println("Initial free memory: " + mem1);
r.gc();
mem1 = r.freeMemory();
System.out.println("Free memory after garbage collection: "
+ mem1);
for(int i=0; i<1000; i++)
someints[i] = new Integer(i); // allocate integers
mem2 = r.freeMemory();
System.out.println("Free memory after allocation: "
+ mem2);
System.out.println("Memory used by allocation: "
+ (mem1-mem2));
// discard Integers
for(int i=0; i<1000; i++) someints[i] = null;
r.gc(); // request garbage collection
mem2 = r.freeMemory();
System.out.println("Free memory after collecting" +
" discarded Integers: " + mem2);
}
}
// -----------------------------------------
// Timing program execution.
class Elapsed {
public static void main(String[] args) {
long start, end;
System.out.println("Timing a for loop from 0 to 100,000,000");
// time a for loop from 0 to 100,000,000
start = System.currentTimeMillis(); // get starting time
for(long i=0; i < 100000000L; i++) ;
end = System.currentTimeMillis(); // get ending time
System.out.println("Elapsed time: " + (end-start));
}
}
// -----------------------------------------
// Using arraycopy().
class ACDemo {
static byte[] a = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 };
static byte[] b = { 77, 77, 77, 77, 77, 77, 77, 77, 77, 77 };
public static void main(String[] args) {
System.out.println("a = " + new String(a));
System.out.println("b = " + new String(b));
System.arraycopy(a, 0, b, 0, a.length);
System.out.println("a = " + new String(a));
System.out.println("b = " + new String(b));
System.arraycopy(a, 0, a, 1, a.length - 1);
System.arraycopy(b, 1, b, 0, b.length - 1);
System.out.println("a = " + new String(a));
System.out.println("b = " + new String(b));
}
}
// -----------------------------------------
class ShowProperties {
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("file.separator"));
}
}
// -----------------------------------------
// Redirect System.out to a file.
import java.io.*;
class RedirectOut {
public static void main(String[] args) throws IOException
{
// First, confirm that a file has been specified.
if(args.length != 1) {
System.out.println("RedirectOut: to");
return;
}
// Create a Printstream linked to the specified file.
try (PrintStream fout = new PrintStream(args[0]))
{
// save original System.out
PrintStream orgOut = System.out;
// redirect System.out to the file.
System.setOut(fout);
// notice that System.out is used here
System.out.println("This goes in the file.");
// restore original System.out
System.setOut(orgOut);
System.out.println("This is shown on the screen.");
} catch(IOException exc) {
System.out.println("I/O Error: " + exc);
}
}
}
// -----------------------------------------
// Demonstrate Run-Time Type Information.
class X {
int a;
float b;
}
class Y extends X {
double c;
}
class RTTI {
public static void main(String[] args) {
X x = new X();
Y y = new Y();
Class<?> clObj;
clObj = x.getClass(); // get Class reference
System.out.println("x is object of type: " +
clObj.getName());
clObj = y.getClass(); // get Class reference
System.out.println("y is object of type: " +
clObj.getName());
clObj = clObj.getSuperclass();
System.out.println("y's superclass is " +
clObj.getName());
}
}
// -----------------------------------------
class Finder {
public static <T extends Comparable<T>> T minElement(T[] data) {
T currMin = data[0];
for(T x : data) {
if (x.compareTo(currMin) < 0)
currMin = x;
}
return currMin;
}
}
class MinElementDemo {
public static void main(String[] args) {
Integer[] intArray = { 3, 1, 4, 3, 6, 5};
int intMin = Finder.minElement(intArray);
System.out.println(intMin);
String[] strArray = {"every", "good", "boy", "does", "fine"};
String strMin = Finder.minElement(strArray);
System.out.println(strMin);
Double[] doubleArray = {3.14, 2.8, 6.023, 1.414};
Double doubleMin = Finder.minElement(doubleArray);
System.out.println(doubleMin);
}
}