TIJ4 Solutions Annotated Solution Guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 778

DownloadTIJ4-solutions Annotated Solution Guide
Open PDF In BrowserView PDF
Annotated Solution Guide for

Thinking
in
Java
Fourth Edition

TIJ4 published February, 2006

Copyright &
Disclaimer
This Annotated Solution Guide for Thinking in Java, Fourth Edition
is not freeware. You cannot post it on any website, reproduce or distribute it,
display it publicly (such as on overhead slides), or make it the basis of any
derivative work. Copyrighted by MindView, Inc., this publication is sold only at
www.MindView.net.
The Source Code in this book is provided without express or implied warranty of
any kind, including any implied warranty of merchantability, fitness for a
particular purpose or non-infringement. MindView, Inc. does not warrant that
the operation of any program that includes the Source Code will be
uninterrupted or error-free. MindView, Inc. makes no representation about the
suitability of the Source Code or of any software that includes the Source Code
for any purpose. The entire risk as to the quality and performance of any
program that includes the Source Code is with the user of the Source Code. The
user understands that the Source Code was developed for research and
instructional purposes and is advised not to rely exclusively for any reason on
the Source Code or any program that includes the Source Code. Should the
Source Code or any resulting software prove defective, the user assumes the cost
of all necessary servicing, repair, or correction.
IN NO EVENT SHALL MINDVIEW, INC., OR ITS PUBLISHER BE LIABLE TO
ANY PARTY UNDER ANY LEGAL THEORY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
LOST PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
INFORMATION, OR ANY OTHER PECUNIARY LOSS, OR FOR PERSONAL
INJURIES, ARISING OUT OF THE USE OF THIS SOURCE CODE AND ITS
DOCUMENTATION, OR ARISING OUT OF THE INABILITY TO USE ANY
RESULTING PROGRAM, EVEN IF MINDVIEW, INC., OR ITS PUBLISHER
HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. MINDVIEW,
INC. SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE. THE SOURCE CODE AND
DOCUMENTATION PROVIDED HEREUNDER IS ON AN “AS IS” BASIS,
WITHOUT ANY ACCOMPANYING SERVICES FROM MINDVIEW, INC., AND
MINDVIEW, INC. HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

i

About this Document
This is the annotated solution guide for Thinking in Java, Fourth Edition.
Thinking in Java, Fourth Edition is available in print from Prentice Hall and for
sale electronically from www.mindview.net. This solution guide is only available
online as a PDF document along with the associated source code (if printed in
the same format as Thinking in Java, this guide would be almost 900 pages).
You can download a free sample of this solution guide, up to and including the
solutions for the chapters Everything is an Object and Operators, from
http://www.mindviewinc.com/Books/TIJ4/Solutions. You should do this before
buying the guide to make sure that you can properly install, compile and run the
solutions on your system.
The complete version of this book including all exercises is only available
electronically, for US $25 (credit cards or PayPal), from
http://www.mindviewinc.com/Books/TIJ4/Solutions/.

Unpacking the Distribution
The Annotated Solutions Guide is distributed as a zipped file containing the
book in Adobe Acrobat PDF format, along with a source-code tree in the
code.zip file. Please make sure your computer can unzip files before
purchasing the guide. There are free unzip utilities for virtually every
platform, available by searching on the Internet.

Linux/Unix (including Mac OSX) Users: Unzip the file using InfoZip (pre-installed on many Linux distributions, or available at http://www.infozip.org/). Unzip the package for Linux/Unix using the –a option to correct for
the difference between DOS and Unix newlines, like this:
unzip –a TIJ4-solutions.zip

All Users: This guide uses the Ant build system, and includes Ant build.xml
files in each subdirectory of the code tree, which compile and run the code
examples using the javac compiler in the Sun JDK (available at
http://java.sun.com/j2se/). Ant, the standard build tool for Java projects, is an
open-source tool. The full download, installation and configuration instructions,
along with the Ant executable and documentation are available at
http://ant.apache.org/.

iii

Once you install and configure Ant on your computer, you can type ant at the
command prompt of the guide’s source-code root directory to build and test all
the code. You can also choose to build and test the code for a particular chapter.
For example, to build and test the code from the Polymorphism chapter, enter
the polymorphism sub-directory and type ant from there.
Full detailed instructions for installation can be found after the table of contents.

No Exercises in Chapter 1
The chapter Introduction to Objects has no exercises.

Additional exercises
This guide features additional exercises not included in Thinking in Java, for
which solutions are not provided, as a challenge to the reader.

iv

Thinking in Java, 4th Edition Annotated Solution Guide

Contents
Copyright & Disclamer
About this Document

i
iii

Unpacking
the Distribution.................. iii
No Exercises in Chapter 1.. iv
Additional exercises........... iv

Installing the Code

1

Using Eclipse....................... 3

Packages & IDEs

5

Left to the Reader

7

Everything is an Object

9

Exercise 1............................. 9
Exercise 2 ............................ 9
Exercise 3 ...........................10
Exercise 4 ...........................10
Exercise 5 ........................... 11
Exercise 6 ........................... 11
Exercise 7 ...........................12
Exercise 8 ...........................13
Exercise 9 ...........................13
Exercise 10 .........................14
Exercise 11 ..........................16
Exercise 12 .........................16
Exercise 13.......................... 17
Exercise 14 ......................... 17
Exercise 15..........................18
Exercise 16 .........................18

Operators

21

Exercise 1............................21
Exercise 2 ...........................21
Exercise 3 .......................... 22
Exercise 4 .......................... 23
Exercise 5 .......................... 24
Exercise 6 .......................... 24
Exercise 7 .......................... 26

v

Exercise 8 .......................... 27
Exercise 9 .......................... 27
Exercise 10 ........................ 28
Exercise 11 ......................... 29
Exercise 12 .........................31
Exercise 13......................... 33
Exercise 14 ........................ 33

Controlling Execution

35

Exercise 1........................... 35
Exercise 2 .......................... 35
Exercise 3 .......................... 37
Exercise 4 .......................... 38
Exercise 5 .......................... 39
Exercise 6 ...........................41
Exercise 7 .......................... 42
Exercise 8 .......................... 43
Exercise 9 .......................... 45
Exercise 10 ........................ 46

Initialization & Cleanup

49

Exercise 1........................... 49
Exercise 2 .......................... 49
Exercise 3 .......................... 50
Exercise 4 ...........................51
Exercise 5 ...........................51
Exercise 6 .......................... 52
Exercise 7 .......................... 53
Exercise 8 .......................... 54
Exercise 9 .......................... 54
Exercise 10 ........................ 55
Exercise 11 ......................... 55
Exercise 12 ........................ 56
Exercise 13..........................57
Exercise 14 ........................ 58
Exercise 15......................... 58
Exercise 16 ........................ 59
Exercise 17......................... 60
Exercise 18 .........................61
Exercise 19 .........................61
Exercise 20........................ 62
Exercise 21 ........................ 63
Exercise 22 ........................ 63

vi

Thinking in Java, 4th Edition Annotated Solution Guide

Access Control

65

Exercise 1........................... 65
Exercise 2 .......................... 65
Exercise 3 .......................... 66
Exercise 4 .......................... 67
Exercise 5 .......................... 68
Exercise 6 .......................... 70
Exercise 7 .......................... 70
Exercise 8 ........................... 71
Exercise 9 ...........................75

Reusing Classes

77

Exercise 1............................77
Exercise 2 .......................... 78
Exercise 3 .......................... 79
Exercise 4 .......................... 80
Exercise 5 ...........................81
Exercise 6 ...........................81
Exercise 7 .......................... 82
Exercise 8 .......................... 83
Exercise 9 .......................... 84
Exercise 10 ........................ 85
Exercise 11 ......................... 86
Exercise 12 ........................ 87
Exercise 13......................... 89
Exercise 14 ........................ 90
Exercise 15..........................91
Exercise 16 ........................ 92
Exercise 17......................... 92
Exercise 18 ........................ 93
Exercise 19 ........................ 94
Exercise 20........................ 95
Exercise 21 ........................ 96
Exercise 22 ........................ 96
Exercise 23 ........................ 97
Exercise 24 ........................ 98

Polymorphism

99

Exercise 1........................... 99
Exercise 2 ........................ 100
Exercise 3 ........................ 102
Exercise 4 ........................ 104
Exercise 5 .........................105
Exercise 6 ........................ 106

Contents

vii

Exercise 7 ........................ 108
Exercise 8 ........................ 109
Exercise 9 ..........................111
Exercise 10 ....................... 113
Exercise 11 ........................ 114
Exercise 12 ....................... 115
Exercise 13........................ 116
Exercise 14 ....................... 118
Exercise 15........................ 121
Exercise 16 .......................122
Exercise 17........................123

Interfaces

125

Exercise 1..........................125
Exercise 2 .........................126
Exercise 3 .........................127
Exercise 4 ........................ 128
Exercise 5 .........................129
Exercise 6 ........................ 130
Exercise 7 ........................ 130
Exercise 8 .........................132
Exercise 9 .........................133
Exercise 10 .......................135
Exercise 11 ........................136
Exercise 12 .......................138
Exercise 13........................139
Exercise 14 ...................... 140
Exercise 15........................142
Exercise 16 .......................143
Exercise 17........................145
Exercise 18 .......................146
Exercise 19 .......................147

Inner Classes

149

Exercise 1..........................149
Exercise 2 .........................149
Exercise 3 .........................150
Exercise 4 ......................... 151
Exercise 5 .........................152
Exercise 6 .........................152
Exercise 7 .........................153
Exercise 8 .........................154
Exercise 9 .........................155
Exercise 10 .......................156

viii

Thinking in Java, 4th Edition Annotated Solution Guide

Exercise 11 ........................ 157
Exercise 12 .......................158
Exercise 13........................158
Exercise 14 .......................159
Exercise 15....................... 160
Exercise 16 ....................... 161
Exercise 17........................162
Exercise 18 .......................163
Exercise 19 .......................164
Exercise 20.......................165
Exercise 21 .......................166
Exercise 22 .......................167
Exercise 23 ...................... 168
Exercise 24 .......................170
Exercise 25 .......................174
Exercise 26 ....................... 175

Holding Your Objects

179

Exercise 1..........................179
Exercise 2 ........................ 180
Exercise 3 ........................ 180
Exercise 4 ......................... 181
Exercise 5 .........................183
Exercise 6 .........................185
Exercise 7 .........................187
Exercise 8 ........................ 188
Exercise 9 ........................ 189
Exercise 10 ...................... 190
Exercise 11 ........................ 191
Exercise 12 .......................192
Exercise 13........................193
Exercise 14 .......................197
Exercise 15........................197
Exercise 16 ...................... 198
Exercise 17........................199
Exercise 18 ......................200
Exercise 19 ...................... 201
Exercise 20...................... 202
Exercise 21 ...................... 203
Exercise 22 ...................... 204
Exercise 23 ...................... 206
Exercise 24 ......................208
Exercise 25 ...................... 209
Exercise 26 ...................... 210

Contents

ix

Exercise 27 .......................212
Exercise 28.......................214
Exercise 29 .......................214
Exercise 30.......................215
Exercise 31........................217
Exercise 32 .......................219

Error Handling
with Exceptions

221

Exercise 1..........................221
Exercise 2 .........................221
Exercise 3 ........................ 222
Exercise 4 ........................ 223
Exercise 5 ........................ 224
Exercise 6 ........................ 225
Exercise 7 ........................ 226
Exercise 8 ........................ 227
Exercise 9 ........................ 228
Exercise 10 ...................... 229
Exercise 11 ....................... 229
Exercise 12 ...................... 230
Exercise 13........................231
Exercise 14 ...................... 232
Exercise 15....................... 233
Exercise 16 ...................... 234
Exercise 17....................... 235
Exercise 18 ...................... 237
Exercise 19 ...................... 238
Exercise 20...................... 239
Exercise 21 .......................241
Exercise 22 ...................... 242
Exercise 23 ...................... 243
Exercise 24 ...................... 245
Exercise 25 ...................... 246
Exercise 26 ...................... 247
Exercise 27 ...................... 248
Exercise 28...................... 248
Exercise 29 ...................... 249
Exercise 30...................... 250

Strings

253

Exercise 1......................... 253
Exercise 2 ........................ 254
Exercise 3 ........................ 255

x

Thinking in Java, 4th Edition Annotated Solution Guide

Exercise 4 ........................ 256
Exercise 5 ........................ 257
Exercise 6 ........................ 260
Exercise 7 .........................261
Exercise 8 ........................ 262
Exercise 9 ........................ 262
Exercise 10 ...................... 263
Exercise 11 ....................... 266
Exercise 12 ...................... 267
Exercise 13....................... 268
Exercise 14 ...................... 270
Exercise 15....................... 270
Exercise 16 ...................... 272
Exercise 17....................... 273
Alternative A ...............................274
Alternative B ...............................275

Exercise 18 ...................... 278
Alternative A ...............................278
Alternative B ...............................279

Exercise 19 ......................280
Alternative A .............................. 280
Alternative B ...............................281

Exercise 20...................... 282

Type Information

285

Exercise 1......................... 285
Exercise 2 ........................ 286
Exercise 3 ........................288
Exercise 4 ........................288
Exercise 5 ........................ 289
Exercise 6 ........................ 290
Exercise 7 ........................ 293
Exercise 8 ........................ 294
Exercise 9 ........................ 295
Exercise 10 ...................... 298
Exercise 11 ....................... 299
Exercise 12 ...................... 303
Exercise 13....................... 303
Exercise 14 ...................... 304
Exercise 15....................... 306
Exercise 16 ...................... 309
Exercise 17........................312
Exercise 18 .......................313
Exercise 19 .......................315

Contents

xi

Exercise 20.......................316
Exercise 21 .......................317
Exercise 22 .......................318
Exercise 23 ...................... 320
Exercise 24 .......................321
Exercise 25 ...................... 324
Exercise 26 ...................... 325

Generics

329

Exercise 1......................... 329
Exercise 2 ........................ 329
Exercise 3 ........................ 330
Exercise 4 .........................331
Exercise 5 ........................ 332
Exercise 6 ........................ 333
Exercise 7 ........................ 334
Exercise 8 ........................ 335
Exercise 9 ........................ 337
Exercise 10 ...................... 338
Exercise 11 ....................... 338
Exercise 12 ...................... 339
Exercise 13....................... 340
Exercise 14 .......................341
Exercise 15....................... 342
Exercise 16 ...................... 343
Exercise 17....................... 344
Exercise 18 ...................... 346
Exercise 19 ...................... 347
Exercise 20...................... 349
Exercise 21 ...................... 350
Exercise 22 .......................351
Exercise 23 ...................... 352
Exercise 24 ...................... 353
Exercise 25 ...................... 354
Exercise 26 ...................... 355
Exercise 27 ...................... 356
Exercise 28...................... 356
Exercise 29 ...................... 357
Exercise 30...................... 359
Exercise 31....................... 360
Exercise 32 ...................... 360
Exercise 33 .......................361
Exercise 34 ...................... 363
Exercise 35 ...................... 364

xii

Thinking in Java, 4th Edition Annotated Solution Guide

Exercise 36 ...................... 365
Exercise 37 ...................... 367
Exercise 38 ...................... 368
Exercise 39 ...................... 370
Exercise 40...................... 370
Exercise 41 ...................... 372
Exercise 42 ...................... 373

Arrays

377

Exercise 1..........................377
Exercise 2 ........................ 378
Exercise 3 ........................ 378
Exercise 4 ........................380
Exercise 5 ........................ 383
Exercise 6 ........................ 384
Exercise 7 ........................ 384
Exercise 8 ........................ 385
Exercise 9 ........................ 386
Exercise 10 ...................... 387
Exercise 11 ....................... 387
Exercise 12 ...................... 388
Exercise 13....................... 388
Exercise 14 ...................... 389
Exercise 15....................... 390
Exercise 16 ...................... 392
Exercise 17....................... 395
Exercise 18 ...................... 396
Exercise 19 ...................... 397
Exercise 20...................... 398
Exercise 21 ...................... 399
Exercise 22 ...................... 401
Exercise 23 ...................... 402
Exercise 24 ...................... 403
Exercise 25 ...................... 404

Containers in Depth

407

Exercise 1......................... 407
Exercise 2 ........................408
Exercise 3 ........................ 409
Exercise 4 ........................ 409
Exercise 5 ........................ 409
Exercise 6 ......................... 411
Exercise 7 .........................413
Exercise 8 .........................414

Contents

xiii

Exercise 9 ........................ 418
Exercise 10 .......................419
Exercise 11 ....................... 424
Exercise 12 ...................... 425
Exercise 13....................... 426
Exercise 14 ...................... 428
Exercise 15....................... 429
Exercise 16 ...................... 430
Exercise 17....................... 434
Exercise 18 ...................... 434
Exercise 19 ...................... 435
Exercise 20...................... 436
Exercise 21 ...................... 439
Exercise 22 ...................... 440
Exercise 23 .......................441
Exercise 24 ...................... 443
Exercise 25 ...................... 445
Exercise 26 ...................... 449
Exercise 27 .......................451
Exercise 28...................... 453
Exercise 29 ...................... 458
Exercise 30...................... 462
Exercise 31....................... 464
Exercise 32 ...................... 466
Exercise 33 ...................... 467
Exercise 34 ...................... 472
Exercise 35 ...................... 474
Exercise 36 ...................... 476
Exercise 37 ...................... 481
Exercise 38 ...................... 484
Exercise 39 ...................... 486
Exercise 40...................... 489
Exercise 41 ...................... 492
Exercise 42 ...................... 494

I/O

497
Exercise 1......................... 497
Exercise 2 ........................ 498
Exercise 3 ........................ 499
Exercise 4 ........................500
Exercise 5 .........................501
Exercise 6 ........................ 502
Exercise 7 ........................ 503
Exercise 8 ........................ 504

xiv

Thinking in Java, 4th Edition Annotated Solution Guide

Exercise 9 ........................ 505
Exercise 10 ...................... 505
Exercise 11 ....................... 506
Exercise 12 ....................... 511
Exercise 13........................512
Exercise 14 .......................513
Exercise 15........................514
Exercise 16 .......................515
Exercise 17........................ 517
Exercise 18 .......................518
Exercise 19 ...................... 520
Exercise 20.......................521
Exercise 21 ...................... 522
Exercise 22 ...................... 522
Exercise 23 ...................... 524
Exercise 24 ...................... 525
Exercise 25 ...................... 525
Exercise 26 ...................... 530
Exercise 27 .......................531
Exercise 28...................... 533
Exercise 29 ...................... 535
Exercise 30...................... 537
Exercise 31....................... 540
Exercise 32 ...................... 543
Exercise 33 ...................... 544

Enumerated Types

547

Exercise 1......................... 547
Exercise 2 ........................ 548
Exercise 3 ........................ 549
Exercise 4 ........................ 550
Exercise 5 ........................ 552
Exercise 6 ........................ 554
Exercise 7 ........................ 554
Exercise 8 ........................ 555
Exercise 9 ........................ 559
Exercise 10 ...................... 562
Exercise 11 ....................... 567

Annotations

575

Exercise 1..........................575
Exercise 2 ........................ 578
Exercise 3 .........................581
Exercise 4 ........................ 584

Contents

xv

Exercise 5 ........................ 585
Exercise 6 ........................ 585
Exercise 7 ........................ 586
Exercise 8 ........................ 587
Exercise 9 ........................ 588
Exercise 10 ...................... 589
Exercise 11 ....................... 590

Concurrency

597

Exercise 1......................... 597
Exercise 2 ........................ 598
Exercise 3 ........................ 599
Exercise 4 ........................600
Exercise 5 ........................ 601
Exercise 6 ........................ 603
Exercise 7 ........................ 604
Exercise 8 ........................ 605
Exercise 9 ........................ 605
Exercise 10 ...................... 607
Exercise 11 .......................609
Exercise 12 ....................... 611
Exercise 13........................612
Exercise 14 .......................613
Exercise 15........................614
Exercise 16 .......................617
Exercise 17........................621
Exercise 18 ...................... 623
Exercise 19 ...................... 623
Exercise 20...................... 625
Exercise 21 ...................... 626
Exercise 22 ...................... 628
Exercise 23 ...................... 630
Exercise 24 ...................... 632
Exercise 25 ...................... 635
Exercise 26 ...................... 637
Exercise 27 ...................... 640
Exercise 28...................... 643
Exercise 29 ...................... 645
Exercise 30...................... 650
Exercise 31....................... 652
Exercise 32 ...................... 655
Exercise 33 ...................... 657
Exercise 34 ...................... 662
Exercise 35 ...................... 664

xvi

Thinking in Java, 4th Edition Annotated Solution Guide

Exercise 36 ...................... 668
Exercise 37 ...................... 676
Exercise 38 ...................... 682
Exercise 39 ...................... 687
Exercise 40......................690
Exercise 41 ...................... 692
Exercise 42 ...................... 694

Graphical
User Interfaces

697

Exercise 1......................... 697
Exercise 2 ........................ 697
Exercise 3 ........................ 698
Exercise 4 ........................ 699
Exercise 5 ........................ 700
Exercise 6 .........................701
Exercise 7 ........................ 702
Exercise 8 ........................ 704
Exercise 9 ........................ 705
Exercise 10 ...................... 708
Exercise 11 ....................... 709
Exercise 12 .......................710
Exercise 13........................712
Exercise 14 .......................714
Exercise 15........................ 715
Exercise 16 .......................716
Exercise 17........................718
Exercise 18 .......................719
Exercise 19 ...................... 720
Exercise 20...................... 724
Exercise 21 ...................... 726
Exercise 22 ...................... 728
Exercise 23 ...................... 729
Exercise 24 .......................731
Exercise 25 ...................... 734
Exercise 26 .......................737
Exercise 27 ...................... 738
Exercise 28...................... 740
Exercise 29 ...................... 742
Exercise 30...................... 743
Exercise 31....................... 744
Exercise 32 ...................... 745
Exercise 33 ...................... 746
Exercise 34 ...................... 749

Contents

xvii

Exercise 35 ....................... 751
Exercise 36 ....................... 751
Exercise 37 ...................... 752
Exercise 38 ...................... 753
Exercise 39 ...................... 753
Exercise 40...................... 754
Exercise 41 ...................... 754
Exercise 42 .......................755
Exercise 43 ...................... 758

xviii

Thinking in Java, 4th Edition Annotated Solution Guide

Installing the Code
Detailed instructions for installing, configuring and testing the
source code.
These instructions describe a Windows installation, but they will also act as a
guide for OSX and Linux installations.
These instructions also work with the free demo version of the guide.
1.

Create a directory called C:\TIJ4-Solutions\code.

2.

Using WinZip or some other zip utility (if one is not preinstalled, search
the web for a free utility), extract the zip file containing the code that you
received when you purchased the guide. Unzip it into the
C:\TIJ4-Solutions\code directory. When you’re done, you should see
numerous subdirectories in the C:\TIJ4-Solutions\code directory,
including subdirectories corresponding to the chapters in the solution
guide.

3.

Install the Java SE Development Kit (JDK), version 5 or newer, from the
download site at Sun (http://java.sun.com/javase/downloads/index.jsp).
You’ll also eventually want the documentation, which is available from the
same site.

4.

Set the CLASSPATH in your computer’s environment. For Windows
machines, right-click on the “My Computer” icon and select “Properties.”
Then select the “Advanced” tab and click the “Environment Variables”
button at the bottom. Under “System Variables,” look to see if there’s
already a “CLASSPATH” variable. If there is, double click it and add
;.;..;C:\TIJ4-Solutions\code;
to the end of the current entry.
If there is no “CLASSPATH” variable, click the “New” button and enter
CLASSPATH
In the “Variable name” box, and
.;..;C:\TIJ4-Solutions\code;
In the “Variable value” box, then click “OK”. To verify that your classpath
has been set, start a command prompt (see below), then enter set and
look for the CLASSPATH information in the output.

5.

Using the same technique as in Step 4, but for PATH instead of
CLASSPATH, add the bin directory from your Java installation into your

1

system’s PATH environment variable. On Windows, the default JDK
installation path is under “C:\Program Files” and because this has
spaces, you must quote that directory when adding it to the PATH:
C:\"Program Files"\Java\bin;
6.

2

Create a directory called C:\jars. Place the following files into this
directory:
•

javassist.jar (download here:
http://sourceforge.net/project/showfiles.php?group_id=22866; you
may need to search for it).

•

swt.jar from the Eclipse SWT library
(http://download.eclipse.org/eclipse/downloads/). Click on the most
recent build number, then scroll down to “SWT Binary and Source”
and select the file corresponding to your platform. Further details
about finding the jar file are in Thinking in Java, 4th Edition, under
the heading “Installing SWT.”

•

tools.jar, which is actually part of the JDK, but you must explicitly
add it to your classpath. You’ll find it in the lib directory wherever you
installed the JDK on your machine. (The default is C:\"Program
Files"\Java\lib).

•

javaws.jar, also part of the JDK, in the /jre/lib/ directory.

•

xom.jar, available from http://www.cafeconleche.org/XOM/.

7.

You must explicitly add each of the Jar files to your CLASSPATH,
following the directions in Step 4. However, you must also include the
name of the Jar file in the CLASSPATH entry. For example, after you put
the javassist.jar file into the C:\jars\ directory, the associated
CLASSPATH entry is C:\jars\javassist.jar;.

8.

Install the Ant 1.7 (or newer) build tool by following the instructions you
will find in the Ant download at http://ant.apache.org/.
Note: Ant is required in order to compile the examples in the book. Once
you successfully run ‘ant build’ in the root directory, you can also
compile each example individually (once you have the CLASSPATH set, as
described in Step 4) using the javac command-line compiler that was
installed when you completed the steps 3 and 5. To compile a file called
MyProgram.java, you type javac MyProgram.java.

9.

Start a command prompt in the C:\TIJ4- Solutions\code directory. To
do this in Windows, press the “Start” button, then select “Run” and type
“cmd” and press “OK.” then type

Thinking in Java, 4th Edition Annotated Solution Guide

cd C:\TIJ4-Solutions\code
into the resulting command window.
10.

At the prompt, type
ant build
The build should successfully compile all the chapters in the solution
guide.

11.

Once you’ve run ant build in the root directory, you can also move into
individual chapters and type ant (to compile and execute the code in that
chapter) or ant build (to compile the code only).

12.

This code is designed to work without an IDE, but it has also been tested
with Eclipse (free at http://www.eclipse.org/); see the following section
for instructions on how to use the code with Eclipse.
If you want to use this code inside other IDEs you might need to make
appropriate adjustments. Different IDEs have different requirements and
you might find it’s more trouble than it’s worth right now; instead, you
may want to begin with a more basic editor like JEdit (free at
http://www.jedit.org/).

13.

Note: The output for the programs has been verified for Java 6. Certain
programs (primarily those that use hashing) can produce different output
from one version to the next.

Using Eclipse
Once you’ve followed the above instructions, you can use the code inside the
Eclipse development environment as follows:
1.

Install Eclipse from http://www.eclipse.org/downloads/; choose a version
for Java developers and follow the installation instructions.

2.

Start Eclipse, then choose File | New | Java Project from the main menu.

3.

In the ensuing dialog box, under “Contents,” select “Create Project from
Existing Source.” Press the “Browse” button and navigate to C:\TIJ4Solutions\code. Enter “TIJ4-Solutions” as the project name and press
the “Finish” button.
Note: If you are installing the demo version of the solution guide, you do
not need to perform any of the following steps.

Installing the Code

3

4.

Eclipse will work for awhile and then present you with a “problems” pane
containing a lot of errors and warnings. We’ll remove the errors in the
following steps.

5.

In the “Package Explorer” pane, right click on “TIJ4-Solutions” and select
“Properties” (at the bottom). In the left column of the ensuing dialog box,
select “Java Build Path.”

6.

Select the “Source” tab. The default Eclipse configuration may have
chosen to exclude and include some files. Click on “Included” and press
the “Remove” button, then click on “Excluded” and press “Remove.”

7.

Click on “Excluded” and press “Edit.” Under “Exclusion Patterns,” add the
following files, which are not intended to compile. After you add the files,
press the “Finish” button.
•
•
•
•
•
•
•

4

access/E04_ForeignClass.java
arrays/E11_AutoboxingWithArrays.java
interfaces/E02_Abstract.java
reusing/E06_ChessWithoutDefCtor.java
reusing/E20_OverrideAnnotation.java
reusing/E21_FinalMethod.java
reusing/E22_FinalClass.java

8.

Click on the “Libraries” tab, then the “Add External Jars” button. Add
tools.jar, swt.jar, javassist.jar and xom.jar that are described in step
6 of the previous section.

9.

When you press OK to close the dialog box, the project should rebuild
without any errors. The warnings that you see refer to code that is
intentional for those solutions, in order to demonstrate features and
issues of the language.

Thinking in Java, 4th Edition Annotated Solution Guide

Packages & IDEs
When Java first appeared there was no Integrated
Development Environment (IDE) support, so you typically
used a text editor and the command-line compiler. Over the
years, IDE support has gotten so good (and many prevalent
IDEs are free) that it’s less and less likely that you’ll develop in
Java – or even learn the language – without an IDE.
There’s a conflict, however, between IDEs and the way that Thinking in Java
attempts to teach the language: One step at a time, using language features only
after they’ve been introduced.
An IDE like Eclipse (from www.Eclipse.org) likes to have all its code in packages
(later versions have become more tolerant of unpackaged code, but it still prefers
packages). Packages, however, are not introduced until the Access Control
chapter.
Because of the prevalence of IDEs, we have chosen to include package
statements for all the code in this book, even for chapters before Access Control.
If you have solved the problems in those chapters without using package
statements, your solutions are still correct.

5

Left to the Reader
We have left only a few exercises to the reader. For these, the solution typically
requires some configuration on your own computer.
The exercises left to the reader include:
•

Exercises 12 & 13 from the chapter Everything Is an Object

•

Exercise 13 from the chapter Initialization & Cleanup

•

Exercise 2 from the chapter Access Control

•

Exercise 15 from the chapter Generics

•

Exercise 8 from the chapter Arrays

•

Exercise 21 from the chapter Containers in Depth

•

Exercise 35, 36, 38, 39, and 43 from the chapter Graphical User Interfaces

7

Everything is an
Object
To satisfy IDEs like Eclipse, we have included package statements for chapters
before Access Control. If you have solved the problems in this chapter without
using package statements, your solutions are still correct.

Exercise 1
//: object/E01_DefaultInitialization.java
/****************** Exercise 1 *****************
* Create a class containing an int and a char
* that are not initialized. Print their values
* to verify that Java performs default
* initialization.
***********************************************/
package object;
public class E01_DefaultInitialization {
int i;
char c;
public E01_DefaultInitialization() {
System.out.println("i = " + i);
System.out.println("c = [" + c + ']');
}
public static void main(String[] args) {
new E01_DefaultInitialization();
}
} /* Output:
i = 0
c = [ ]
*///:~

When you run the program you’ll see that both variables are given default
values: 0 for the int, and a “space” for the char.

Exercise 2
//: object/E02_HelloWorld.java

9

/****************** Exercise 2 ******************
* Follow the HelloDate.java example in this
* chapter to create a "hello, world" program that
* simply displays that statement. You need only a
* single method in your class (the "main" one that
* executes when the program starts). Remember
* to make it static and to include the argument
* list (even though you don't use it).
* Compile the program with javac and run it using
* java. If you are using a different development
* environment than the JDK, learn how to compile
* and run programs in that environment.
************************************************/
package object;
public class E02_HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
} /* Output:
Hello, world!
*///:~

Exercise 3
//: object/E03_ATypeName.java
/****************** Exercise 3 ******************
* Turn the code fragments involving ATypeName
* into a program that compiles and
* runs.
************************************************/
package object;
public class E03_ATypeName {
public static void main(String[] args) {
E03_ATypeName a = new E03_ATypeName();
}
} ///:~

Exercise 4
//: object/E04_DataOnly.java
/****************** Exercise 4 ******************
* Turn the DataOnly code fragments into a
* program that compiles and runs.

10

Thinking in Java, 4th Edition Annotated Solution Guide

************************************************/
package object;
public class E04_DataOnly {
int i;
double d;
boolean b;
public static void main(String[] args) {
E04_DataOnly d = new E04_DataOnly();
d.i = 47;
d.d = 1.1;
d.b = false;
}
} ///:~

Exercise 5
//: object/E05_DataOnly2.java
/****************** Exercise 5 ******************
* Modify Exercise 4 so the values
* of the data in DataOnly are assigned to and
* printed in main().
************************************************/
package object;
public class E05_DataOnly2 {
public static void main(String[] args) {
E04_DataOnly d = new E04_DataOnly();
d.i = 47;
System.out.println("d.i = " + d.i);
d.d = 1.1;
System.out.println("d.d = " + d.d);
d.b = false;
System.out.println("d.b = " + d.b);
}
} /* Output:
d.i = 47
d.d = 1.1
d.b = false
*///:~

Exercise 6
//: object/E06_Storage.java
/****************** Exercise 6 ******************

Everything is an Object

11

* Write a program that includes and calls the
* storage() method defined as a code fragment in
* this chapter.
************************************************/
package object;
public class E06_Storage {
String s = "Hello, World!";
int storage(String s) {
return s.length() * 2;
}
void print() {
System.out.println("storage(s) = " + storage(s));
}
public static void main(String[] args) {
E06_Storage st = new E06_Storage();
st.print();
}
} /* Output:
storage(s) = 26
*///:~

Exercise 7
//: object/E07_Incrementable.java
/****************** Exercise 7 ******************
* Turn the Incrementable code fragments into a
* working program.
************************************************/
package object;
class StaticTest {
static int i = 47;
}
public class E07_Incrementable {
static void increment() { StaticTest.i++; }
public static void main(String[] args) {
E07_Incrementable sf = new E07_Incrementable();
sf.increment();
E07_Incrementable.increment();
increment();
}
} ///:~

12

Thinking in Java, 4th Edition Annotated Solution Guide

You can call increment( ) by itself, because a static method (main( ), in this
case) can call another static method without qualification.

Exercise 8
//: object/E08_StaticTest.java
/****************** Exercise 8 ******************
* Write a program to demonstrate that no
* matter how many objects you create of a
* particular class, there is only one instance
* of a particular static field in that class.
************************************************/
package object;
public class E08_StaticTest {
static int i = 47;
public static void main(String[] args) {
E08_StaticTest st1 = new E08_StaticTest();
E08_StaticTest st2 = new E08_StaticTest();
System.out.println(st1.i + " == " + st2.i);
st1.i++;
System.out.println(st1.i + " == " + st2.i);
}
} /* Output:
47 == 47
48 == 48
*///:~

The output shows that both instances of E08_StaticTest share the same static
field. We incremented the shared field through the first instance and the effect
was visible in the second instance.

Exercise 9
//: object/E09_AutoboxingTest.java
/****************** Exercise 9 ******************
* Write a program to demonstrate that
* autoboxing works for all the primitive types
* and their wrappers.
************************************************/
package object;
public class E09_AutoboxingTest {
public static void main(String[] args) {

Everything is an Object

13

Byte by = 1;
byte bt = by;
System.out.println("byte = " + bt);
Short sh = 1;
short s = sh;
System.out.println("short = " + s);
Integer in = 1;
int i = in;
System.out.println("int = " + i);
Long lo = 1L;
long l = lo;
System.out.println("long = " + l);
Boolean bo = true;
boolean b = bo;
System.out.println("boolean = " + b);
Character ch = 'x';
char c = ch;
System.out.println("char = " + c);
Float fl = 1.0f;
float f = fl;
System.out.println("float = " + f);
Double db = 1.0d;
double d = db;
System.out.println("double = " + d);
}
} /* Output:
byte = 1
short = 1
int = 1
long = 1
boolean = true
char = x
float = 1.0
double = 1.0
*///:~

The terms Autoboxing and Autounboxing appear often in the literature. The
only difference is the direction of the conversion: autoboxing converts from the
primitive type to the wrapper object, and autounboxing converts from the
wrapped type to the primitive type.

Exercise 10
//: object/E10_ShowArgs.java
// {Args: A B C}
/****************** Exercise 10 ****************

14

Thinking in Java, 4th Edition Annotated Solution Guide

* Write a program that prints three arguments
* taken from the command line.
* You'll need to index into the command-line
* array of Strings.
***********************************************/
package object;
public class E10_ShowArgs {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
}
} /* Output:
A
B
C
*///:~

Remember, when you want to get an argument from the command line:
•

Arguments are provided in a String array.

•

args[0] is the first command-line argument and not the name of the
program (as it is in C).

•

You’ll cause a runtime exception if you run the program without enough
arguments.

You can test for the length of the command-line argument array like this:
//: object/E10_ShowArgs2.java
// {Args: A B C}
package object;
public class E10_ShowArgs2 {
public static void main(String[] args) {
if(args.length < 3) {
System.err.println("Need 3 arguments");
System.exit(1);
}
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
}
} /* Output:
A
B

Everything is an Object

15

C
*///:~

System.exit( ) terminates the program and passes its argument back to the
operating system as a status code. (With most operating systems, a non-zero
status code indicates that the program execution failed.) Typically, you send
error messages to System.err, as shown above.

Exercise 11
//: object/E11_AllTheColorsOfTheRainbow.java
/****************** Exercise 11 *****************
* Turn the AllTheColorsOfTheRainbow example into
* a program that compiles and runs.
************************************************/
package object;
public class E11_AllTheColorsOfTheRainbow {
int anIntegerRepresentingColors;
void changeTheHueOfTheColor(int newHue) {
anIntegerRepresentingColors = newHue;
}
public static void main(String[] args) {
E11_AllTheColorsOfTheRainbow all =
new E11_AllTheColorsOfTheRainbow();
all.changeTheHueOfTheColor(27);
}
} ///:~

Exercise 12
//: object/E12_LeftToReader.java
/****************** Exercise 12 *****************
* Find the code for the second version of
* HelloDate.java, the simple comment* documentation example. Execute Javadoc on the
* file and view the results with your Web browser.
************************************************/
package object;
public class E12_LeftToReader {
public static void main(String args[]) {
System.out.println("Exercise left to reader");
}
} ///:~

16

Thinking in Java, 4th Edition Annotated Solution Guide

Note that Javadoc doesn’t automatically create the destination directory.
Consult the Javadoc reference in the JDK documentation to learn the many
uses of Javadoc.

Exercise 13
//: object/E13_LeftToReader.java
/****************** Exercise 13 *****************
* Run Documentation1.java, Documentation2.java,
* and Documentation3.java through Javadoc. Verify
* the resulting documentation with your Web
* browser.
************************************************/
package object;
public class E13_LeftToReader {
public static void main(String args[]) {
System.out.println("Exercise left to reader");
}
} ///:~

Exercise 14
//: object/E14_DocTest.java
/****************** Exercise 14 *****************
* Add an HTML list of items to the documentation
* in Exercise 13.
************************************************/
package object;
/** A class comment
* 
* System.out.println(new Date());
* 
*/ public class E14_DocTest { /** A variable comment */ public int i; /** A method comment * You can even insert a list: *
    *
  1. Item one *
  2. Item two *
  3. Item three Everything is an Object 17 *
*/ public void f() {} } ///:~ We simply added the HTML code fragments from the chapter examples. Exercise 15 //: object/E15_HelloWorldDoc.java /****************** Exercise 15 ***************** * Add comment documentation to the program in Exercise 2. * Extract it into an HTML file using Javadoc * and view it with your Web browser. ************************************************/ package object; /** A first example from sTIJ4. * Demonstrates the basic class * structure and the creation of a * main() method. */ public class E15_HelloWorldDoc { /** The main() method which is called when the program is executed by saying java E15_HelloWorldDoc. @param args array passed from the command-line */ public static void main(String[] args) { System.out.println("Hello, world!"); } } /* Output: Hello, world! *///:~ Exercise 16 //: object/E16_OverloadingDoc.java /****************** Exercise 16 ***************** * In the Initialization and Cleanup chapter, add * Javadoc documentation to the Overloading.java example. * Extract it into an HTML file using Javadoc * and view it with your Web browser. ************************************************/ package object; 18 Thinking in Java, 4th Edition Annotated Solution Guide /** Model of a single arboreal unit. */ class Tree { /** Current vertical aspect to the tip. */ int height; // 0 by default /** Plant a seedling. Assume height can be considered as zero. */ Tree() { System.out.println("Planting a seedling"); } /** Transplant an existing tree with a given height. */ Tree(int i) { System.out.println("Creating new Tree that is " + i + " feet tall"); height = i; } /** Produce information about this unit. */ void info() { System.out.println("Tree is " + height + " feet tall"); } /** Produce information with optional message. */ void info(String s) { System.out.println(s + ": Tree is " + height + " feet tall"); } } /** Simple test code for Tree class */ public class E16_OverloadingDoc { /** Creates Tree objects and exercises the two different info() methods. */ public static void main(String[] args) { for(int i = 0; i < 5; i++) { Tree t = new Tree(i); t.info(); t.info("overloaded method"); } // Overloaded constructor: new Tree(); } } /* Output: Creating new Tree that is 0 feet tall Tree is 0 feet tall overloaded method: Tree is 0 feet tall Creating new Tree that is 1 feet tall Tree is 1 feet tall overloaded method: Tree is 1 feet tall Everything is an Object 19 Creating new Tree that is 2 feet tall Tree is 2 feet tall overloaded method: Tree is 2 feet tall Creating new Tree that is 3 feet tall Tree is 3 feet tall overloaded method: Tree is 3 feet tall Creating new Tree that is 4 feet tall Tree is 4 feet tall overloaded method: Tree is 4 feet tall Planting a seedling *///:~ The one-argument constructor does not check the input argument, which should be greater than zero. Statements that control the execution flow of the program appear in a later chapter of TIJ4. 20 Thinking in Java, 4th Edition Annotated Solution Guide Operators To satisfy IDEs like Eclipse, we have included package statements for chapters before Access Control. If you have solved the problems in this chapter without using package statements, your solutions are still correct. Exercise 1 //: operators/E01_PrintStatements.java /****************** Exercise 1 ***************** * Write a program that uses the "short" and * normal form of print statement. ***********************************************/ package operators; import java.util.Date; import static net.mindview.util.Print.*; public class E01_PrintStatements { public static void main(String[] args) { Date currDate = new Date(); System.out.println("Hello, it's: " + currDate); print("Hello, it's: " + currDate); } } /* Output: (Sample) Hello, it's: Wed Mar 30 17:39:26 CEST 2005 Hello, it's: Wed Mar 30 17:39:26 CEST 2005 *///:~ Exercise 2 //: operators/E02_Aliasing.java /****************** Exercise 2 ***************** * Create a class containing a float and use it to * demonstrate aliasing. ***********************************************/ package operators; import static net.mindview.util.Print.*; class Integral { float f; } 21 public class E02_Aliasing { public static void main(String[] args) { Integral n1 = new Integral(); Integral n2 = new Integral(); n1.f = 9f; n2.f = 47f; print("1: n1.f: " + n1.f + ", n2.f: " + n2.f); n1 = n2; print("2: n1.f: " + n1.f + ", n2.f: " + n2.f); n1.f = 27f; print("3: n1.f: " + n1.f + ", n2.f: " + n2.f); } } /* Output: 1: n1.f: 9.0, n2.f: 47.0 2: n1.f: 47.0, n2.f: 47.0 3: n1.f: 27.0, n2.f: 27.0 *///:~ You can see the effect of aliasing after n2 is assigned to n1: they both point to the same object. Exercise 3 //: operators/E03_Aliasing2.java /****************** Exercise 3 ***************** * Create a class containing a float and use it * to demonstrate aliasing during method calls. ***********************************************/ package operators; import static net.mindview.util.Print.*; public class E03_Aliasing2 { static void f(Integral y) { y.f = 1.0f; } public static void main(String[] args) { Integral x = new Integral(); x.f = 2.0f; print("1: x.f: " + x.f); f(x); print("2: x.f: " + x.f); } } /* Output: 1: x.f: 2.0 2: x.f: 1.0 *///:~ 22 Thinking in Java, 4th Edition Annotated Solution Guide This exercise emphasizes that you’re always passing references around, thus you’re always aliasing. Even when you don’t actually see changes being made to the code you’re writing or the method you’re calling, that code or method could be calling other methods that modify the object. Exercise 4 //: operators/E04_Velocity.java // {Args: 30.5 3.2} /****************** Exercise 4 ***************** * Write a program that calculates velocity * using a constant distance and a constant time. ***********************************************/ package operators; public class E04_Velocity { public static void main(String[] args) { if(args.length < 2) { System.err.println( "Usage: java E04_Velocity distance time"); System.exit(1); } float distance = Float.parseFloat(args[0]); float time = Float.parseFloat(args[1]); System.out.print("Velocity = "); System.out.print(distance / time); // Change the next line if you want to use a different // unit for 'distance' System.out.println(" m/s"); } } /* Output: Velocity = 9.53125 m/s *///:~ Here we take the distance and time values from the command line. Arguments come in as a String array; if you need a float instead, use the static parseFloat( ) method of class Float. This can be difficult to locate using the JDK HTML documentation; you must remember either “parse” or that it’s part of class Float. Note the difference between System.out.print( ) and System.out.println( ); the latter terminates the current line by writing the line separator string. Operators 23 Exercise 5 //: operators/E05_Dogs.java /****************** Exercise 5 ***************** * Create a class called Dog with two Strings: * name and says. In main(), create two dogs, * "spot" who says, "Ruff!", and "scruffy" who * says, "Wurf!". Then display their names and * what they say. ***********************************************/ package operators; class Dog { String name; String says; } public class E05_Dogs { public static void main(String[] args) { Dog dog1 = new Dog(); Dog dog2 = new Dog(); dog1.name = "spot"; dog1.says = "ruff!"; dog2.name = "scruffy"; dog2.says = "wurf!"; System.out.println(dog1.name + " says " + dog1.says); System.out.println(dog2.name + " says " + dog2.says); } } /* Output: spot says ruff! scruffy says wurf! *///:~ This walks you through the basics of objects, and demonstrates that each object has its own distinct storage space. Exercise 6 //: operators/E06_DogsComparison.java /****************** Exercise 6 ***************** * Following Exercise 5 assign, a new Dog * reference to spot's object. Test for comparison * using == and equals() for all references. ***********************************************/ package operators; import static net.mindview.util.Print.*; 24 Thinking in Java, 4th Edition Annotated Solution Guide public class E06_DogsComparison { static void compare(Dog dog1, Dog dog2) { print("== on top references: " + (dog1 == dog2)); print( "equals() on top references: " + dog1.equals(dog2) ); print("== on names: " + (dog1.name == dog2.name)); print( "equals() on names: " + dog1.name.equals(dog2.name) ); print("== on says: " + (dog1.says == dog2.says)); print( "equals() on says: " + dog1.says.equals(dog2.says) ); } public static void main(String[] args) { Dog dog1 = new Dog(); Dog dog2 = new Dog(); Dog dog3 = dog1; // "Aliased" reference dog1.name = "spot"; dog1.says = "ruff!"; dog2.name = "scruffy"; dog2.says = "wurf!"; print("Comparing dog1 and dog2 objects..."); compare(dog1, dog2); print("\nComparing dog1 and dog3 objects..."); compare(dog1, dog3); print("\nComparing dog2 and dog3 objects..."); compare(dog2, dog3); } } /* Output: Comparing dog1 and dog2 objects... == on top references: false equals() on top references: false == on names: false equals() on names: false == on says: false equals() on says: false Comparing dog1 and dog3 objects... == on top references: true equals() on top references: true == on names: true equals() on names: true == on says: true equals() on says: true Comparing dog2 and dog3 objects... == on top references: false Operators 25 equals() on top references: false == on names: false equals() on names: false == on says: false equals() on says: false *///:~ Guess whether the following line compiles: print("== on top references: " + dog1 == dog2); Why or why not? (Hint: Read the Precedence and String operator + and += sections in TIJ4.) Apply the same reasoning to the next case and explain why the comparison always results in false: print("== on says: " + dog1.name == dog2.name); Exercise 7 //: operators/E07_CoinFlipping.java /****************** Exercise 7 ***************** * Write a program that simulates coin-flipping. ***********************************************/ package operators; import java.util.Random; public class E07_CoinFlipping { public static void main(String[] args) { Random rand = new Random(47); boolean flip = rand.nextBoolean(); System.out.print("OUTCOME: "); System.out.println(flip ? "HEAD" : "TAIL"); } } /* Output: OUTCOME: HEAD *///:~ This is partly an exercise in Standard Java Library usage. After familiarizing yourself with the HTML documentation for the JDK (downloadable from java.sun.com), select “R” at the JDK index to see various ways to generate random numbers. The program uses a ternary if-else operator to produce output. (See the Ternary if-else Operator section in TIJ4 for more information.) NOTE: You will normally create a Random object with no arguments to produce different output for each execution of the program. Otherwise it can 26 Thinking in Java, 4th Edition Annotated Solution Guide hardly be called a simulator. In this exercise and throughout the book, we use the seed value of 47 to make the output identical, thus verifiable, for each run. Exercise 8 //: operators/E08_LongLiterals.java /****************** Exercise 8 ***************** * Show that hex and octal notations work with long * values. Use Long.toBinaryString() to display * the results. ***********************************************/ package operators; import static net.mindview.util.Print.*; public class E08_LongLiterals { public static void main(String[] args) { long l1 = 0x2f; // Hexadecimal (lowercase) print("l1: " + Long.toBinaryString(l1)); long l2 = 0X2F; // Hexadecimal (uppercase) print("l2: " + Long.toBinaryString(l2)); long l3 = 0177; // Octal (leading zero) print("l3: " + Long.toBinaryString(l3)); } } /* Output: l1: 101111 l2: 101111 l3: 1111111 *///:~ Note that Long.toBinaryString( ) does not print leading zeroes. Exercise 9 //: operators/E09_MinMaxExponents.java /****************** Exercise 9 ***************** * Display the largest and smallest numbers for * both float and double exponential notation. ***********************************************/ package operators; import static net.mindview.util.Print.*; public class E09_MinMaxExponents { public static void main(String[] args) { print("Float MIN: " + Float.MIN_VALUE); print("Float MAX: " + Float.MAX_VALUE); Operators 27 print("Double MIN: " + Double.MIN_VALUE); print("Double MAX: " + Double.MAX_VALUE); } } /* Output: Float MIN: 1.4E-45 Float MAX: 3.4028235E38 Double MIN: 4.9E-324 Double MAX: 1.7976931348623157E308 *///:~ Exercise 10 //: operators/E10_BitwiseOperators.java /****************** Exercise 10 ***************** * Write a program with two constant values, one * with alternating binary ones and zeroes, with * a zero in the least-significant digit, and the * second, also alternating, with a one in the * least-significant digit. (Hint: It's easiest to * use hexadecimal constants for this.) Combine * these two values every way possible using the * bitwise operators. Display the results using * Integer.toBinaryString(). ************************************************/ package operators; import static net.mindview.util.Print.*; public class E10_BitwiseOperators { public static void main(String[] args) { int i1 = 0xaaaaaaaa; int i2 = 0x55555555; print("i1 = " + Integer.toBinaryString(i1)); print("i2 = " + Integer.toBinaryString(i2)); print("~i1 = " + Integer.toBinaryString(~i1)); print("~i2 = " + Integer.toBinaryString(~i2)); print("i1 & i1 = " + Integer.toBinaryString(i1 print("i1 | i1 = " + Integer.toBinaryString(i1 print("i1 ^ i1 = " + Integer.toBinaryString(i1 print("i1 & i2 = " + Integer.toBinaryString(i1 print("i1 | i2 = " + Integer.toBinaryString(i1 print("i1 ^ i2 = " + Integer.toBinaryString(i1 } } /* Output: i1 = 10101010101010101010101010101010 i2 = 1010101010101010101010101010101 ~i1 = 1010101010101010101010101010101 28 & | ^ & | ^ i1)); i1)); i1)); i2)); i2)); i2)); Thinking in Java, 4th Edition Annotated Solution Guide ~i2 = 10101010101010101010101010101010 i1 & i1 = 10101010101010101010101010101010 i1 | i1 = 10101010101010101010101010101010 i1 ^ i1 = 0 i1 & i2 = 0 i1 | i2 = 11111111111111111111111111111111 i1 ^ i2 = 11111111111111111111111111111111 *///:~ Note that Integer.toBinaryString( ) does not print leading zeroes. Exercise 11 //: operators/E11_SignedRightShift.java /****************** Exercise 11 ***************** * Start with a number that has a binary one in * the most significant position. (Hint: Use a * hexadecimal constant.) Use the signed * right-shift operator to right shift your * number through all its binary positions. * Display each result using Integer.toBinaryString(). ************************************************/ package operators; import static net.mindview.util.Print.*; public class E11_SignedRightShift { public static void main(String[] args) { int i = 0x80000000; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); i >>= 1; print(Integer.toBinaryString(i)); Operators 29 i i i i i i i i i i i i i i >>= >>= >>= >>= >>= >>= >>= >>= >>= >>= >>= >>= >>= >>= 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); } } /* Output: 10000000000000000000000000000000 11000000000000000000000000000000 11100000000000000000000000000000 11110000000000000000000000000000 11111000000000000000000000000000 11111100000000000000000000000000 11111110000000000000000000000000 11111111000000000000000000000000 11111111100000000000000000000000 11111111110000000000000000000000 11111111111000000000000000000000 11111111111100000000000000000000 11111111111110000000000000000000 11111111111111000000000000000000 11111111111111100000000000000000 11111111111111110000000000000000 11111111111111111000000000000000 11111111111111111100000000000000 11111111111111111110000000000000 11111111111111111111000000000000 11111111111111111111100000000000 11111111111111111111110000000000 11111111111111111111111000000000 11111111111111111111111100000000 11111111111111111111111110000000 11111111111111111111111111000000 11111111111111111111111111100000 11111111111111111111111111110000 11111111111111111111111111111000 11111111111111111111111111111100 11111111111111111111111111111110 30 Thinking in Java, 4th Edition Annotated Solution Guide 11111111111111111111111111111111 *///:~ This exercise required duplicating the line of code (the “brute force” approach). In the next chapter you learn to use looping structures to greatly reduce the amount of code you see above. Exercise 12 //: operators/E12_UnsignedRightShift.java /****************** Exercise 12 ***************** * Start with a number that is all binary ones. * Left shift it, then use the unsigned right-shift * operator to right shift through all of its binary * positions. Display each result using * Integer.toBinaryString(). ************************************************/ package operators; import static net.mindview.util.Print.*; public class E12_UnsignedRightShift { public static void main(String[] args) { int i = -1 << 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); i >>>= 1; print(Integer.toBinaryString(i)); Operators 31 i i i i i i i i i >>>= >>>= >>>= >>>= >>>= >>>= >>>= >>>= >>>= 1; 1; 1; 1; 1; 1; 1; 1; 1; print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); print(Integer.toBinaryString(i)); } } /* Output: 11111111111111111111111111111110 1111111111111111111111111111111 111111111111111111111111111111 11111111111111111111111111111 1111111111111111111111111111 111111111111111111111111111 11111111111111111111111111 1111111111111111111111111 111111111111111111111111 11111111111111111111111 1111111111111111111111 111111111111111111111 11111111111111111111 1111111111111111111 111111111111111111 11111111111111111 1111111111111111 111111111111111 11111111111111 1111111111111 111111111111 11111111111 1111111111 111111111 11111111 1111111 111111 11111 1111 111 11 1 *///:~ See the note for Exercise 11. 32 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 13 //: operators/E13_BinaryChar.java /****************** Exercise 13 ***************** * Write a method to display char values in * binary form. Demonstrate it using several * different characters. ************************************************/ package operators; import static net.mindview.util.Print.*; public class E13_BinaryChar { public static void main(String[] args) { print("A: " + Integer.toBinaryString('A')); print("!: " + Integer.toBinaryString('!')); print("x: " + Integer.toBinaryString('x')); print("7: " + Integer.toBinaryString('7')); } } /* Output: A: 1000001 !: 100001 x: 1111000 7: 110111 *///:~ Exercise 14 //: operators/E14_CompareStrings.java /****************** Exercise 14 ***************** * Write a method that compares two String arguments * using all the Boolean comparisons and print the * results. Perform the equals() test for the == and * !=. In main(), call your method with different * String objects. ************************************************/ package operators; public class E14_CompareStrings { public static void p(String s, boolean b) { System.out.println(s + ": " + b); } public static void compare(String lval, String rval) { System.out.println("lval: " + lval + " rval: " + rval); //! p("lval < rval: " + lval < rval); //! p("lval > rval: " + lval > rval); Operators 33 //! p("lval <= rval: " //! p("lval >= rval: " p("lval == rval", lval p("lval != rval", lval p("lval.equals(rval)", + lval <= rval); + lval >= rval); == rval); != rval); lval.equals(rval)); } public static void main(String[] args) { compare("Hello", "Hello"); // Force creation of separate object: String s = new String("Hello"); compare("Hello", s); compare("Hello", "Goodbye"); } } /* Output: lval: Hello rval: Hello lval == rval: true lval != rval: false lval.equals(rval): true lval: Hello rval: Hello lval == rval: false lval != rval: true lval.equals(rval): true lval: Hello rval: Goodbye lval == rval: false lval != rval: true lval.equals(rval): false *///:~ The only comparisons that actually compile are == and !=. This (slightly tricky) exercise highlights the critical difference between the == and != operators, which compare references, and equals( ), which actually compares content. Remember that quoted character arrays also produce references to String objects. In the first case, the compiler recognizes that the two strings actually contain the same values. Because String objects are immutable (you cannot change their contents), the compiler can merge the two String objects into one, so == returns true in that case. However, when you create a separate String s you also create a distinct object with the same contents, therefore the == returns false. The only reliable way to compare objects for equality is with equals( ). Be wary of any comparison that uses ==, which always and only compares two references to see if they are identical (that is, they point to the same object). 34 Thinking in Java, 4th Edition Annotated Solution Guide Controlling Execution To satisfy IDEs like Eclipse, we have included package statements for chapters before Access Control. If you have solved the problems in this chapter without using package statements, your solutions are still correct. Exercise 1 //: control/E01_To100.java /****************** Exercise 1 ****************** * Write a program to print values from one to * 100. ************************************************/ package control; public class E01_To100 { public static void main(String[] args) { for(int i = 1; i <= 100; i++) System.out.print(i + " "); } } /* Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 *///:~ 19 39 59 79 99 20 21 40 41 60 61 80 81 100 22 42 62 82 23 43 63 83 This is the most trivial use of a for loop. Try to simplify Exercises 11 and 12 in the previous chapter using a for loop. Exercise 2 //: control/E02_RandomInts.java /****************** Exercise 2 ****************** * Write a program to generate 25 random int * values. Use an if-else statement for each value * to classify it as greater than, less than, or * equal to a second randomly generated value. ************************************************/ package control; 35 import java.util.*; public class E02_RandomInts { static Random r = new Random(47); public static void compareRand() { int a = r.nextInt(); int b = r.nextInt(); System.out.println("a = " + a + ", b = " + b); if(a < b) System.out.println("a < b"); else if(a > b) System.out.println("a > b"); else System.out.println("a = b"); } public static void main(String[] args) { for(int i = 0; i < 25; i++) compareRand(); } } /* Output: a = -1172028779, b = 1717241110 a < b a = -2014573909, b = 229403722 a < b a = 688081923, b = -1812486437 a > b a = 809509736, b = 1791060401 a < b a = -2076178252, b = -1128074882 a < b a = 1150476577, b = -210207040 a > b a = 1122537102, b = 491149179 a > b a = 218473618, b = -1946952740 a > b a = -843035300, b = 865149722 a < b a = -1021916256, b = -1916708780 a > b a = -2016789463, b = 674708281 a < b a = -2020372274, b = 1703464645 a < b a = 2092435409, b = 1072754767 a > b a = -846991883, b = 488201151 36 Thinking in Java, 4th Edition Annotated Solution Guide a < b a = 100996820, b = -855894611 a > b a = -1612351948, b = 1891197608 a < b a = -56789395, b = 849275653 a < b a = 2078628644, b = -1099465504 a > b a = 39716067, b = 875665968 a < b a = 1738084688, b = -914835675 a > b a = 1169976606, b = 1947946283 a < b a = 691554276, b = -1004355271 a > b a = -541407364, b = 1920737378 a < b a = -1278072925, b = 281473985 a < b a = -1439435803, b = -955419343 a < b *///:~ In the solution above, we create a method that generates and compares the random numbers, then call that method 25 times. In your solution, you may have created all the code inline, inside main( ). Exercise 3 //: control/E03_RandomInts2.java // {RunByHand} /****************** Exercise 3 ****************** * Modify Exercise 2 so your code is * surrounded by an "infinite" while loop. It * will then run until you interrupt it, * typically with Control-C. ************************************************/ package control; public class E03_RandomInts2 { public static void main(String[] args) { while(true) E02_RandomInts.compareRand(); } Controlling Execution 37 } ///:~ A method outside of main( ) did most of the work in the previous exercise, so this solution requires only a minor change to main( ). Structure a program properly and it requires fewer code changes during its lifetime. The benefit may lie in reducing the maintenance costs of the software rather than the cost of the initial release, but a well-designed program is usually easier to get running in the first place. Exercise 4 //: control/E04_FindPrimes.java /****************** Exercise 4 ****************** * Write a program to detect and print prime numbers * (integers evenly divisible only by themselves * and 1), using two nested for loops and the * modulus operator (%). ************************************************/ package control; public class E04_FindPrimes { public static void main(String[] args) { int max = 100; // Get the max value from the command line, // if the argument has been provided: if(args.length != 0) max = Integer.parseInt(args[0]); for(int i = 1; i < max; i++) { boolean prime = true; for(int j = 2; j < i; j++) if(i % j == 0) prime = false; if(prime) System.out.print(i + " "); } } } /* Output: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 *///:~ Note that the program includes 1 as a prime number even though 2 is ordinarily considered the smallest prime. 38 Thinking in Java, 4th Edition Annotated Solution Guide One of the fastest ways of finding prime numbers is called the Sieve of Eratosthenes. The following program uses a boolean array to mark prime numbers. //: control/E04_FindPrimes2.java package control; import static java.lang.Math.*; import static net.mindview.util.Print.*; public class E04_FindPrimes2 { public static void main(String[] args) { int max = 100; // Get the max value from the command line, // if the argument has been provided: if(args.length != 0) max = Integer.parseInt(args[0]); boolean[] sieve = new boolean[max + 1]; int limit = (int)floor(sqrt(max)); printnb(1 + " "); if(max > 1) printnb(2 + " "); // Detect prime numbers for(int i = 3; i <= limit; i += 2) if(!sieve[i]) for(int j = 2 * i; j <= max; j += i) sieve[j] = true; // Print prime numbers for(int i = 3; i <= max; i += 2) if(!sieve[i]) printnb(i + " "); } } /* Output: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 *///:~ You need only test odd numbers, since 2 is the only even prime. The program uses a max + 1 sized array to make indexing easier. The search continues until ⎣ ⎦ you have crossed out all numbers divisible by max , where ⎣x ⎦ is the floor function. Additional optimizations are left as exercises. Exercise 5 //: control/E05_BitwiseOperators2.java Controlling Execution 39 /****************** Exercise 5 ****************** * Repeat Exercise 10 from the previous chapter, * but use the ternary operator and a bitwise test * instead of Integer.toBinaryString() to display * the ones and zeroes. ************************************************/ package control; import static net.mindview.util.Print.*; public class E05_BitwiseOperators2 { private static void toBinaryString(int i) { char[] buffer = new char[32]; int bufferPosition = 32; do { buffer[--bufferPosition] = ((i & 0x01) != 0) ? '1' : '0'; i >>>= 1; } while (i != 0); for(int j = bufferPosition; j < 32; j++) printnb(buffer[j]); print(); } public static void main(String[] args) { int i1 = 0xaaaaaaaa; int i2 = 0x55555555; printnb("i1 = "); toBinaryString(i1); printnb("i2 = "); toBinaryString(i2); printnb("~i1 = "); toBinaryString(~i1); printnb("~i2 = "); toBinaryString(~i2); printnb("i1 & i1 = "); toBinaryString(i1 printnb("i1 | i1 = "); toBinaryString(i1 printnb("i1 ^ i1 = "); toBinaryString(i1 printnb("i1 & i2 = "); toBinaryString(i1 printnb("i1 | i2 = "); toBinaryString(i1 printnb("i1 ^ i2 = "); toBinaryString(i1 } } /* Output: i1 = 10101010101010101010101010101010 i2 = 1010101010101010101010101010101 ~i1 = 1010101010101010101010101010101 ~i2 = 10101010101010101010101010101010 i1 & i1 = 10101010101010101010101010101010 i1 | i1 = 10101010101010101010101010101010 i1 ^ i1 = 0 i1 & i2 = 0 i1 | i2 = 11111111111111111111111111111111 i1 ^ i2 = 11111111111111111111111111111111 40 & | ^ & | ^ i1); i1); i1); i2); i2); i2); Thinking in Java, 4th Edition Annotated Solution Guide *///:~ The private static method toBinaryString( ) behaves like Integer.toBinaryString( ), using buffer to hold the binary digits because printing out the digits as encountered would produce an inverted output. Exercise 6 //: control/E06_RangeTest.java /****************** Exercise 6 ****************** * Modify the two test() methods in the previous * two programs so they take two extra * arguments, begin and end, and so testval is * tested to see if it is within the range between * (and including) begin and end. ************************************************/ package control; public class E06_RangeTest { static boolean test(int testval, int begin, int end) { boolean result = false; if(testval >= begin && testval <= end) result = true; return result; } public static void main(String[] args) { System.out.println(test(10, 5, 15)); System.out.println(test(5, 10, 15)); System.out.println(test(5, 5, 5)); } } /* Output: true false true *///:~ The test( ) methods are now only testing for two conditions, so we changed the return value to Boolean. Note that by using return in the following program, no intermediate result variable is necessary: //: control/E06_RangeTest2.java // No intermediate 'result' value necessary: package control; Controlling Execution 41 public class E06_RangeTest2 { static boolean test(int testval, int begin, int end) { if(testval >= begin && testval <= end) return true; return false; } public static void main(String[] args) { System.out.println(test(10, 5, 15)); System.out.println(test(5, 10, 15)); System.out.println(test(5, 5, 5)); } } /* Output: true false true *///:~ Exercise 7 //: control/E07_To98.java /****************** Exercise 7 ****************** * Modify Exercise 1 so the program exits by * using the break keyword at value 99. Try using * return instead. ************************************************/ package control; public class E07_To98 { public static void main(String[] args) { for(int i = 1; i <= 100; i++) { if(i == 99) break; System.out.print(i + " "); } } } /* Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 *///:~ 19 39 59 79 20 40 60 80 21 41 61 81 22 42 62 82 There is no observable difference whether you use break or return in this program. 42 Thinking in Java, 4th Edition Annotated Solution Guide 23 43 63 83 Exercise 8 This straightforward exercise demonstrates all the behaviors of switch. We wrote two programs, one with breaks and one without. Here’s the version with breaks: //: control/E08_SwitchDemo.java /****************** Exercise 8 ***************** * Create a switch statement inside a for loop * that tries each case and prints a message. Put * a break after each case and test it, then see * what happens when you remove the breaks. ***********************************************/ package control; public class E08_SwitchDemo { public static void main(String[] args) { for(int i = 0; i < 7; i++) switch(i) { case 1: System.out.println("case 1"); break; case 2: System.out.println("case 2"); break; case 3: System.out.println("case 3"); break; case 4: System.out.println("case 4"); break; case 5: System.out.println("case 5"); break; default: System.out.println("default"); } } } /* Output: default case 1 case 2 case 3 case 4 case 5 default *///:~ As a demonstration, we allowed the value of i to go out of bounds. You can see that anything that doesn’t match one of the cases goes to the default statement. Here’s the same program with the breaks removed: Controlling Execution 43 //: control/E08_SwitchDemo2.java // E08_SwitchDemo.java with the breaks removed. package control; public class E08_SwitchDemo2 { public static void main(String[] args) { for(int i = 0; i < 7; i++) switch(i) { case 1: System.out.println("case 1"); case 2: System.out.println("case 2"); case 3: System.out.println("case 3"); case 4: System.out.println("case 4"); case 5: System.out.println("case 5"); default: System.out.println("default"); } } } /* Output: default case 1 case 2 case 3 case 4 case 5 default case 2 case 3 case 4 case 5 default case 3 case 4 case 5 default case 4 case 5 default case 5 default default *///:~ Without the break, each case falls through to the next one. When you select case 1 you get all the other cases as well, so you’ll almost always want a break at the end of each case. 44 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 9 //: control/E09_Fibonacci.java // {Args: 20} /****************** Exercise 9 ********************** * A Fibonacci sequence is the sequence of numbers 1, * 1, 2, 3, 5, 8, 13, 21, 34, etc., where each * number (from the third on) is the sum of the previous * two. Create a method that takes an integer as an * argument and displays that many Fibonacci numbers * starting from the beginning. If, e.g., you run java * Fibonacci 5 (where Fibonacci is the name of the * class) the output will be: 1, 1, 2, 3, 5. ****************************************************/ package control; public class E09_Fibonacci { static int fib(int n) { if (n <= 2) return 1; return fib(n-1) + fib(n-2); } public static void main(String[] args) { // Get the max value from the command line: int n = Integer.parseInt(args[0]); if(n < 0) { System.out.println("Cannot use negative numbers"); return; } for(int i = 1; i <= n; i++) System.out.print(fib(i) + ", "); } } /* Output: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, *///:~ This problem, commonly presented in introductory programming classes, uses recursion, meaning that a function calls itself until it reaches a bottoming-out condition and returns. As an additional exercise, rewrite the solution without relying on recursion. (Hint: Use Binet’s formula for the nth Fibonacci number.) Controlling Execution 45 Exercise 10 //: control/E10_Vampire.java /****************** Exercise 10 ********************* * A vampire number has an even number of digits and * is formed by multiplying a pair of numbers containing * half the number of digits of the result. The digits * are taken from the original number in any order. * Pairs of trailing zeroes are not allowed. Examples * include: * 1260 = 21 * 60 * 1827 = 21 * 87 * 2187 = 27 * 81 * Write a program that finds all the 4-digit vampire * numbers. (Suggested by Dan Forhan.) ****************************************************/ package control; public class E10_Vampire { public static void main(String[] args) { int[] startDigit = new int[4]; int[] productDigit = new int[4]; for(int num1 = 10; num1 <= 99; num1++) for(int num2 = num1; num2 <= 99; num2++) { // Pete Hartley's theoretical result: // If x·y is a vampire number then // x·y == x+y (mod 9) if((num1 * num2) % 9 != (num1 + num2) % 9) continue; int product = num1 * num2; startDigit[0] = num1 / 10; startDigit[1] = num1 % 10; startDigit[2] = num2 / 10; startDigit[3] = num2 % 10; productDigit[0] = product / 1000; productDigit[1] = (product % 1000) / 100; productDigit[2] = product % 1000 % 100 / 10; productDigit[3] = product % 1000 % 100 % 10; int count = 0; for(int x = 0; x < 4; x++) for(int y = 0; y < 4; y++) { if(productDigit[x] == startDigit[y]) { count++; productDigit[x] = -1; startDigit[y] = -2; if(count == 4) 46 Thinking in Java, 4th Edition Annotated Solution Guide System.out.println(num1 + " * " + num2 + " : " + product); } } } } } /* Output: 15 * 93 : 1395 21 * 60 : 1260 21 * 87 : 1827 27 * 81 : 2187 30 * 51 : 1530 35 * 41 : 1435 80 * 86 : 6880 *///:~ The program does not produce duplicates, and is optimized by using Pete Hartley’s theoretical result (see the comment inside main( )). Controlling Execution 47 Initialization & Cleanup To satisfy IDEs like Eclipse, we have included package statements for chapters before Access Control. If you have solved the problems in this chapter without using package statements, your solutions are still correct. Exercise 1 //: initialization/E01_StringRefInitialization.java /****************** Exercise 01 **************** * Create a class with an uninitialized * String reference. Demonstrate that this * reference is initialized by Java to null. ***********************************************/ package initialization; public class E01_StringRefInitialization { String s; public static void main(String args[]) { E01_StringRefInitialization sri = new E01_StringRefInitialization(); System.out.println("sri.s = " + sri.s); } } /* Output: sri.s = null *///:~ Exercise 2 //: initialization/E02_StringInitialization.java /****************** Exercise 2 ***************** * Create a class with a String field initialized * at the point of definition, and another one * initialized by the constructor. What is the * difference between the two approaches? ***********************************************/ package initialization; 49 public class E02_StringInitialization { String s1 = "Initialized at definition"; String s2; public E02_StringInitialization(String s2i) { s2 = s2i; } public static void main(String args[]) { E02_StringInitialization si = new E02_StringInitialization( "Initialized at construction"); System.out.println("si.s1 = " + si.s1); System.out.println("si.s2 = " + si.s2); } } /* Output: si.s1 = Initialized at definition si.s2 = Initialized at construction *///:~ The s1 field is initialized before the constructor is entered; technically, so is the s2 field, which is set to null as the object is created. The more flexible s2 field lets you choose what value to give it when you call the constructor, whereas s1 always has the same value. Exercise 3 //: initialization/E03_DefaultConstructor.java /****************** Exercise 3 ****************** * Create a class with a default constructor (one * that takes no arguments) that prints a * message. Create an object of this class. ************************************************/ package initialization; public class E03_DefaultConstructor { E03_DefaultConstructor() { System.out.println("Default constructor"); } public static void main(String args[]) { new E03_DefaultConstructor(); } } /* Output: Default constructor *///:~ Here we create the E03_DefaultConstructor object for the side effects of the constructor call, so there is no need to create and hold a reference to the object. 50 Thinking in Java, 4th Edition Annotated Solution Guide In practice, when an operation doesn’t actually require an object, then a static utility method is more appropriate. (See The meaning of static section in TIJ4 for more information.) Exercise 4 //: initialization/E04_OverloadedConstructor.java /****************** Exercise 4 ***************** * Add an overloaded constructor to Exercise 3 that * takes a String argument and prints it along with * your message. ***********************************************/ package initialization; public class E04_OverloadedConstructor { E04_OverloadedConstructor() { System.out.println("Default constructor"); } E04_OverloadedConstructor(String s) { System.out.println("String arg constructor"); System.out.println(s); } public static void main(String args[]) { // Call default version: new E04_OverloadedConstructor(); // Call overloaded version: new E04_OverloadedConstructor("Overloaded"); } } /* Output: Default constructor String arg constructor Overloaded *///:~ Exercise 5 //: initialization/E05_OverloadedDog.java /****************** Exercise 5 ***************** * Create a class called Dog with an overloaded * bark() method. Your method should be * overloaded based on various primitive data * types, and should print different types of barking, * howling, etc., depending on which overloaded * version is called. Write a main() that calls Initialization & Cleanup 51 * all the different versions. ***********************************************/ package initialization; class Dog { public void bark() { System.out.println("Default bark!"); } public void bark(int i) { System.out.println("int bark = howl"); } public void bark(double d) { System.out.println("double bark = yip"); } // Etc. ... } public class E05_OverloadedDog { public static void main(String args[]) { Dog dog = new Dog(); dog.bark(); dog.bark(1); dog.bark(1.1); } } /* Output: Default bark! int bark = howl double bark = yip *///:~ As an additional challenge, write a class with a method boolean print(int) that prints a value and returns a boolean. Now overload the method to return a long. (This is similar to some questions on the Sun Java Certification Exam.) Exercise 6 //: initialization/E06_SwappedArguments.java /****************** Exercise 6 ****************** * Modify Exercise 5 so two of the overloaded * methods have two arguments of two different * types, but in reversed order relative to each * other. Verify that this works. ************************************************/ package initialization; class Dog2 { 52 Thinking in Java, 4th Edition Annotated Solution Guide public void bark(int i, double d) System.out.println("int, double } public void bark(double d, int i) System.out.println("double, int } { bark"); { bark"); } public class E06_SwappedArguments { public static void main(String args[]) { Dog2 dog = new Dog2(); dog.bark(1, 2.2); dog.bark(2.2, 1); } } /* Output: int, double bark double, int bark *///:~ Note that not only the type of arguments but also their order distinguish an overloaded method. In the example, the two versions of bark( ) are unique. Exercise 7 //: initialization/E07_SynthesizedConstructor.java /****************** Exercise 7 ****************** * Create a class without a constructor, then * create an object of that class in main() to * verify that the default constructor is * automatically synthesized. ************************************************/ package initialization; public class E07_SynthesizedConstructor { public static void main(String args[]) { // Call the synthesized default constructor // for this class: new E07_SynthesizedConstructor(); } } ///:~ Because it’s possible to call the constructor, you know it was created, even if you can’t see it. Initialization & Cleanup 53 Exercise 8 //: initialization/E08_ThisMethodCall.java /****************** Exercise 8 ***************** * Create a class with two methods. Within the * first method call the second method twice to * see it work, the first time without using this, * and the second time using this. * (You should not use this form in practice.) ***********************************************/ package initialization; public class E08_ThisMethodCall { public void a() { b(); this.b(); } public void b() { System.out.println("b() called"); } public static void main(String args[]) { new E08_ThisMethodCall().a(); } } /* Output: b() called b() called *///:~ This exercise shows that this refers to the current object. Use the this.b( ) style of method call only when necessary; otherwise you risk confusing the reader/maintainer of your code. Exercise 9 //: initialization/E09_ThisConstructorCall.java /****************** Exercise 9 ***************** * Create a class with two (overloaded) * constructors. Using this, call the second * constructor inside the first one. ***********************************************/ package initialization; public class E09_ThisConstructorCall { public E09_ThisConstructorCall(String s) { System.out.println("s = " + s); 54 Thinking in Java, 4th Edition Annotated Solution Guide } public E09_ThisConstructorCall(int i) { this("i = " + i); } public static void main(String args[]) { new E09_ThisConstructorCall("String call"); new E09_ThisConstructorCall(47); } } /* Output: s = String call s = i = 47 *///:~ Here’s a situation where you are forced to use the this keyword. Exercise 10 //: initialization/E10_FinalizeCall.java /****************** Exercise 10 ***************** * Create a class with a finalize() method that * prints a message. In main(), create an object * of your class. Explain the behavior of your * program. ************************************************/ package initialization; public class E10_FinalizeCall { protected void finalize() { System.out.println("finalize() called"); } public static void main(String args[]) { new E10_FinalizeCall(); } } ///:~ You probably won’t see the finalizer called because the program doesn’t usually generate enough garbage for the collector to run. Exercise 11 //: initialization/E11_FinalizeAlwaysCalled.java /****************** Exercise 11 **************** * Modify Exercise 10 so your * finalize() will always be called. ***********************************************/ Initialization & Cleanup 55 package initialization; public class E11_FinalizeAlwaysCalled { protected void finalize() { System.out.println("finalize() called"); } public static void main(String args[]) { new E11_FinalizeAlwaysCalled(); System.gc(); System.runFinalization(); } } /* Output: finalize() called *///:~ Calling System.gc( ) and System.runFinalization( ) in sequence will probably but not necessarily call your finalizer (The behavior of finalize has been uncertain from one version of JDK to another.) The call to these methods is just a request; it doesn’t ensure the finalizer will actually run. Ultimately, nothing guarantees that finalize( ) will be called. Exercise 12 //: initialization/E12_TankWithTerminationCondition.java /********************* Exercise 12 ******************** * Create a class called Tank that can be filled * and emptied, with a termination condition that it * must be empty when the object is cleaned up. * Write a finalize() that verifies this termination * condition. In main(), test the possible * scenarios that can occur when you use Tank. ******************************************************/ package initialization; class Tank { static int counter; int id = counter++; boolean full; public Tank() { System.out.println("Tank " + id + " created"); full = true; } public void empty() { full = false; } protected void finalize() { if(full) System.out.println( 56 Thinking in Java, 4th Edition Annotated Solution Guide "Error: tank " + id + " must be empty at cleanup"); else System.out.println("Tank " + id + " cleaned up OK"); } public String toString() { return "Tank " + id; } } public class E12_TankWithTerminationCondition { public static void main(String args[]) { new Tank().empty(); new Tank(); // Don't empty the second one System.gc(); // Force finalization? System.runFinalization(); } } /* Output: Tank 0 created Tank 1 created Error: tank 1 must be empty at cleanup Tank 0 cleaned up OK *///:~ We created no references to the two instances of type Tank, because those references would be in scope when System.gc( ) was called so they wouldn’t be cleaned up, thus they wouldn’t be finalized. Another option is to set references to zero when you want them to be garbage-collected. Modify the above example to try this method. You can never be sure finalizers will be called, so their utility is limited. A finalizer can, for example, check the state of objects when it does run, to ensure they’ve been cleaned up properly. Exercise 13 //: initialization/E13_LeftToReader.java /****************** Exercise 13 ***************** * Comment the line marked (1) in * ExplicitStatic.java and verify that the static * initialization clause is not called. Now * uncomment one of the lines marked (2) and * verify that the static initialization clause * is called. Finally, uncomment the other line marked * (2) and verify that static initialization * occurs only once. ************************************************/ package initialization; Initialization & Cleanup 57 public class E13_LeftToReader { public static void main(String args[]) { System.out.println("Exercise left to reader"); } } ///:~ Exercise 14 //: initialization/E14_StaticStringInitialization.java /****************** Exercise 14 ***************** * Create a class with a static String field that * is initialized at the point of definition, and * another one initialized by the static * block. Add a static method that prints both * fields and demonstrates that they are both * initialized before they are used. ************************************************/ package initialization; public class E14_StaticStringInitialization { static String s1 = "Initialized at definition"; static String s2; static { s2 = "Initialized in static block"; } public static void main(String args[]) { System.out.println("s1 = " + s1); System.out.println("s2 = " + s2); } } /* Output: s1 = Initialized at definition s2 = Initialized in static block *///:~ main( ) is a static method, so we used it to print the values. Exercise 15 //: initialization/E15_StringInstanceInitialization.java /****************** Exercise 15 **************** * Create a class with a String that is * initialized using "instance initialization." ***********************************************/ package initialization; public class E15_StringInstanceInitialization { 58 Thinking in Java, 4th Edition Annotated Solution Guide String s; { s = "'instance initialization'"; } public E15_StringInstanceInitialization() { System.out.println("Default constructor; s = " + s); } public E15_StringInstanceInitialization(int i) { System.out.println("int constructor; s = " + s); } public static void main(String args[]) { new E15_StringInstanceInitialization(); new E15_StringInstanceInitialization(1); } } /* Output: Default constructor; s = 'instance initialization' int constructor; s = 'instance initialization' *///:~ When you run this program, you’ll see that instance initialization occurs before either of the two constructors. Exercise 16 //: initialization/E16_StringArray.java /****************** Exercise 16 ***************** * Assign a string to each element of an array of * String objects. Print the array using a for loop. ************************************************/ package initialization; public class E16_StringArray { public static void main(String args[]) { // Doing it the hard way: String sa1[] = new String[4]; sa1[0] = "These"; sa1[1] = "are"; sa1[2] = "some"; sa1[3] = "strings"; for(int i = 0; i < sa1.length; i++) System.out.println(sa1[i]); // Using aggregate initialization to // make it easier: String sa2[] = { "These", "are", "some", "strings" }; for(int i = 0; i < sa2.length; i++) System.out.println(sa2[i]); Initialization & Cleanup 59 } } /* Output: These are some strings These are some strings *///:~ The solution shows both ways to solve the exercise: you can either create the array object and assign a string into each slot by hand, or use aggregate initialization, which creates and initializes the array object in a single step. Exercise 17 //: initialization/E17_ObjectReferences.java /****************** Exercise 17 ***************** * Create a class with a constructor that takes * a String argument. During construction, print * the argument. Create an array of object * references to this class, but don't * create objects to assign into the * array. When you run the program, notice * whether the initialization messages from the * constructor calls are printed. ************************************************/ package initialization; class Test { Test(String s) { System.out.println("String constructor; s = " + s); } } public class E17_ObjectReferences { // You can define the array as a field in the class: Test[] array1 = new Test[5]; public static void main(String args[]) { // Or as a temporary inside main: Test[] array2 = new Test[5]; } } ///:~ 60 Thinking in Java, 4th Edition Annotated Solution Guide This code creates only the array, not the objects that go into it. You don’t see initialization messages in Test’s constructors because no instances of that class exist. Exercise 18 //: initialization/E18_ObjectArray.java /****************** Exercise 18 ***************** * Create objects to attach to the array of * references for Exercise 17. ************************************************/ package initialization; public class E18_ObjectArray { public static void main(String args[]) { Test[] array = new Test[5]; for(int i = 0; i < array.length; i++) array[i] = new Test(Integer.toString(i)); } } /* Output: String constructor; s = 0 String constructor; s = 1 String constructor; s = 2 String constructor; s = 3 String constructor; s = 4 *///:~ Integer.toString( ) returns a String object representing the specified integer. Can you find a way to convert an integer into a String without using this utility method? (Hint: Recall previous discussions of the overloaded String operator +.) Exercise 19 //: initialization/E19_VarargStringArray.java /****************** Exercise 19 ***************** * Write a method that takes a vararg String * array. Verify that you can pass either a * comma-separated list of Strings or a * String[] into this method. ************************************************/ package initialization; public class E19_VarargStringArray { Initialization & Cleanup 61 static void printStrings(String... strs) { for(String s : strs) System.out.println(s); } public static void main(String args[]) { printStrings("These", "are", "some", "strings"); printStrings( new String[] { "These", "are", "some", "strings" } ); } } /* Output: These are some strings These are some strings *///:~ Exercise 20 //: initialization/E20_VarargMain.java // {Args: These, are, some, strings} /****************** Exercise 20 ***************** * Create a main() that uses varargs instead * of the ordinary main() syntax. Print all the * elements in the resulting args array. Test it * with various numbers of command-line * arguments. ************************************************/ package initialization; public class E20_VarargMain { public static void main(String... args) { E19_VarargStringArray.printStrings(args); } } /* Output: These, are, some, strings *///:~ 62 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 21 //: initialization/E21_PaperCurrencyTypesEnum.java /****************** Exercise 21 **************** * Create an enum of the six lowest denominations * of paper currency. Loop through the values() * and print each value and its ordinal(). ***********************************************/ package initialization; enum PaperCurrencyTypes { ONE, TWO, FIVE, TEN, TWENTY, FIFTY } public class E21_PaperCurrencyTypesEnum { public static void main(String args[]) { for(PaperCurrencyTypes s : PaperCurrencyTypes.values()) System.out.println(s + ", ordinal " + s.ordinal()); } } /* Output: ONE, ordinal 0 TWO, ordinal 1 FIVE, ordinal 2 TEN, ordinal 3 TWENTY, ordinal 4 FIFTY, ordinal 5 *///:~ Exercise 22 //: initialization/E22_PaperCurrencyTypesEnum2.java /****************** Exercise 22 **************** * Write a switch statement for the enum in * Exercise 21. For each case, output a * description of that particular currency. ***********************************************/ package initialization; import static net.mindview.util.Print.*; public class E22_PaperCurrencyTypesEnum2 { static void describe(PaperCurrencyTypes pct) { printnb(pct + " has a portrait of "); switch(pct) { case ONE: print("George Washington"); break; Initialization & Cleanup 63 case TWO: print("Thomas Jefferson"); break; case FIVE: print("Abraham Lincoln"); break; case TEN: print("Alexander Hamilton"); break; case TWENTY: print("Andrew Jackson"); break; case FIFTY: print("U.S. Grant"); break; } } public static void main(String args[]) { for(PaperCurrencyTypes s : PaperCurrencyTypes.values()) describe(s); } } /* Output: ONE has a portrait of George Washington TWO has a portrait of Thomas Jefferson FIVE has a portrait of Abraham Lincoln TEN has a portrait of Alexander Hamilton TWENTY has a portrait of Andrew Jackson FIFTY has a portrait of U.S. Grant *///:~ 64 Thinking in Java, 4th Edition Annotated Solution Guide Access Control Exercise 1 //: access/local/E01_PackagedClass.java /****************** Exercise 1 **************** * Create a class in a package. Create an * instance of your class outside of that package. ***********************************************/ package access.local; public class E01_PackagedClass { } ///:~ Create the above file in the access/local subdirectory. (Be sure the file is in a directory that starts at a CLASSPATH location, then continues into access/local.) Then create the following file in the access directory, which is above the local directory and thus outside of the access.local package: //: access/E01_ForeignClass.java package access; public class E01_ForeignClass { public static void main(String[] args) { new access.local.E01_PackagedClass(); } } ///:~ Exercise 2 //: access/E02_LeftToReader.java /****************** Exercise 2 ****************** * Turn the code fragments in the "Collisions" * section into a program, and verify that * collisions do in fact occur. ***********************************************/ package access; public class E02_LeftToReader { public static void main(String args[]) { System.out.println("Exercise left to reader"); } 65 } ///:~ Exercise 3 //: access/debug/E03_Debug.java /****************** Exercise 3 **************** * Create two packages: debug and debugoff, * containing an identical class with a debug() * method. The first version displays its String * argument to the console, the second does nothing. * Import the class into a test program * using a static import line, and demonstrate * the conditional compilation effect. ***********************************************/ package access.debug; public class E03_Debug { public static void debug(String msg) { System.out.println("Message: " + msg); } } ///:~ //: access/debugoff/E03_Debug.java package access.debugoff; public class E03_Debug { public static void debug(String msg) {} } ///:~ //: access/E03_DebugApp.java package access; import static access.debug.E03_Debug.*; public class E03_DebugApp { public static void main(String[] args) { debug("DEBUG VERSION"); } } /* Output: Message: DEBUG VERSION *///:~ //: access/E03_ReleaseApp.java package access; import static access.debugoff.E03_Debug.*; public class E03_ReleaseApp { public static void main(String[] args) { 66 Thinking in Java, 4th Edition Annotated Solution Guide debug("RELEASE VERSION"); } } ///:~ The release version E03_ReleaseApp prints no message. Exercise 4 //: access/local/E04_PackagedClass.java /****************** Exercise 4 **************** * Show that protected methods have package * access but are not public. ***********************************************/ package access.local; public class E04_PackagedClass { protected static void greeting() { System.out.println("Hello client programmer!"); } } ///:~ //: access/local/E04_ConsumerInSamePackage.java package access.local; public class E04_ConsumerInSamePackage { public static void main(String[] args) { E04_PackagedClass.greeting(); } } /* Output: Hello client programmer! *///:~ //: access/E04_ForeignClass.java // {CompileTimeError} to see results package access; public class E04_ForeignClass { public static void main(String[] args) { access.local.E04_PackagedClass.greeting(); } } ///:~ Explain why the compiler generates an error for E04_ForeignClass.java. Would making the E04_ForeignClass class part of the access.local package make a difference? Access Control 67 Solution: E04_PackagedClass is in its own package, and greeting( ) is not a public method so is generally unavailable outside of the package access.local. If E04_ForeignClass were included in access.local, it would share the same package as E04_PackagedClass.greeting( ), and so could access it. (See Exercises 6 & 9.) Exercise 5 //: access/local/E05_AccessControl.java /****************** Exercise 5 ****************** * Create a class with public, private, * protected, and package-access fields and * method members. Create an object of this class * and see what kind of compiler messages you get * when you try to access all the class members. * Remember that classes in the same directory * are part of the "default" package. ***********************************************/ package access.local; public class E05_AccessControl { public int a; private int b; protected int c; int d; // Package access public void f1() {} private void f2() {} protected void f3() {} void f4() {} // Package access public static void main(String args[]) { E05_AccessControl test = new E05_AccessControl(); // No problem accessing everything inside // of main() for this class, since main() // is a member and therefore has access: test.a = 1; test.b = 2; test.c = 3; test.d = 4; test.f1(); test.f2(); test.f3(); test.f4(); } } ///:~ 68 Thinking in Java, 4th Edition Annotated Solution Guide You can see that main( ) is a member and so has access to everything. If you create a separate class within the same package, that class cannot access the private members: //: access/local/E05_Other.java // A separate class in the same package cannot // access private elements: package access.local; public class E05_Other { public E05_Other() { E05_AccessControl test = new E05_AccessControl(); test.a = 1; //! test.b = 2; // Can't access: private test.c = 3; test.d = 4; test.f1(); //! test.f2(); // Can't access: private test.f3(); test.f4(); } } ///:~ When you create a class in a separate package (by either using a package statement or putting it in a different directory) then it can access only public members: //: access/E05_Other.java // A separate class in the other package cannot // access private, protected and package elements: package access; import access.local.E05_AccessControl; public class E05_Other { public E05_Other() { E05_AccessControl test = new E05_AccessControl(); test.a = 1; //! test.b = 2; // Can't access: private //! test.c = 3; // Can't access: protected //! test.d = 4; // Can't access: package test.f1(); //! test.f2(); // Can't access: private //! test.f3(); // Can't access: protected //! test.f4(); // Can't access: package } } ///:~ Access Control 69 Exercise 6 //: access/E06_ProtectedManipulation.java /****************** Exercise 6 ***************** * Create one class with protected data, and a * second class in the same file with a method * that manipulates that protected data. ***********************************************/ package access; class WithProtected { protected int i; } public class E06_ProtectedManipulation { public static void main(String args[]) { WithProtected wp = new WithProtected(); wp.i = 47; System.out.println("wp.i = " + wp.i); } } /* Output: wp.i = 47 *///:~ This exercise shows that protected also means “package access” (a.k.a. “friendly”). You can always access protected fields within the same package. As a further exercise, add a protected method to WithProtected and access it from within E06_ProtectedManipulation. Exercise 7 //: access/e07/E07_Widget.java /****************** Exercise 7 ***************** * Create the library according to the code * fragments describing access and Widget. Create * a Widget in a class that is not part of the * access package. ***********************************************/ package access.e07; import access.Widget; public class E07_Widget { public static void main(String args[]) { new Widget(); } 70 Thinking in Java, 4th Edition Annotated Solution Guide } /* Output: Making a Widget *///:~ //: access/Widget.java package access; public class Widget { public Widget() { System.out.println("Making a Widget"); } } ///:~ Exercise 8 //: access/E08_ConnectionManager.java /****************** Exercise 8 ***************** * Following the form of the example Lunch.java, * create a class called ConnectionManager that * manages a fixed array of Connection objects. * The client programmer must not be able to * create Connection objects, but only get them * via a static method in ConnectionManager. * ConnectionManager returns a null reference when * it runs out of objects. Test the classes in main(). ***********************************************/ package access; import access.connection.*; public class E08_ConnectionManager { public static void main(String args[]) { Connection c = ConnectionManager.getConnection(); while(c != null) { System.out.println(c); c.doSomething(); c = ConnectionManager.getConnection(); } } } /* Output: Connection 0 Connection 1 Connection 2 Connection 3 Connection 4 Connection 5 Connection 6 Access Control 71 Connection 7 Connection 8 Connection 9 *///:~ //: access/connection/Connection.java package access.connection; public class Connection { private static int counter = 0; private int id = counter++; Connection() {} public String toString() { return "Connection " + id; } public void doSomething() {} } ///:~ //: access/connection/ConnectionManager.java package access.connection; public class ConnectionManager { private static Connection[] pool = new Connection[10]; private static int counter = 0; static { for(int i = 0; i < pool.length; i++) pool[i] = new Connection(); } // Very simple -- just hands out each one once: public static Connection getConnection() { if(counter < pool.length) return pool[counter++]; return null; } } ///:~ The Connection class identifies each Connection object with a static int called counter, which produces the identifier as part of its toString( ) representation. Connection also has a doSomething( ) method to indicate the task for which you created the Connection object. Note that the constructor for Connection has package access; it is unavailable outside of this package, so the client programmer cannot access it to make instances of Connection directly. The only way to get Connection objects is through the ConnectionManager. 72 Thinking in Java, 4th Edition Annotated Solution Guide ConnectionManager initializes a static array of objects inside the static clause that is called only once when the class loads. (TIJ4 covers static clauses in detail.) Here is a more sophisticated connection manager that allows the client programmer to “check in” a connection when finished with it: //: access/E08_ConnectionManager2.java // Connections that can be checked in. package access; import access.connection2.*; public class E08_ConnectionManager2 { public static void main(String args[]) { Connection[] ca = new Connection[10]; // Use up all the connections for(int i = 0; i < 10; i++) ca[i] = ConnectionManager.getConnection(); // Should produce "null" since there are no // more connections: System.out.println(ConnectionManager.getConnection()); // Return connections, then get them out: for(int i = 0; i < 5; i++) { ca[i].checkIn(); Connection c = ConnectionManager.getConnection(); System.out.println(c); c.doSomething(); c.checkIn(); } } } /* Output: null Connection 0 Connection 0 Connection 0 Connection 0 Connection 0 *///:~ //: access/connection2/Connection.java package access.connection2; public class Connection { private static int counter = 0; private int id = counter++; Connection() {} public String toString() { Access Control 73 return "Connection " + id; } public void doSomething() {} public void checkIn() { ConnectionManager.checkIn(this); } } ///:~ //: access/connection2/ConnectionManager.java package access.connection2; public class ConnectionManager { private static Connection[] pool = new Connection[10]; static { for(int i = 0; i < pool.length; i++) pool[i] = new Connection(); } // Produce the first available connection: public static Connection getConnection() { for(int i = 0; i < pool.length; i++) if(pool[i] != null) { Connection c = pool[i]; pool[i] = null; // Indicates "in use" return c; } return null; // None left } public static void checkIn(Connection c) { for(int i = 0; i < pool.length; i++) if(pool[i] == null) { pool[i] = c; // Check it back in return; } } } ///:~ When a Connection is checked out, its slot in pool is set to null. When the client programmer is done with the connection, checkIn( ) returns it to the connection pool by assigning it to a null slot in pool. However, there are all kinds of potential problems with this approach. What if a client checks in a Connection and then continues to use it, or checks in more than once? We address the problem of the connection pool more thoroughly in Thinking in Patterns with Java (available from www.MindView.net). 74 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 9 (See TIJ4 for problem description.) Solution: PackagedClass is in its own package and is not a public class, so is unavailable outside of package access.local. If Foreign were also part of access.local, then it would be in the same package as PackagedClass and would have access to it. Access Control 75 Reusing Classes Exercise 1 //: reusing/E01_Composition.java /****************** Exercise 1 ***************** * Create a simple class. Inside a second class, * define a reference to an object of the first * class. Use lazy initialization to instantiate * this object. ***********************************************/ package reusing; import static net.mindview.util.Print.*; class Simple { String s; public Simple(String si) { s = si; } public String toString() { return s; } public void setString(String sNew) { s = sNew; } } class Second { Simple simple; String s; public Second(String si) { s = si; // 'simple' not initialized } public void check() { if(simple == null) print("not initialized"); else print("initialized"); } private Simple lazy() { if(simple == null) { print("Creating Simple"); simple = new Simple(s); } return simple; } public Simple getSimple() { return lazy(); } public String toString() { 77 return lazy().toString(); } public void setSimple(String sNew) { lazy().setString(sNew); } } public class E01_Composition { public static void main(String args[]) { Second second = new Second("Init String"); second.check(); print(second.getSimple()); second.check(); print(second); // toString() call second.setSimple("New String"); print(second); } } /* Output: not initialized Creating Simple Init String initialized Init String New String *///:~ The Simple class has some data and methods. The Second class performs lazy initialization through the lazy( ) method, which creates (if it hasn’t been) the Simple object and then returns it. The lazy( ) method is called by all the other methods to access the Simple object. We added print statements to show when initialization occurs, and that it happens only once. Exercise 2 //: reusing/E02_NewDetergent.java /****************** Exercise 2 ***************** * Inherit a new class from class Detergent. * Override scrub() and add a new method called * sterilize(). ***********************************************/ package reusing; class NewDetergent extends Detergent { public void scrub() { 78 Thinking in Java, 4th Edition Annotated Solution Guide append(" NewDetergent.scrub()"); super.scrub(); // Doesn't have to be first } public void sterilize() { append(" sterilize()"); } } public class E02_NewDetergent { public static void main(String args[]) { NewDetergent nd = new NewDetergent(); nd.dilute(); nd.scrub(); nd.sterilize(); System.out.println(nd); } } /* Output: Cleanser dilute() NewDetergent.scrub() Detergent.scrub() scrub() sterilize() *///:~ Exercise 3 //: reusing/E03_CartoonWithDefCtor.java /****************** Exercise 3 ****************** * Even if you don't create a constructor for * Cartoon(), the compiler will synthesize a * default constructor that calls the base-class * constructor. Prove that assertion. ***********************************************/ package reusing; class CartoonWithDefCtor extends Drawing { //! CartoonWithDefCtor() { //! System.out.println("CartoonWithDefCtor constructor"); //! } } public class E03_CartoonWithDefCtor { public static void main(String args[]) { new CartoonWithDefCtor (); } } /* Output: Art constructor Drawing constructor *///:~ Reusing Classes 79 We commented out the CartoonWithDefCtor constructor above. TIJ4 shows this output by printing in the constructor. The compiler synthesizes the default CartoonWithDefCtor constructor, in which it calls the base class Drawing default constructor, which in turn calls the base class Art default constructor. The compiler ensures that a constructor is called. If you don’t call a constructor, it calls the default constructor if available; however, a default constructor is not synthesized if you define any constructors but not the default. Exercise 4 //: reusing/E04_ConstructorOrder.java /****************** Exercise 4 ****************** * Prove that base-class constructors are (a) * always called and (b) called before * derived-class constructors. ***********************************************/ package reusing; class Base1 { public Base1() { System.out.println("Base1"); } } class Derived1 extends Base1 { public Derived1() { System.out.println("Derived1"); } } class Derived2 extends Derived1 { public Derived2() { System.out.println("Derived2"); } } public class E04_ConstructorOrder { public static void main(String args[]) { new Derived2(); } } /* Output: Base1 Derived1 Derived2 *///:~ 80 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 5 //: reusing/E05_SimpleInheritance.java /****************** Exercise 5 ***************** * Create classes A and B with default * constructors (empty argument lists) that * announce themselves. Inherit a new class * called C from A, and create a member of class * B inside C. Do not create a constructor for C. * Create an object of class C and observe the * results. ***********************************************/ package reusing; class A { public A() { System.out.println("A()"); } } class B { public B() { System.out.println("B()"); } } class C extends A { B b = new B(); } public class E05_SimpleInheritance { public static void main(String args[]) { new C(); } } /* Output: A() B() *///:~ Here the compiler synthesizes a constructor for C, first calling the base-class constructor, then the member object constructors. Exercise 6 //: reusing/E06_ChessWithoutDefCtor.java // {CompileTimeError} /****************** Exercise 6 ***************** * If you don’t call the base-class constructor * in BoardGame(), the compiler will respond Reusing Classes 81 * that it can’t find a constructor of the form * Game(). The call to the base-class * constructor must be the first thing you do * in the derived-class constructor. (The compiler * will remind you if you get it wrong.) * Use Chess.java to prove those assertions. ***********************************************/ package reusing; class ChessWithoutDefCtor extends BoardGame { //ChessWithoutDefCtor () { // System.out.println("ChessWithoutDefCtor constructor"); // super(11); //} } public class E06_ChessWithoutDefCtor { public static void main(String args[]) { new ChessWithoutDefCtor(); } } ///:~ BoardGame has no default constructor for ChessWithoutDefCtor, nor its own default constructor that the compiler can use to synthesize one, so the program won’t compile. BoardGame defines a constructor that takes an argument, so the compiler cannot generate a default. Moreover, if you uncomment the default constructor definition for ChessWithoutDefCtor, the compiler will demand that you first call the base-class constructor in the derived-class constructor. The {CompileTimeError} directive takes the compilation out of the build process because this compilation fails. Exercise 7 //: reusing/E07_SimpleInheritance2.java /****************** Exercise 7 ****************** * Modify Exercise 5 so A and B have * constructors with arguments instead of default * constructors. Write a constructor for C and * perform all initialization within it. ***********************************************/ package reusing; class A2 { public A2(String s) { System.out.println("A2(): " + s); } 82 Thinking in Java, 4th Edition Annotated Solution Guide } class B2 { public B2(String s) { System.out.println("2B(): " + s); } } class C2 extends A2 { B2 b; public C2(String s) { super(s); b = new B2(s); } } public class E07_SimpleInheritance2 { public static void main(String args[]) { new C2("Init string"); } } /* Output: A2(): Init string 2B(): Init string *///:~ We added the 2’s to keep class names in the same directory from clashing. Remember that super calls the base-class constructor and must be the first call in a derived-class constructor. Exercise 8 //: reusing/E08_CallBaseConstructor.java /****************** Exercise 8 ***************** * Create a base class with only a non-default * constructor, and a derived class with both a * default (no-arg) and non-default constructor. * Call the base-class constructor in the * derived-class constructors. ***********************************************/ package reusing; class BaseNonDefault { public BaseNonDefault(int i) {} } class DerivedTwoConstructors extends BaseNonDefault { public DerivedTwoConstructors() { super(47); } Reusing Classes 83 public DerivedTwoConstructors(int i) { super(i); } } public class E08_CallBaseConstructor { public static void main(String args[]) { new DerivedTwoConstructors(); new DerivedTwoConstructors(74); } } ///:~ Exercise 9 //: reusing/E09_ConstructorOrder2.java /****************** Exercise 9 ***************** * Create a class called Root and an instance of * each of three classes, Component1, Component2, and * Component3. Derive a class Stem from Root that * also contains an instance of each "component." * Default constructors for each class should * print a message about that class. ***********************************************/ package reusing; class Component1 { public Component1() { System.out.println("Component1"); } } class Component2 { public Component2() { System.out.println("Component2"); } } class Component3 { public Component3() { System.out.println("Component3"); } } class Root { Component1 c1 Component2 c2 Component3 c3 public Root() } = = = { new Component1(); new Component2(); new Component3(); System.out.println("Root"); } class Stem extends Root { Component1 c1 = new Component1(); Component2 c2 = new Component2(); Component3 c3 = new Component3(); 84 Thinking in Java, 4th Edition Annotated Solution Guide public Stem() { System.out.println("Stem"); } } public class E09_ConstructorOrder2 { public static void main(String args[]) { new Stem(); } } /* Output: Component1 Component2 Component3 Root Component1 Component2 Component3 Stem *///:~ Exercise 10 //: reusing/E10_ConstructorOrder3.java /****************** Exercise 10 ***************** * Modify Exercise 9 so each class only has * non-default constructors. ***********************************************/ package reusing; class Component1b { public Component1b(int i) { System.out.println("Component1b " + i); } } class Component2b { public Component2b(int i) { System.out.println("Component2b " + i); } } class Component3b { public Component3b(int i) { System.out.println("Component3b " + i); } } class Rootb { Reusing Classes 85 Component1b c1 = Component2b c2 = Component3b c3 = public Rootb(int new Component1b(1); new Component2b(2); new Component3b(3); i) { System.out.println("Rootb"); } } class Stemb extends Rootb { Component1b c1 = new Component1b(4); Component2b c2 = new Component2b(5); Component3b c3 = new Component3b(6); public Stemb(int i) { super(i); System.out.println("Stemb"); } } public class E10_ConstructorOrder3 { public static void main(String args[]) { new Stemb(47); } } /* Output: Component1b 1 Component2b 2 Component3b 3 Rootb Component1b 4 Component2b 5 Component3b 6 Stemb *///:~ We display the “Component” argument to clarify the order of constructor calls. Exercise 11 //: reusing/E11_Delegation.java /****************** Exercise 11 ***************** * Modify Detergent.java so it uses delegation. ***********************************************/ package reusing; import static net.mindview.util.Print.*; class DetergentDelegation { private Cleanser cleanser = new Cleanser(); // Delegated methods: public void append(String a) { cleanser.append(a); } 86 Thinking in Java, 4th Edition Annotated Solution Guide public void dilute() { cleanser.dilute(); } public void apply() { cleanser.apply(); } public String toString() { return cleanser.toString(); } public void scrub() { append(" DetergentDelegation.scrub()"); cleanser.scrub(); } public void foam() { append(" foam()"); } public static void main(String[] args) { DetergentDelegation x = new DetergentDelegation(); x.dilute(); x.apply(); x.scrub(); x.foam(); print(x); print("Testing base class:"); Cleanser.main(args); } } public class E11_Delegation { public static void main(String[] args) { DetergentDelegation.main(args); } } /* Output: Cleanser dilute() apply() DetergentDelegation.scrub() scrub() foam() Testing base class: Cleanser dilute() apply() scrub() *///:~ Exercise 12 //: reusing/E12_Dispose.java /****************** Exercise 12 ***************** * Add a proper hierarchy of dispose() methods to * all the classes in Exercise 9. ***********************************************/ package reusing; class Component1c { public Component1c(int i) { System.out.println("Component1c"); } public void dispose() { System.out.println("Component1c dispose"); Reusing Classes 87 } } class Component2c { public Component2c(int i) { System.out.println("Component2c"); } public void dispose() { System.out.println("Component2c dispose"); } } class Component3c { public Component3c(int i) { System.out.println("Component3c"); } public void dispose() { System.out.println("Component3c dispose"); } } class Rootc { Component1c c1 = new Component1c(1); Component2c c2 = new Component2c(2); Component3c c3 = new Component3c(3); public Rootc(int i) { System.out.println("Rootc"); } public void dispose() { System.out.println("Rootc dispose"); c3.dispose(); c2.dispose(); c1.dispose(); } } class Stemc extends Rootc { Component1c c1 = new Component1c(4); Component2c c2 = new Component2c(5); Component3c c3 = new Component3c(6); public Stemc(int i) { super(i); System.out.println("Stemc"); } public void dispose() { System.out.println("Stemc dispose"); c3.dispose(); c2.dispose(); c1.dispose(); 88 Thinking in Java, 4th Edition Annotated Solution Guide super.dispose(); } } public class E12_Dispose { public static void main(String args[]) { new Stemc(47).dispose(); } } /* Output: Component1c Component2c Component3c Rootc Component1c Component2c Component3c Stemc Stemc dispose Component3c dispose Component2c dispose Component1c dispose Rootc dispose Component3c dispose Component2c dispose Component1c dispose *///:~ Remember, it’s important to call the dispose( ) methods in the reverse order of initialization. Exercise 13 //: reusing/E13_InheritedOverloading.java /****************** Exercise 13 ***************** * Create a class with a method that is * overloaded three times. Inherit a new class, * add a new overloading of the method, and show * that all four methods are available in the * derived class. ***********************************************/ package reusing; class ThreeOverloads { public void f(int i) { System.out.println("f(int i)"); } Reusing Classes 89 public void f(char c) { System.out.println("f(char c)"); } public void f(double d) { System.out.println("f(double d)"); } } class MoreOverloads extends ThreeOverloads { public void f(String s) { System.out.println("f(String s)"); } } public class E13_InheritedOverloading { public static void main(String args[]) { MoreOverloads mo = new MoreOverloads(); mo.f(1); mo.f('c'); mo.f(1.1); mo.f("Hello"); } } /* Output: f(int i) f(char c) f(double d) f(String s) *///:~ Exercise 14 //: reusing/E14_ServicableEngine.java /****************** Exercise 14 ***************** * In Car.java add a service() method to Engine * and call this method in main(). ***********************************************/ package reusing; class ServicableEngine extends Engine { public void service() {} } class ServicableCar { public ServicableEngine engine = new ServicableEngine(); public Wheel[] wheel = new Wheel[4]; public Door 90 Thinking in Java, 4th Edition Annotated Solution Guide left = new Door(), right = new Door(); // 2-door public ServicableCar() { for(int i = 0; i < 4; i++) wheel[i] = new Wheel(); } } public class E14_ServicableEngine { public static void main(String[] args) { ServicableCar car = new ServicableCar(); car.left.window.rollup(); car.wheel[0].inflate(72); car.engine.service(); } } ///:~ Exercise 15 //: reusing/protect/E15_Protected.java /****************** Exercise 15 ***************** * Create a class with a protected method inside * a package. Try to call the protected method * outside the package, and explain the results. * Now inherit from your class and call the * protected method from inside a method of your * derived class. ***********************************************/ package reusing.protect; public class E15_Protected { protected void f() {} } ///:~ //: reusing/E15_ProtectedTest.java package reusing; import reusing.protect.*; class Derived extends E15_Protected { public void g() { f(); // Accessible in derived class } } public class E15_ProtectedTest { public static void main(String args[]) { Reusing Classes 91 //! new E15_Protected().f(); // Cannot access new Derived().g(); } } ///:~ Outside the package, you can only access a protected member inside an inherited class. Exercise 16 //: reusing/E16_Frog.java /****************** Exercise 16 ***************** * Create a class called Amphibian. From it, * inherit a class from it called Frog. Put * appropriate methods in the base class. In * main(), create a Frog, upcast it to Amphibian, * and demonstrate that all the methods still work. ***********************************************/ package reusing; class Amphibian { public void moveInWater() { System.out.println("Moving in Water"); } public void moveOnLand() { System.out.println("Moving on Land"); } } class Frog extends Amphibian {} public class E16_Frog { public static void main(String args[]) { Amphibian a = new Frog(); a.moveInWater(); a.moveOnLand(); } } /* Output: Moving in Water Moving on Land *///:~ Exercise 17 //: reusing/E17_Frog2.java 92 Thinking in Java, 4th Edition Annotated Solution Guide /****************** Exercise 17 ***************** * Modify Exercise 16 so Frog overrides the * method definitions from the base class * (provides new definitions using the same * method signatures). Note what happens in * main(). ***********************************************/ package reusing; class Frog2 extends Amphibian { public void moveInWater() { System.out.println("Frog swimming"); } public void moveOnLand() { System.out.println("Frog jumping"); } } public class E17_Frog2 { public static void main(String args[]) { Amphibian a = new Frog2(); a.moveInWater(); a.moveOnLand(); } } /* Output: Frog swimming Frog jumping *///:~ Since the compiler has a reference to an Amphibian, you might guess it will call the Amphibian methods. Instead, it calls the Frog2 methods. Since a is indeed a reference to a Frog2, this is the appropriate result. That’s polymorphism: The right behavior happens even if you are talking to a base-class reference. Exercise 18 //: reusing/E18_FinalFields.java /****************** Exercise 18 ***************** * Create a class with a static final field and a * final field and demonstrate the difference * between the two. ***********************************************/ package reusing; class SelfCounter { private static int count; Reusing Classes 93 private int id = count++; public String toString() { return "SelfCounter " + id; } } class WithFinalFields { final SelfCounter scf = new SelfCounter(); static final SelfCounter scsf = new SelfCounter(); public String toString() { return "scf = " + scf + "\nscsf = " + scsf; } } public class E18_FinalFields { public static void main(String args[]) { System.out.println("First object:"); System.out.println(new WithFinalFields()); System.out.println("Second object:"); System.out.println(new WithFinalFields()); } } /* Output: First object: scf = SelfCounter 1 scsf = SelfCounter 0 Second object: scf = SelfCounter 2 scsf = SelfCounter 0 *///:~ Because class loading initializes the static final, it has the same value in both instances of WithFinalFields, whereas the regular final’s values are different for each instance. Exercise 19 //: reusing/E19_BlankFinalField.java /****************** Exercise 19 ***************** * Create a class with a blank final reference to * an object. Perform initialization of the * blank final inside all constructors. * Demonstrate that the final must * be initialized before use, and cannot * be changed once initialized. ***********************************************/ package reusing; class WithBlankFinalField { 94 Thinking in Java, 4th Edition Annotated Solution Guide private final Integer i; // Without this constructor, you'll get a compiler error: // "variable i might not have been initialized" public WithBlankFinalField(int ii) { i = new Integer(ii); } public Integer geti() { // This won't compile. The error is: // "cannot assign a value to final variable i" // if(i == null) // i = new Integer(47); return i; } } public class E19_BlankFinalField { public static void main(String args[]) { WithBlankFinalField wbff = new WithBlankFinalField(10); System.out.println(wbff.geti()); } } /* Output: 10 *///:~ Exercise 20 //: reusing/E20_OverrideAnnotation.java // {CompileTimeError} /****************** Exercise 20 **************** * Show that the @Override annotation solves the * problem from the "final and private" section. ***********************************************/ package reusing; import static net.mindview.util.Print.*; class OverridingPrivateE20 extends WithFinals { @Override private final void f() { print("OverridingPrivateE20.f()"); } @Override private void g() { print("OverridingPrivateE20.g()"); } } class OverridingPrivate2E20 extends OverridingPrivateE20 { @Override public final void f() { Reusing Classes 95 print("OverridingPrivate2E20.f()"); } @Override public void g() { print("OverridingPrivate2E20.g()"); } } public class E20_OverrideAnnotation { public static void main(String[] args) { OverridingPrivate2E20 op2 = new OverridingPrivate2E20(); op2.f(); op2.g(); } } ///:~ Exercise 21 //: reusing/E21_FinalMethod.java // {CompileTimeError} /****************** Exercise 21 **************** * Create a class with a final method. Inherit * from that class and attempt to override that * method. ***********************************************/ package reusing; class WithFinalMethod { final void f() {} } public class E21_FinalMethod extends WithFinalMethod { void f() {} public static void main(String args[]) {} } ///:~ Exercise 22 //: reusing/E22_FinalClass.java // {CompileTimeError} /****************** Exercise 22 **************** * Create a final class and attempt to inherit * from it. ***********************************************/ package reusing; 96 Thinking in Java, 4th Edition Annotated Solution Guide final class FinalClass {} public class E22_FinalClass extends FinalClass { public static void main(String args[]) {} } ///:~ Exercise 23 //: reusing/E23_ClassLoading.java /****************** Exercise 23 **************** * Prove that class loading takes place only * once and may be caused by either the creation * of the first instance of that class or by * accessing a static member. ***********************************************/ package reusing; class LoadTest { // The static clause is executed // upon class loading: static { System.out.println("Loading LoadTest"); } static void staticMember() {} } public class E23_ClassLoading { public static void main(String args[]) { System.out.println("Calling static member"); LoadTest.staticMember(); System.out.println("Creating an object"); new LoadTest(); } } /* Output: Calling static member Loading LoadTest Creating an object *///:~ Now modify the code so object creation occurs before the static member call to see that object creation loads the object. Remember, a constructor is a static method, even though you don’t define it using the static keyword. Reusing Classes 97 Exercise 24 //: reusing/E24_JapaneseBeetle.java /****************** Exercise 24 **************** * In Beetle.java, inherit a specific type of * beetle from class Beetle, following the same * format as the existing classes. Trace and * explain the output. ***********************************************/ package reusing; import static net.mindview.util.Print.*; class JapaneseBeetle extends Beetle { int m = printInit("JapaneseBeetle.m initialized"); JapaneseBeetle() { print("m = " + m); print("j = " + j); } static int x3 = printInit("static JapaneseBeetle.x3 initialized"); } public class E24_JapaneseBeetle { public static void main(String args[]) { new JapaneseBeetle(); } } /* Output: static Insect.x1 initialized static Beetle.x2 initialized static JapaneseBeetle.x3 initialized i = 9, j = 0 Beetle.k initialized k = 47 j = 39 JapaneseBeetle.m initialized m = 47 j = 39 *///:~ Loading the class initializes the static variables. The base class loads first, then the next-derived class, and finally the most-derived class. This creates the object and initializes the non-static members, also starting at the root class. 98 Thinking in Java, 4th Edition Annotated Solution Guide Polymorphism Exercise 1 //: polymorphism/E01_Upcasting.java /****************** Exercise 1 **************** * Create a Cycle class, with subclasses * Unicycle, Bicycle, and Tricycle. Demonstrate * that an instance of each type can be upcast * to Cycle via a ride() method. ***********************************************/ package polymorphism; import polymorphism.cycle.*; public class E01_Upcasting { public static void ride(Cycle c) {} public static void main(String[] args) { ride(new Cycle()); // No upcasting ride(new Unicycle()); // Upcast ride(new Bicycle()); // Upcast ride(new Tricycle()); // Upcast } } ///:~ //: polymorphism/cycle/Cycle.java package polymorphism.cycle; public class Cycle { } ///:~ //: polymorphism/cycle/Unicycle.java package polymorphism.cycle; public class Unicycle extends Cycle { } ///:~ //: polymorphism/cycle/Bicycle.java package polymorphism.cycle; public class Bicycle extends Cycle { } ///:~ //: polymorphism/cycle/Tricycle.java package polymorphism.cycle; 99 public class Tricycle extends Cycle { } ///:~ We created the classes in individual files because they are used in a later example. Exercise 2 //: polymorphism/E02_Shapes.java /****************** Exercise 2 **************** * Add the @Override annotation to the shapes * example. ***********************************************/ package polymorphism; import polymorphism.shape.Shape; import polymorphism.oshape.*; public class E02_Shapes { private static RandomShapeGenerator gen = new RandomShapeGenerator(); public static void main(String[] args) { Shape[] shapes = new Shape[9]; // Fill up the array with shapes: for(int i = 0; i < shapes.length; i++) shapes[i] = gen.next(); // Make polymorphic method calls: for(Shape shape : shapes) shape.draw(); } } /* Output: Triangle.draw() Triangle.draw() Square.draw() Triangle.draw() Square.draw() Triangle.draw() Square.draw() Triangle.draw() Circle.draw() *///:~ //: polymorphism/oshape/RandomShapeGenerator.java // A "factory" that randomly creates shapes. package polymorphism.oshape; import polymorphism.shape.Shape; 100 Thinking in Java, 4th Edition Annotated Solution Guide import java.util.*; public class RandomShapeGenerator { private Random rand = new Random(47); public Shape next() { switch(rand.nextInt(3)) { default: case 0: return new Circle(); case 1: return new Square(); case 2: return new Triangle(); } } } ///:~ //: polymorphism/oshape/Square.java package polymorphism.oshape; import polymorphism.shape.Shape; import static net.mindview.util.Print.*; public class Square extends Shape { @Override public void draw() { print("Square.draw()"); } @Override public void erase() { print("Square.erase()"); } } ///:~ //: polymorphism/oshape/Triangle.java package polymorphism.oshape; import polymorphism.shape.Shape; import static net.mindview.util.Print.*; public class Triangle extends Shape { @Override public void draw() { print("Triangle.draw()"); } @Override public void erase() { print("Triangle.erase()"); } } ///:~ //: polymorphism/oshape/Circle.java package polymorphism.oshape; import polymorphism.shape.Shape; import static net.mindview.util.Print.*; public class Circle extends Shape { @Override public void draw() { print("Circle.draw()"); } @Override public void erase() { print("Circle.erase()"); } } ///:~ Polymorphism 101 Exercise 3 //: polymorphism/E03_NewShapeMethod.java /****************** Exercise 3 ***************** * Add a new method in the base class of * Shapes.java that prints a message, but don't * override it in the derived classes. Explain * what happens. Now override it in only one of the * derived classes and see what happens. Finally, * override it in all the derived classes. ***********************************************/ package polymorphism; import polymorphism.newshape.*; public class E03_NewShapeMethod { public static void main(String args[]) { Shape[] shapes = { new Circle(), new Square(), new Triangle(), }; // Make polymorphic method calls: for(Shape shape : shapes) { shape.draw(); shape.erase(); shape.msg(); } } } /* Output: Circle.draw() Circle.erase() Circle.msg() Square.draw() Square.erase() Square.msg() Triangle.draw() Triangle.erase() Triangle.msg() *///:~ //: polymorphism/newshape/Shape.java package polymorphism.newshape; import static net.mindview.util.Print.*; public class Shape { public void draw() {} public void erase() {} public void msg() { print("Base class msg()"); } 102 Thinking in Java, 4th Edition Annotated Solution Guide } ///:~ //: polymorphism/newshape/Circle.java package polymorphism.newshape; import static net.mindview.util.Print.*; public class Circle extends Shape { public void draw() { print("Circle.draw()"); } public void erase() { print("Circle.erase()"); } public void msg() { print("Circle.msg()"); } } ///:~ //: polymorphism/newshape/Square.java package polymorphism.newshape; import static net.mindview.util.Print.*; public class Square extends Shape { public void draw() { print("Square.draw()"); } public void erase() { print("Square.erase()"); } public void msg() { print("Square.msg()"); } } ///:~ //: polymorphism/newshape/Triangle.java package polymorphism.newshape; import static net.mindview.util.Print.*; public class Triangle extends Shape { public void draw() { print("Triangle.draw()"); } public void erase() { print("Triangle.erase()"); } public void msg() { print("Triangle.msg()"); } } ///:~ This final version overrides print( ) in all classes. Defining print( ) in only the base class results in: Circle.draw() Circle.erase() Base class msg() Square.draw() Square.erase() Base class msg() Triangle.draw() Triangle.erase() Base class msg() Nothing overrides the base-class definition, so it is used everywhere. Overriding print( ) in Circle produces: Polymorphism 103 Circle.draw() Circle.erase() Circle.msg() Square.draw() Square.erase() Base class msg() Triangle.draw() Triangle.erase() Base class msg() Anywhere we override, we use that version, as in Circle; otherwise we use the default base-class version. Exercise 4 //: polymorphism/E04_NewShapeType.java /****************** Exercise 4 ***************** * Add a new type of Shape to Shapes.java and * verify in main() that polymorphism works for * your new type as it does in the old types. ***********************************************/ package polymorphism; import polymorphism.newshape.*; public class E04_NewShapeType { public static void main(String args[]) { Shape[] shapes = { new Circle(), new Square(), new Triangle(), new Tetrahedron() }; // Make polymorphic method calls: for(Shape shape : shapes) { shape.draw(); shape.erase(); shape.msg(); } } } /* Output: Circle.draw() Circle.erase() Circle.msg() Square.draw() Square.erase() Square.msg() Triangle.draw() Triangle.erase() 104 Thinking in Java, 4th Edition Annotated Solution Guide Triangle.msg() Tetrahedron.draw() Tetrahedron.erase() Tetrahedron.msg() *///:~ //: polymorphism/newshape/Tetrahedron.java package polymorphism.newshape; import static net.mindview.util.Print.*; public class Tetrahedron extends Shape { public void draw() { print("Tetrahedron.draw()"); } public void erase() { print("Tetrahedron.erase()"); } public void msg() { print("Tetrahedron.msg()"); } } ///:~ The other shape definitions are in the same package so we just add the new shape and override the methods. The code in the for loop is unchanged from the previous example. Exercise 5 //: polymorphism/E05_Wheels.java /****************** Exercise 5 **************** * Starting from Exercise 1, add a wheels() * method in Cycle, which returns the number of * wheels. Modify ride() to call wheels() and * verify that polymorphism works. ***********************************************/ package polymorphism; import polymorphism.cycle2.*; public class E05_Wheels { public static void ride(Cycle c) { System.out.println("Num. of wheels: " + c.wheels()); } public static void main(String[] args) { ride(new Unicycle()); ride(new Bicycle()); ride(new Tricycle()); } } /* Output: Num. of wheels: 1 Num. of wheels: 2 Num. of wheels: 3 *///:~ Polymorphism 105 //: polymorphism/cycle2/Cycle.java package polymorphism.cycle2; public class Cycle { public int wheels() { return 0; } } ///:~ //: polymorphism/cycle2/Unicycle.java package polymorphism.cycle2; public class Unicycle extends Cycle { public int wheels() { return 1; } } ///:~ //: polymorphism/cycle2/Bicycle.java package polymorphism.cycle2; public class Bicycle extends Cycle { public int wheels() { return 2; } } ///:~ //: polymorphism/cycle2/Tricycle.java package polymorphism.cycle2; public class Tricycle extends Cycle { public int wheels() { return 3; } } ///:~ Cycle defines wheels( ) to return 0. In later chapters of TIJ4, you’ll learn better ways to specify wheels( ) than using a dummy implementation. Exercise 6 //: polymorphism/E06_MusicToString.java /****************** Exercise 6 ***************** * Change Music3.java so what() becomes the root * Object method toString(). Print the Instrument * objects using System.out.println() (without * any casting). ***********************************************/ package polymorphism; import polymorphism.music.Note; import static net.mindview.util.Print.*; class Instrument { void play(Note n) { print("Instrument.play() " + n); } public String toString() { return "Instrument"; } 106 Thinking in Java, 4th Edition Annotated Solution Guide void adjust() {} } class Wind extends Instrument { void play(Note n) { print("Wind.play() " + n); } public String toString () { return "Wind"; } } class Percussion extends Instrument { void play(Note n) { print("Percussion.play() " + n); } public String toString () { return "Percussion"; } } class Stringed extends Instrument { void play(Note n) { print("Stringed.play() " + n); } public String toString () { return "Stringed"; } } class Brass extends Wind { void play(Note n) { print("Brass.play() " + n); } void adjust() { print("Brass.adjust()"); } } class Woodwind extends Wind { void play(Note n) { print("Woodwind.play() " + n); } public String toString () { return "Woodwind"; } } public class E06_MusicToString { static Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind() }; public static void printAll(Instrument[] orch) { for(Instrument i : orch) System.out.println(i); } public static void main(String args[]) { printAll(orchestra); } } /* Output: Wind Percussion Stringed Polymorphism 107 Wind Woodwind *///:~ Exercise 7 //: polymorphism/E07_NewInstrument.java /****************** Exercise 7 ***************** * Add a new type of Instrument to Music3.java * and verify that polymorphism works for your * new type. ***********************************************/ package polymorphism; import polymorphism.music.Note; import static net.mindview.util.Print.*; class Electronic extends Instrument { void play(Note n) { print("Electronic.play() " + n); } public String toString() { return "Electronic"; } } public class E07_NewInstrument { static Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind(), new Electronic() }; public static void main(String args[]) { for(Instrument i : orchestra) { i.play(Note.MIDDLE_C); i.adjust(); print(i); } } } /* Output: Wind.play() MIDDLE_C Wind Percussion.play() MIDDLE_C Percussion Stringed.play() MIDDLE_C Stringed Brass.play() MIDDLE_C Brass.adjust() 108 Thinking in Java, 4th Edition Annotated Solution Guide Wind Woodwind.play() MIDDLE_C Woodwind Electronic.play() MIDDLE_C Electronic *///:~ Exercise 8 //: polymorphism/E08_RandomInstruments.java /****************** Exercise 8 ***************** * Modify Music3.java so it randomly creates * Instrument objects the way Shapes.java does. ***********************************************/ package polymorphism; class InstrumentGenerator { java.util.Random gen = new java.util.Random(47); public Instrument next() { switch(gen.nextInt(6)) { default: case 0: return new Wind(); case 1: return new Percussion(); case 2: return new Stringed(); case 3: return new Brass(); case 4: return new Woodwind(); case 5: return new Electronic(); } } } public class E08_RandomInstruments { public static void main(String args[]) { InstrumentGenerator gen = new InstrumentGenerator(); for(int i = 0; i < 20; i++) System.out.println(gen.next()); } } /* Output: Stringed Electronic Percussion Electronic Percussion Electronic Woodwind Stringed Polymorphism 109 Wind Percussion Wind Wind Wind Percussion Electronic Woodwind Woodwind Percussion Stringed Woodwind *///:~ A generator produces a new value each time you call it, though you don’t give it any parameters. The generator nextInt( ) produces a random value within the array between zero (inclusive) and the value of the argument (exclusive). Managing the case statement with a random generator can be error-prone. However, features of the Class object enable us to index an array that itself generates various instruments, as in this more elegant solution: //: polymorphism/E08_RandomInstruments2.java // A more sophisticated solution using features // you'll learn about in later chapters. package polymorphism; class InstrumentGenerator2 { java.util.Random gen = new java.util.Random(47); Class instruments[] = { Wind.class, Percussion.class, Stringed.class, Brass.class, Woodwind.class, Electronic.class, }; public Instrument next() { try { int idx = Math.abs(gen.nextInt(instruments.length)); return (Instrument) instruments[idx].newInstance(); } catch(Exception e) { throw new RuntimeException("Cannot Create", e); } } } 110 Thinking in Java, 4th Edition Annotated Solution Guide public class E08_RandomInstruments2 { public static void main(String args[]) { InstrumentGenerator2 gen = new InstrumentGenerator2(); for(int i = 0; i < 20; i++) System.out.println(gen.next()); } } /* Output: Stringed Electronic Percussion Electronic Percussion Electronic Woodwind Stringed Wind Percussion Wind Wind Wind Percussion Electronic Woodwind Woodwind Percussion Stringed Woodwind *///:~ The .class syntax in the array definition produces references to Class objects for each type of instrument. Class.newInstance( ) creates an object of the class it is called for, but it can throw exceptions. Here, we create and throw a RuntimeException for this programming error, so your code doesn’t have to catch such exceptions. You can embed the cause of an error inside a thrown exception to pass detailed information about the condition to a client programmer. (We cover exceptions in detail in the chapter Error Handling with Exceptions.) The benefit of this design is that you can add a new type to the system by only adding it to the Class array; the rest of the code takes care of itself. Exercise 9 //: polymorphism/E09_Rodents.java /****************** Exercise 9 ***************** * Create an inheritance hierarchy of Rodent: Polymorphism 111 * Mouse, Gerbil, Hamster, etc. In the base * class, provide methods that are common to all * Rodents, and override these in the derived * classes to perform different behaviors * depending on the specific type of Rodent. * Create an array of Rodent, fill it with * different specific types of Rodents, and call * your base-class methods to see what happens. ***********************************************/ package polymorphism; import static net.mindview.util.Print.*; class Rodent { public void hop() { print("Rodent hopping"); } public void scurry() { print("Rodent scurrying"); } public void reproduce() { print("Making more Rodents"); } public String toString() { return "Rodent"; } } class Mouse extends Rodent { public void hop() { print("Mouse hopping"); } public void scurry() { print("Mouse scurrying"); } public void reproduce() { print("Making more Mice"); } public String toString() { return "Mouse"; } } class Gerbil extends Rodent { public void hop() { print("Gerbil hopping"); } public void scurry() { print("Gerbil scurrying"); } public void reproduce() { print("Making more Gerbils"); } public String toString() { return "Gerbil"; } } class Hamster extends Rodent { public void hop() { print("Hamster hopping"); } public void scurry() { print("Hamster scurrying"); } public void reproduce() { print("Making more Hamsters"); } public String toString() { return "Hamster"; } } public class E09_Rodents { public static void main(String args[]) { Rodent[] rodents = { new Mouse(), new Gerbil(), new Hamster(), }; 112 Thinking in Java, 4th Edition Annotated Solution Guide for(Rodent r : rodents) { r.hop(); r.scurry(); r.reproduce(); print(r); } } } /* Output: Mouse hopping Mouse scurrying Making more Mice Mouse Gerbil hopping Gerbil scurrying Making more Gerbils Gerbil Hamster hopping Hamster scurrying Making more Hamsters Hamster *///:~ Exercise 10 //: polymorphism/E10_MethodCalls.java /****************** Exercise 10 ***************** * Create a base class with two methods. In the * first method, call the second method. Inherit * a class and override the second method. Create * an object of the derived class, upcast it to * the base type, and call the first method. * Explain what happens. ***********************************************/ package polymorphism; class TwoMethods { public void m1() { System.out.println("Inside m1, calling m2"); m2(); } public void m2() { System.out.println("Inside m2"); } } class Inherited extends TwoMethods { Polymorphism 113 public void m2() { System.out.println("Inside Inherited.m2"); } } public class E10_MethodCalls { public static void main(String args[]) { TwoMethods x = new Inherited(); x.m1(); } } /* Output: Inside m1, calling m2 Inside Inherited.m2 *///:~ The first method isn’t overridden, but it calls the second method, which is. Java always uses the most-derived method for the object type; this is very powerful (and may surprise the unaware). The Template Method design pattern makes heavy use of polymorphism. (See Thinking in Patterns with Java at www.MindView.net.) Exercise 11 //: polymorphism/E11_Pickle.java /****************** Exercise 11 ***************** * Add class Pickle to Sandwich.java. ***********************************************/ package polymorphism; import static net.mindview.util.Print.*; class Pickle { Pickle() { print("Pickle()"); } } class Sandwich2 extends PortableLunch { Bread b = new Bread(); Cheese c = new Cheese(); Lettuce l = new Lettuce(); Pickle p = new Pickle(); Sandwich2() { print("Sandwich()"); } } public class E11_Pickle { public static void main(String args[]) { new Sandwich2(); } 114 Thinking in Java, 4th Edition Annotated Solution Guide } /* Output: Meal() Lunch() PortableLunch() Bread() Cheese() Lettuce() Pickle() Sandwich() *///:~ Notice that Pickle appears in the correct order, after the other member objects. Exercise 12 //: polymorphism/E12_RodentInitialization.java /****************** Exercise 12 ***************** * Modify Exercise 9 so it demonstrates the * order of initialization of the base classes * and derived classes. Now add member objects to * both the base and derived classes, and show * the order in which their initialization occurs * during construction. ***********************************************/ package polymorphism; import static net.mindview.util.Print.*; class Member { public Member(String id) { print("Member constructor " + id); } } class Rodent2 { Member m1 = new Member("r1"), m2 = new Member("r2"); public Rodent2() { print("Rodent constructor"); } public void hop() { print("Rodent hopping"); } public void scurry() { print("Rodent scurrying"); } public void reproduce() { print("Making more Rodents"); } public String toString() { return "Rodent"; } } class Mouse2 extends Rodent2 { Member m1 = new Member("m1"), m2 = new Member("m2"); public Mouse2() { print("Mouse constructor"); } public void hop() { print("Mouse hopping"); } Polymorphism 115 public void scurry() { print("Mouse scurrying"); } public void reproduce() { print("Making more Mice"); } public String toString() { return "Mouse"; } } class Gerbil2 extends Rodent2 { Member m1 = new Member("g1"), m2 = new Member("g2"); public Gerbil2() { print("Gerbil constructor"); } public void hop() { print("Gerbil hopping"); } public void scurry() { print("Gerbil scurrying"); } public void reproduce() { print("Making more Gerbils"); } public String toString() { return "Gerbil"; } } class Hamster2 extends Rodent2 { Member m1 = new Member("h1"), m2 = new Member("h2"); public Hamster2() { print("Hamster constructor"); } public void hop() { print("Hamster hopping"); } public void scurry() { print("Hamster scurrying"); } public void reproduce() { print("Making more Hamsters"); } public String toString() { return "Hamster"; } } public class E12_RodentInitialization { public static void main(String args[]) { new Hamster2(); } } /* Output: Member constructor r1 Member constructor r2 Rodent constructor Member constructor h1 Member constructor h2 Hamster constructor *///:~ We initialize the base class first, starting with the member objects in order of definition, then the derived class, starting with its member objects. Exercise 13 //: polymorphism/E13_VerifiedRefCounting.java /****************** Exercise 13 ***************** * Add a finalize() method to ReferenceCounting.java * to verify the termination condition. (See * the Initialization & Cleanup chapter.) 116 Thinking in Java, 4th Edition Annotated Solution Guide ***********************************************/ package polymorphism; import static net.mindview.util.Print.*; class Shared { private int refcount = 0; private static int counter = 0; private int id = counter++; public Shared() { print("Creating " + this); } public void addRef() { refcount++; } protected void dispose() { if(--refcount == 0) print("Disposing " + this); } protected void finalize() { if(refcount != 0) print("Error: object is not properly cleaned-up!"); } public String toString() { return "Shared " + id; } } class Composing { private Shared shared; private static int counter = 0; private int id = counter++; public Composing(Shared shared) { print("Creating " + this); this.shared = shared; this.shared.addRef(); } protected void dispose() { print("disposing " + this); shared.dispose(); } public String toString() { return "Composing " + id; } } public class E13_VerifiedRefCounting { public static void main(String[] args) { Shared shared = new Shared(); Composing[] composing = { new Composing(shared), new Composing(shared), new Composing(shared), new Composing(shared), new Composing(shared) }; for(Composing c : composing) c.dispose(); Polymorphism 117 System.gc(); // Verify failure: new Composing(new Shared()); System.gc(); } } /* Output: Creating Shared 0 Creating Composing 0 Creating Composing 1 Creating Composing 2 Creating Composing 3 Creating Composing 4 disposing Composing 0 disposing Composing 1 disposing Composing 2 disposing Composing 3 disposing Composing 4 Disposing Shared 0 Creating Shared 1 Creating Composing 5 Error: object is not properly cleaned-up! *///:~ We kept our last Composing( ) object alive, so you can see the termination condition report the mistake. Exercise 14 //: polymorphism/E14_SharedRodentInitialization.java /****************** Exercise 14 ***************** * Modify Exercise 12 so one of the member * objects is a shared object with reference * counting, and demonstrate that it works * properly. ***********************************************/ package polymorphism; import static net.mindview.util.Print.*; class NonSharedMember { public NonSharedMember(String id) { print("Non shared member constructor " + id); } } class SharedMember { private int refcount; 118 Thinking in Java, 4th Edition Annotated Solution Guide public void addRef() { print("Number of references " + ++refcount); } protected void dispose() { if(--refcount == 0) print("Disposing " + this); } public SharedMember() { print("Shared member constructor"); } public String toString() { return "Shared member"; } } class Rodent3 { private SharedMember m; NonSharedMember m1 = new NonSharedMember("r1"), m2 = new NonSharedMember("r2"); public Rodent3(SharedMember sm) { print("Rodent constructor"); m = sm; m.addRef(); } public void hop() { print("Rodent hopping"); } public void scurry() { print("Rodent scurrying"); } public void reproduce() { print("Making more Rodents"); } protected void dispose() { print("Disposing " + this); m.dispose(); } public String toString() { return "Rodent"; } } class Mouse3 extends Rodent3 { NonSharedMember m1 = new NonSharedMember("m1"), m2 = new NonSharedMember("m2"); public Mouse3(SharedMember sm) { super(sm); print("Mouse constructor"); } public void hop() { print("Mouse hopping"); } public void scurry() { print("Mouse scurrying"); } public void reproduce() { print("Making more Mice"); } public String toString() { return "Mouse"; } } class Gerbil3 extends Rodent3 { private SharedMember m; Polymorphism 119 NonSharedMember m1 = new NonSharedMember("g1"), m2 = new NonSharedMember("g2"); public Gerbil3(SharedMember sm) { super(sm); print("Gerbil constructor"); } public void hop() { print("Gerbil hopping"); } public void scurry() { print("Gerbil scurrying"); } public void reproduce() { print("Making more Gerbils"); } public String toString() { return "Gerbil"; } } class Hamster3 extends Rodent3 { private SharedMember m; NonSharedMember m1 = new NonSharedMember("h1"), m2 = new NonSharedMember("h2"); public Hamster3(SharedMember sm) { super(sm); print("Hamster constructor"); } public void hop() { print("Hamster hopping"); } public void scurry() { print("Hamster scurrying"); } public void reproduce() { print("Making more Hamsters"); } public String toString() { return "Hamster"; } } public class E14_SharedRodentInitialization { public static void main(String args[]) { SharedMember sm = new SharedMember(); Rodent3[] rodents = { new Hamster3(sm), new Gerbil3(sm), new Mouse3(sm), }; for(Rodent3 r : rodents) r.dispose(); } } /* Output: Shared member constructor Non shared member constructor r1 Non shared member constructor r2 Rodent constructor Number of references 1 120 Thinking in Java, 4th Edition Annotated Solution Guide Non shared member constructor Non shared member constructor Hamster constructor Non shared member constructor Non shared member constructor Rodent constructor Number of references 2 Non shared member constructor Non shared member constructor Gerbil constructor Non shared member constructor Non shared member constructor Rodent constructor Number of references 3 Non shared member constructor Non shared member constructor Mouse constructor Disposing Hamster Disposing Gerbil Disposing Mouse Disposing Shared member *///:~ h1 h2 r1 r2 g1 g2 r1 r2 m1 m2 All types of rodents share one member object. When we instantiate a new concrete rodent, the reference counter of the shared member is incremented. When a rodent is destroyed, the reference counter is decremented. The shared member is automatically disposed after release of the last reference. Exercise 15 //: polymorphism/E15_PolyConstructors2.java /****************** Exercise 15 ***************** * Add a RectangularGlyph to PolyConstructors.java * and demonstrate the problem described in this * section. ***********************************************/ package polymorphism; import static net.mindview.util.Print.*; class RectangularGlyph extends Glyph { private int width = 4; private int height = 5; RectangularGlyph(int width, int height) { this.width = width; this.height = height; print("RectangularGlyph.RectangularGlyph(), width = " + Polymorphism 121 width + ", height = " + height); } void draw() { print("RectangularGlyph.draw(), area = " + width * height); } } public class E15_PolyConstructors2 { public static void main(String[] args) { new RoundGlyph(5); new RectangularGlyph(2,2); } } /* Output: Glyph() before draw() RoundGlyph.draw(), radius = 0 Glyph() after draw() RoundGlyph.RoundGlyph(), radius = 5 Glyph() before draw() RectangularGlyph.draw(), area = 0 Glyph() after draw() RectangularGlyph.RectangularGlyph(), width = 2, height = 2 *///:~ This section shows how polymorphism calls the most-derived method, even before completely initializing the object. During construction, you see output of area = 0 because the base-class constructor calls draw( ), though this isn’t obvious in the code. Exercise 16 //: polymorphism/E16_Starship.java /****************** Exercise 16 ***************** * Following the example in Transmogrify.java, * create a Starship class containing an * AlertStatus reference that can indicate three * different states. Include methods to change * the states. ***********************************************/ package polymorphism; class AlertStatus { public String getStatus() { return "None"; } } class RedAlertStatus extends AlertStatus { 122 Thinking in Java, 4th Edition Annotated Solution Guide public String getStatus() { return "Red"; }; } class YellowAlertStatus extends AlertStatus { public String getStatus() { return "Yellow"; }; } class GreenAlertStatus extends AlertStatus { public String getStatus() { return "Green"; }; } class Starship { private AlertStatus status = new GreenAlertStatus(); public void setStatus(AlertStatus istatus) { status = istatus; } public String toString() { return status.getStatus(); } } public class E16_Starship { public static void main(String args[]) { Starship eprise = new Starship(); System.out.println(eprise); eprise.setStatus(new YellowAlertStatus()); System.out.println(eprise); eprise.setStatus(new RedAlertStatus()); System.out.println(eprise); } } /* Output: Green Yellow Red *///:~ This is an example of the State design pattern, wherein the object’s behavior is state-dependent. The three states of an AlertStatus object, to which the Starship class holds a reference, represent behaviors of the Starship. You switch the setStatus( ) reference from one state to another to change the behavior of the Starship. Exercise 17 //: polymorphism/E17_RTTI.java // {ThrowsException} /****************** Exercise 17 **************** Polymorphism 123 * Add a balance() method to Unicycle and Bicycle * but not to Tricycle, using the Cycle hierarchy * from Exercise 1. Upcast instances of all * three types to an array of Cycle. Try to call * balance() on each element of the array and * observe the results. Downcast and call * balance() and observe what happens. ***********************************************/ package polymorphism; import polymorphism.cycle.Cycle; import polymorphism.cycle.Tricycle; import polymorphism.cycle3.*; public class E17_RTTI { public static void main(String[] args) { Cycle[] cycles = new Cycle[]{ new Unicycle(), new Bicycle(), new Tricycle() }; // Compile time: method not found in Cycle: // cycles[0].balance(); // cycles[1].balance(); // cycles[2].balance(); ((Unicycle)cycles[0]).balance(); // Downcast/RTTI ((Bicycle)cycles[1]).balance(); // Downcast/RTTI ((Unicycle)cycles[2]).balance(); // Exception thrown } } ///:~ //: polymorphism/cycle3/Unicycle.java package polymorphism.cycle3; import polymorphism.cycle.Cycle; public class Unicycle extends Cycle { public void balance() {} } ///:~ //: polymorphism/cycle3/Bicycle.java package polymorphism.cycle3; import polymorphism.cycle.Cycle; public class Bicycle extends Cycle { public void balance() {} } ///:~ 124 Thinking in Java, 4th Edition Annotated Solution Guide Interfaces Exercise 1 //: interfaces/E01_AbstractRodent.java /****************** Exercise 1 ***************** * Modify Exercise 9 in the previous chapter so * Rodent is an abstract class. Make the * methods of Rodent abstract whenever possible. ***********************************************/ package interfaces; import static net.mindview.util.Print.*; abstract public public public } class Rodent { abstract void hop(); abstract void scurry(); abstract void reproduce(); class Mouse extends Rodent { public void hop() { print("Mouse hopping"); } public void scurry() { print("Mouse scurrying"); } public void reproduce() { print("Making more Mice"); } public String toString() { return "Mouse"; } } class Gerbil extends Rodent { public void hop() { print("Gerbil hopping"); } public void scurry() { print("Gerbil scurrying"); } public void reproduce() { print("Making more Gerbils"); } public String toString() { return "Gerbil"; } } class Hamster extends Rodent { public void hop() { print("Hamster hopping"); } public void scurry() { print("Hamster scurrying"); } public void reproduce() { print("Making more Hamsters"); } public String toString() { return "Hamster"; } } public class E01_AbstractRodent { public static void main(String args[]) { 125 Rodent[] rodents = { new Mouse(), new Gerbil(), new Hamster(), }; for(Rodent r : rodents) { r.hop(); r.scurry(); r.reproduce(); print(r); } } } /* Output: Mouse hopping Mouse scurrying Making more Mice Mouse Gerbil hopping Gerbil scurrying Making more Gerbils Gerbil Hamster hopping Hamster scurrying Making more Hamsters Hamster *///:~ Note that the root class method Object.toString( ) can be left out of the abstract base class. Exercise 2 //: interfaces/E02_Abstract.java // {CompileTimeError} /****************** Exercise 2 ***************** * Create a class as abstract without including * any abstract methods, and verify that you * cannot create any instances of that class. ***********************************************/ package interfaces; abstract class NoAbstractMethods { void f() { System.out.println("f()"); } } public class E02_Abstract { 126 Thinking in Java, 4th Edition Annotated Solution Guide public static void main(String args[]) { new NoAbstractMethods(); } } ///:~ Exercise 3 //: interfaces/E03_Initialization.java /****************** Exercise 3 ***************** * Create a base class with an abstract print() * method that is overridden in a derived class. * The overridden version of the method prints * the value of an int variable defined in the * derived class. Define this variable with a * nonzero value. Call print() in the * base-class constructor. Create an object of * the derived type in main(), then call its * print() method. Explain the results. ***********************************************/ package interfaces; abstract class BaseWithPrint { public BaseWithPrint() { print(); } public abstract void print(); } class DerivedWithPrint extends BaseWithPrint { int i = 47; public void print() { System.out.println("i = " + i); } } public class E03_Initialization { public static void main(String args[]) { DerivedWithPrint dp = new DerivedWithPrint(); dp.print(); } } /* Output: i = 0 i = 47 *///:~ The java virtual machine zeroes the bits of the object after it allocates storage, producing a default value for i before any other initialization occurs. The code Interfaces 127 calls the base-class constructor before running the derived-class initialization, so we see the zeroed value of i as the initial output. The danger of calling a method inside a constructor is when that method depends on a derived initialization. Before the derived-class constructor is called, the object may be in an unexpected state (in Java, at least that state is defined; this is not true with all languages – C++, for example). The safest approach is to set the object into a known good state as simply as possible, and then perform any other operations outside the constructor. Exercise 4 //: interfaces/E04_AbstractBase.java /****************** Exercise 4 ***************** * Create an abstract class with no methods. * Derive a class and add a method. Create a * static method that downcasts a reference from * the base class to the derived class and calls * the method. Demonstrate that it works in main(). * Eliminate the need for the downcast by moving * the abstract declaration to the base class. ***********************************************/ package interfaces; abstract class NoMethods {} class Extended1 extends NoMethods { public void f() { System.out.println("Extended1.f"); } } abstract class WithMethods { abstract public void f(); } class Extended2 extends WithMethods { public void f() { System.out.println("Extended2.f"); } } public class E04_AbstractBase { public static void test1(NoMethods nm) { // Must downcast to access f(): 128 Thinking in Java, 4th Edition Annotated Solution Guide ((Extended1)nm).f(); } public static void test2(WithMethods wm) { // No downcast necessary: wm.f(); } public static void main(String args[]) { NoMethods nm = new Extended1(); test1(nm); WithMethods wm = new Extended2(); test2(wm); } } /* Output: Extended1.f Extended2.f *///:~ test1( ) needs the downcast to call f( ), while test2( ) doesn’t need a downcast because f( ) is defined in the base class. Exercise 5 //: interfaces/E05_ImplementInterface.java /****************** Exercise 5 ****************** * Create an interface with three methods in its * own package. Implement the interface in a * different package. ***********************************************/ package interfaces; import interfaces.ownpackage.*; import static net.mindview.util.Print.*; class ImplementInterface implements AnInterface { public void f() { print("ImplementInterface.f"); } public void g() { print("ImplementInterface.g"); } public void h() { print("ImplementInterface.h"); } } public class E05_ImplementInterface { public static void main(String args[]) { ImplementInterface imp = new ImplementInterface(); imp.f(); imp.g(); imp.h(); } } /* Output: Interfaces 129 ImplementInterface.f ImplementInterface.g ImplementInterface.h *///:~ The elements of an interface are public but the interface itself is not, until we declare it public for use outside its package: //: interfaces/ownpackage/AnInterface.java package interfaces.ownpackage; public interface AnInterface { void f(); void g(); void h(); } ///:~ Exercise 6 //: interfaces/E06_InterfacePublicMethods.java /****************** Exercise 6 ****************** * Prove that all the methods in an interface are * automatically public. ***********************************************/ package interfaces; import interfaces.ownpackage.*; public class E06_InterfacePublicMethods implements AnInterface { // Each of these produces a compile-time error message, // stating that you cannot reduce the access of the // base class public method in a derived class. //! void f() {} //! void g() {} //! void h() {} // Compiles OK: public void f() {} public void g() {} public void h() {} public static void main(String args[]) {} } ///:~ Exercise 7 //: interfaces/E07_RodentInterface.java 130 Thinking in Java, 4th Edition Annotated Solution Guide /****************** Exercise 7 ****************** * Change Rodent to an interface in Exercise 9 of * the Polymorphism chapter. ***********************************************/ package interfaces; import static net.mindview.util.Print.*; interface Rodent2 { void hop(); void scurry(); void reproduce(); } class Mouse2 implements Rodent2 { public void hop() { print("Mouse hopping"); } public void scurry() { print("Mouse scurrying"); } public void reproduce() { print("Making more Mice"); } public String toString() { return "Mouse"; } } class Gerbil2 implements Rodent2 { public void hop() { print("Gerbil hopping"); } public void scurry() { print("Gerbil scurrying"); } public void reproduce() { print("Making more Gerbils"); } public String toString() { return "Gerbil"; } } class Hamster2 implements Rodent2 { public void hop() { print("Hamster hopping"); } public void scurry() { print("Hamster scurrying"); } public void reproduce() { print("Making more Hamsters"); } public String toString() { return "Hamster"; } } public class E07_RodentInterface { public static void main(String args[]) { Rodent2[] rodents = { new Mouse2(), new Gerbil2(), new Hamster2(), }; for(Rodent2 r : rodents) { r.hop(); r.scurry(); r.reproduce(); print(r); } Interfaces 131 } } /* Output: Mouse hopping Mouse scurrying Making more Mice Mouse Gerbil hopping Gerbil scurrying Making more Gerbils Gerbil Hamster hopping Hamster scurrying Making more Hamsters Hamster *///:~ Exercise 8 //: interfaces/E08_FastFood.java /****************** Exercise 8 ****************** * Create an interface called FastFood (with * appropriate methods) in * polymorphism.Sandwich.java, and change Sandwich * so it also implements FastFood. ***********************************************/ package interfaces; import polymorphism.Sandwich; import static net.mindview.util.Print.*; interface FastFood { void rushOrder(); void gobble(); } class FastSandwich extends Sandwich implements FastFood { public void rushOrder() { print("Rushing your sandwich order"); } public void gobble() { print("Chomp! Snort! Gobble!"); } } public class E08_FastFood { public static void main(String args[]) { FastSandwich burger = new FastSandwich(); print("Fries with that?"); print("Super Size?"); 132 Thinking in Java, 4th Edition Annotated Solution Guide burger.rushOrder(); burger.gobble(); } } /* Output: Meal() Lunch() PortableLunch() Bread() Cheese() Lettuce() Sandwich() Fries with that? Super Size? Rushing your sandwich order Chomp! Snort! Gobble! *///:~ Exercise 9 //: interfaces/E09_AbstractMusic5.java /****************** Exercise 9 ****************** * Refactor Music5.java by moving the common * methods in Wind, Percussion and Stringed into * an abstract class. ***********************************************/ package interfaces; import polymorphism.music.Note; import static net.mindview.util.Print.*; abstract class Instrument { public void play(Note n) { print(this + ".play() " + n); } public void adjust() { print(this + ".adjust()"); } // Forces implementation in derived class: public abstract String toString(); } class Wind extends Instrument { public String toString() { return "Wind"; } } class Percussion extends Instrument { public String toString() { return "Percussion"; } } Interfaces 133 class Stringed extends Instrument { public String toString() { return "Stringed"; } } class Brass extends Wind { public String toString() { return "Brass"; } } class Woodwind extends Wind { public String toString() { return "Woodwind"; } } public class E09_AbstractMusic5 { static void tune(Instrument i) { i.adjust(); i.play(Note.MIDDLE_C); } static void tuneAll(Instrument[] e) { for(Instrument i : e) tune(i); } public static void main(String[] args) { Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind() }; tuneAll(orchestra); } } /* Output: Wind.adjust() Wind.play() MIDDLE_C Percussion.adjust() Percussion.play() MIDDLE_C Stringed.adjust() Stringed.play() MIDDLE_C Brass.adjust() Brass.play() MIDDLE_C Woodwind.adjust() Woodwind.play() MIDDLE_C *///:~ We eliminate code duplication, moving common functionality into the abstract base class. toString( ) is now an abstract method, so all classes that implement Instrument provide a definition for it. Without the redefinition of 134 Thinking in Java, 4th Edition Annotated Solution Guide toString( ), all Instruments would otherwise use the original, non-abstract toString( ) from the root class Object. Exercise 10 //: interfaces/E10_PlayableMusic5.java /****************** Exercise 10 ****************** * Add a Playable to Modify Music5.java, and move * the play() declaration from Instrument to * Playable. Include Playable in the implements * list to add it to the derived classes. * Change tune() so it takes a Playable instead * of an Instrument. ***********************************************/ package interfaces; import polymorphism.music.Note; import static net.mindview.util.Print.*; interface Instrument2 { void adjust(); } interface Playable { void play(Note n); } class Wind2 implements Instrument2, Playable { public void play(Note n) { print(this + ".play() " + n); } public String toString() { return "Wind"; } public void adjust() { print(this + ".adjust()"); } } class Percussion2 implements Instrument2, Playable { public void play(Note n) { print(this + ".play() " + n); } public String toString() { return "Percussion"; } public void adjust() { print(this + ".adjust()"); } } class Stringed2 implements Instrument2, Playable { public void play(Note n) { print(this + ".play() " + n); Interfaces 135 } public String toString() { return "Stringed"; } public void adjust() { print(this + ".adjust()"); } } class Brass2 extends Wind2 { public String toString() { return "Brass"; } } class Woodwind2 extends Wind2 { public String toString() { return "Woodwind"; } } public class E10_PlayableMusic5 { static void tune(Playable p) { p.play(Note.MIDDLE_C); } static void tuneAll(Playable[] e) { for(Playable p : e) tune(p); } public static void main(String[] args) { Playable[] orchestra = { new Wind2(), new Percussion2(), new Stringed2(), new Brass2(), new Woodwind2() }; tuneAll(orchestra); } } /* Output: Wind.play() MIDDLE_C Percussion.play() MIDDLE_C Stringed.play() MIDDLE_C Brass.play() MIDDLE_C Woodwind.play() MIDDLE_C *///:~ We make Playable a concrete class to eliminate code duplication, as Wind2, Percussion2, and Stringed2 only use interfaces, and don’t inherit from any concrete classes. Exercise 11 //: interfaces/E11_Swapper.java /****************** Exercise 11 ****************** * Create a class with a method that takes a String 136 Thinking in Java, 4th Edition Annotated Solution Guide * argument and produces a result that swaps each * pair of characters in that argument. Adapt the * class to work with * interfaceprocessor.Apply.process(). ***********************************************/ package interfaces; import interfaces.interfaceprocessor.*; class CharacterPairSwapper { static String swap(String s) { StringBuilder sb = new StringBuilder(s); for(int i = 0; i < sb.length() - 1; i += 2) { char c1 = sb.charAt(i); char c2 = sb.charAt(i + 1); sb.setCharAt(i, c2); sb.setCharAt(i + 1, c1); } return sb.toString(); } } class SwapperAdapter implements Processor { public String name() { return CharacterPairSwapper.class.getSimpleName(); } public String process(Object input) { return CharacterPairSwapper.swap((String)input); } } public class E11_Swapper { public static void main(String[] args) { Apply.process(new SwapperAdapter(), "1234"); Apply.process(new SwapperAdapter(), "abcde"); } } /* Output: Using Processor CharacterPairSwapper 2143 Using Processor CharacterPairSwapper badce *///:~ CharacterPairSwapper uses the methods of the StringBuilder class to access and modify individual characters inside the character sequence. (See the J2SE5 API documentation for details.) CharacterPairSwapper has a completely different interface and cannot be directly integrated into the rest of Interfaces 137 the input processing system. The Adapter pattern application solves this problem. Because swap( ) is a static method, you don’t need a new SwapperAdapter instance for each SwapperAdapter. Refactor the SwapperAdapter class as a Singleton as an additional exercise. Exercise 12 //: interfaces/E12_CanClimb.java /****************** Exercise 12 ****************** * Follow the form of the other * interfaces to add an interface called * CanClimb in Adventure.java. ***********************************************/ package interfaces; interface CanClimb { void climb(); } class Hero2 extends ActionCharacter implements CanFight, CanSwim, CanFly, CanClimb { public void swim() {} public void fly() {} public void climb() {} } public class E12_CanClimb { static void t(CanFight x) { x.fight(); } static void u(CanSwim x) { x.swim(); } static void v(CanFly x) { x.fly(); } static void z(CanClimb x) { x.climb(); } static void w(ActionCharacter x) { x.fight(); } public static void main(String[] args) { Hero2 h = new Hero2(); t(h); // Treat it as a CanFight u(h); // Treat it as a CanSwim v(h); // Treat it as a CanFly z(h); // Treat it as a CanClimb w(h); // Treat it as an ActionCharacter } } ///:~ 138 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 13 //: interfaces/E13_Diamond.java /****************** Exercise 13 ****************** * Create an interface, inherit two new * interfaces from it, then multiply-inherit * a third interface from the second two. ***********************************************/ package interfaces; interface BaseInterface { void f(); } interface IntermediateInterface1 extends BaseInterface { void f(); } interface IntermediateInterface2 extends BaseInterface { void f(); } interface CombinedInterface extends IntermediateInterface1, IntermediateInterface2 { void f(); } class CombinedImpl implements CombinedInterface { public void f() { System.out.println("CombinedImpl.f()"); } } public class E13_Diamond { public static void main(String[] args) { new CombinedImpl().f(); } } /* Output: CombinedImpl.f() *///:~ Java allows multiple interface inheritance but not multiple implementation inheritance, which eliminates ambiguity about which of two identical members we use when combining implementations of the same base class. We replicate f( ) in the interfaces above to demonstrate that Java avoids the “diamond problem” (so called because of the diamond-shaped class diagram produced by Interfaces 139 multiple inheritance. C++ requires extra base-class syntax resolve the ambiguity created by concrete multiple inheritance). Exercise 14 //: interfaces/E14_InterfaceInheritance.java /****************** Exercise 14 ****************** * Create three interfaces, each with two methods. * Inherit a new interface from each, adding * a new method. Use the new interface to create * a class, and inherit from a concrete class. * Now write four methods, each of which takes one * of the four interfaces as an argument. Create * an object of your class in main(), and pass it * to each of the methods. ***********************************************/ package interfaces; import static net.mindview.util.Print.*; interface Interface1 { void f1(); void g1(); } interface Interface2 { void f2(); void g2(); } interface Interface3 { void f3(); void g3(); } interface Multiple extends Interface1, Interface2, Interface3 { void h(); } class Concrete { String s; public Concrete(String s) { this.s = s; } } class All extends Concrete implements Multiple { 140 Thinking in Java, 4th Edition Annotated Solution Guide All() { super("All"); } public void h() { print("All.h"); } public void f1() { print("All.f1"); public void g1() { print("All.g1"); public void f2() { print("All.f2"); public void g2() { print("All.g2"); public void f3() { print("All.f3"); public void g3() { print("All.g3"); } } } } } } } public class E14_InterfaceInheritance { static void takes1(Interface1 i) { i.f1(); i.g1(); } static void takes2(Interface2 i) { i.f2(); i.g2(); } static void takes3(Interface3 i) { i.f3(); i.g3(); } static void takesAll(All a) { a.f1(); a.g1(); a.f2(); a.g2(); a.f3(); a.g3(); a.h(); } public static void main(String args[]) { All a = new All(); takes1(a); takes2(a); takes3(a); takesAll(a); } } /* Output: All.f1 All.g1 All.f2 All.g2 All.f3 All.g3 All.f1 Interfaces 141 All.g1 All.f2 All.g2 All.f3 All.g3 All.h *///:~ Exercise 15 //: interfaces/E15_AbstractsAndInterfaces.java /****************** Exercise 15 ****************** * Modify Exercise 14 by creating an abstract class * and inheriting it into the derived class. ***********************************************/ package interfaces; import static net.mindview.util.Print.*; abstract class Abstract { String s; public Abstract(String s) { this.s = s; } abstract void af(); } class All2 extends Abstract implements Multiple { All2() { super("All2"); } void af() { print("All.af"); } public void f1() { print("All.f1"); } public void g1() { print("All.g1"); } public void f2() { print("All.f2"); } public void g2() { print("All.g2"); } public void f3() { print("All.f3"); } public void g3() { print("All.g3"); } public void h() { print("All2.h"); } } public class E15_AbstractsAndInterfaces { static void takes1(Interface1 i) { i.f1(); i.g1(); } static void takes2(Interface2 i) { i.f2(); i.g2(); } static void takes3(Interface3 i) { 142 Thinking in Java, 4th Edition Annotated Solution Guide i.f3(); i.g3(); } static void takesAll(All2 a) { a.f1(); a.g1(); a.f2(); a.g2(); a.f3(); a.g3(); a.h(); } static void takesAbstract(Abstract a) { a.af(); } public static void main(String args[]) { All2 a = new All2(); takes1(a); takes2(a); takes3(a); takesAll(a); takesAbstract(a); } } /* Output: All.f1 All.g1 All.f2 All.g2 All.f3 All.g3 All.f1 All.g1 All.f2 All.g2 All.f3 All.g3 All2.h All.af *///:~ Exercise 16 //: interfaces/E16_AdaptedCharSequence.java /****************** Exercise 16 ****************** * Create a class that produces a sequence of chars. * Adapt this class so that it can be an input to a Interfaces 143 * Scanner object. ***********************************************/ package interfaces; import java.nio.*; import java.util.*; class CharSequence { private static Random rand = new Random(47); private static final char[] capitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); private static final char[] lowers = "abcdefghijklmnopqrstuvwxyz".toCharArray(); private static final char[] vowels = "aeiou".toCharArray(); char[] generate() { char[] buffer = new char[10]; int idx = 0; buffer[idx++] = capitals[rand.nextInt(capitals.length)]; for(int i = 0; i < 4; i++) { buffer[idx++] = vowels[rand.nextInt(vowels.length)]; buffer[idx++] = lowers[rand.nextInt(lowers.length)]; } buffer[idx] = ' '; return buffer; } } class E16_AdaptedCharSequence extends CharSequence implements Readable { private int count; public E16_AdaptedCharSequence(int count) { this.count = count; } public int read(CharBuffer cb) { if(count-- == 0) return -1; // Indicates end of input char[] buffer = generate(); cb.put(buffer); return buffer.length; } public static void main(String[] args) { Scanner s = new Scanner(new E16_AdaptedCharSequence(10)); while(s.hasNext()) System.out.println(s.next()); } } /* Output: 144 Thinking in Java, 4th Edition Annotated Solution Guide Yazeruyac Fowenucor Goeazimom Raeuuacio Nuoadesiw Hageaikux Ruqicibui Numasetih Kuuuuozog Waqizeyoy *///:~ Though structurally different, this program behaves like RandomWords.java from the Interfaces chapter. Because CharSequence alone does not work as an input to a Scanner instance, an Adapter is created to provide a read( ) method. Exercise 17 //: interfaces/E17_ImplicitStaticFinal.java /****************** Exercise 17 ****************** * Prove that the fields in an interface are * implicitly static and final. ***********************************************/ package interfaces; interface StaticFinalTest { String RED = "Red"; } class Test implements StaticFinalTest { public Test() { // Compile-time error: cannot assign a value // to final variable RED: //! RED = "Blue"; } } public class E17_ImplicitStaticFinal { public static void main(String args[]) { // Accessing as a static field: System.out.println("StaticFinalTest.RED = " + StaticFinalTest.RED); } } /* Output: StaticFinalTest.RED = Red Interfaces 145 *///:~ The compiler tells you RED is a final variable when you try to assign a value to it. RED is clearly static because you can access it using static syntax. Exercise 18 //: interfaces/E18_CycleFactories.java /****************** Exercise 18 ****************** * Create a Cycle interface, with implementations * Unicycle, Bicycle, and Tricycle. Create factories * for each type of Cycle, and code that uses * these factories. ***********************************************/ package interfaces; interface Cycle { int wheels(); } interface CycleFactory { Cycle getCycle(); } class Unicycle implements Cycle { public int wheels() { return 1; } } class UnicycleFactory implements CycleFactory { public Unicycle getCycle() { return new Unicycle(); } } class Bicycle implements Cycle { public int wheels() { return 2; } } class BicycleFactory implements CycleFactory { public Bicycle getCycle() { return new Bicycle(); } } class Tricycle implements Cycle { public int wheels() { return 3; } } class TricycleFactory implements CycleFactory { public Tricycle getCycle() { return new Tricycle(); } 146 Thinking in Java, 4th Edition Annotated Solution Guide } public class E18_CycleFactories { public static void ride(CycleFactory fact) { Cycle c = fact.getCycle(); System.out.println("Num. of wheels: " + c.wheels()); } public static void main(String[] args) { ride(new UnicycleFactory()); ride(new BicycleFactory ()); ride(new TricycleFactory ()); } } /* Output: Num. of wheels: 1 Num. of wheels: 2 Num. of wheels: 3 *///:~ This solution has several more classes than Exercise 5 in the Polymorphism chapter. To anticipate every possibility, programmers often use this interface + factory form to create classes, because it allows them to add new classes anywhere. However, complexity and necessary maintenance make this a wise choice only when you know you’ll be adding new classes. Exercise 19 //: interfaces/E19_TossingFramework.java /****************** Exercise 19 ****************** * Create a framework using Factory Methods that * performs both coin tossing and dice tossing. ***********************************************/ package interfaces; interface Tossing { boolean event(); } interface TossingFactory { Tossing getTossing(); } class CoinTossing implements Tossing { private int events; private static final int EVENTS = 2; public boolean event() { System.out.println("Coin tossing event " + events); return ++events != EVENTS; } } Interfaces 147 class CoinTossingFactory implements TossingFactory { public CoinTossing getTossing() { return new CoinTossing(); } } class DiceTossing implements Tossing { private int events; private static final int EVENTS = 6; public boolean event() { System.out.println("Dice tossing event " + events); return ++events != EVENTS; } } class DiceTossingFactory implements TossingFactory { public DiceTossing getTossing() { return new DiceTossing(); } } public class E19_TossingFramework { public static void simulate(TossingFactory fact) { Tossing t = fact.getTossing(); while(t.event()) ; } public static void main(String[] args) { simulate(new CoinTossingFactory()); simulate(new DiceTossingFactory()); } } /* Output: Coin tossing event 0 Coin tossing event 1 Dice tossing event 0 Dice tossing event 1 Dice tossing event 2 Dice tossing event 3 Dice tossing event 4 Dice tossing event 5 *///:~ 148 Thinking in Java, 4th Edition Annotated Solution Guide Inner Classes Exercise 1 //: innerclasses/E01_ReferenceToInnerClass.java /****************** Exercise 1 ***************** * Write a class named Outer containing an * inner class named Inner. Add a method to Outer * that returns an object of type Inner. In * main(), create and initialize a reference to * an Inner. ***********************************************/ package innerclasses; class Outer { class Inner { { System.out.println("Inner created"); } } Inner getInner() { return new Inner(); } } public class E01_ReferenceToInnerClass { public static void main(String[] args) { Outer o = new Outer(); Outer.Inner i = o.getInner(); } } /* Output: Inner created *///:~ Exercise 2 //: innerclasses/E02_SequenceOfStringHolders.java /****************** Exercise 2 ***************** * Create a class that holds a String, with a * toString() method that displays this String. * Add several instances of your new class to a * Sequence object, then display them. ***********************************************/ package innerclasses; 149 class StringHolder { private String data; StringHolder(String data) { this.data = data; } public String toString() { return data; } } public class E02_SequenceOfStringHolders { public static void main(String[] args) { Sequence sequence = new Sequence(10); for(int i = 0; i < 10; i++) sequence.add(new StringHolder(Integer.toString(i))); Selector selector = sequence.selector(); while(!selector.end()) { System.out.print(selector.current() + " "); selector.next(); } } } /* Output: 0 1 2 3 4 5 6 7 8 9 *///:~ Exercise 3 //: innerclasses/E03_InnerAccessingOuter.java /****************** Exercise 3 ***************** * Modify Exercise 1 so Outer has a private * String field (initialized by the constructor), * and Inner has a toString() that displays this * field. Create an object of type Inner and * display it. ***********************************************/ package innerclasses; class Outer2 { private final String data; class Inner { public String toString() { return data; } } Outer2(String data) { this.data = data; } Inner getInner() { return new Inner(); } } public class E03_InnerAccessingOuter { public static void main(String[] args) { Outer2 o = new Outer2("Inner accessing outer!"); Outer2.Inner i = o.getInner(); 150 Thinking in Java, 4th Edition Annotated Solution Guide System.out.println(i.toString()); } } /* Output: Inner accessing outer! *///:~ Exercise 4 //: innerclasses/E04_SequenceSelectorToSequence.java /****************** Exercise 4 ***************** * Add a method to the class Sequence.SequenceSelector * that produces the reference to the outer class * Sequence. ***********************************************/ package innerclasses; class Sequence2 { private Object[] items; private int next; public Sequence2(int size) { items = new Object[size]; } public void add(Object x) { if(next < items.length) items[next++] = x; } private class SequenceSelector implements Selector { private int i; public boolean end() { return i == items.length; } public Object current() { return items[i]; } public void next() { if(i < items.length) i++; } public Sequence2 sequence() { return Sequence2.this; } } public Selector selector() { return new SequenceSelector(); } public boolean check() { return this == ((SequenceSelector)selector()).sequence(); } } public class E04_SequenceSelectorToSequence { public static void main(String[] args) { Sequence2 s = new Sequence2(10); System.out.println(s.check()); } } /* Output: Inner Classes 151 true *///:~ The private inner class SequenceSelector is inaccessible outside of Sequence2, so check( ) performs the validation. For more information about this topic, see the Inner classes and upcasting section of TIJ4. Exercise 5 //: innerclasses/E05_InstanceOfInner.java /****************** Exercise 5 ***************** * Create a class with an inner class. In a * separate class, make an instance of the inner * class. ***********************************************/ package innerclasses; class Outer3 { class Inner { { System.out.println("Inner created"); } } } public class E05_InstanceOfInner { public static void main(String args[]) { Outer3 o = new Outer3(); Outer3.Inner i = o.new Inner(); } } /* Output: Inner created *///:~ To create a reference in the separate class E05_InstanceOfInner, you must fully resolve the name of the inner class Outer3.Inner. The inner class object has a connection to the outer-class object, so the new expression must specify the object that creates the inner class. Exercise 6 //: innerclasses/E06_ProtectedInnerClass.java /****************** Exercise 6 ***************** * Create an interface with at least one method, * in its own package. Create a class in a 152 Thinking in Java, 4th Edition Annotated Solution Guide * separate package. Add a protected inner class * that implements the interface. In a third * package, inherit from your class and, inside a * method, return an object of the protected * inner class, upcasting to the interface during * the return. ***********************************************/ package innerclasses; import innerclasses.exercise6b.*; import innerclasses.exercise6.*; public class E06_ProtectedInnerClass extends SimpleClass { public SimpleInterface get() { return new Inner(); } public static void main(String args[]) { new E06_ProtectedInnerClass().get().f(); } } ///:~ //: innerclasses/exercise6/SimpleInterface.java package innerclasses.exercise6; public interface SimpleInterface { void f(); } ///:~ //: innerclasses/exercise6b/SimpleClass.java package innerclasses.exercise6b; import innerclasses.exercise6.*; public class SimpleClass { protected class Inner implements SimpleInterface { // Force constructor to be public: public Inner() {} public void f() {} } } ///:~ You cannot access the synthesized default constructor from E06 ProtectedInnerClass because it has the same protected access as the defining class. Exercise 7 //: innerclasses/E07_InnerClassAccess.java Inner Classes 153 /****************** Exercise 7 ***************** * Create a class with a private field and a * private method. Create an inner class with a * method that modifies the outer-class field and * calls the outer-class method. In a second * outer-class method, create an object of the * inner class and call its method, then show * the effect on the outer-class object. ***********************************************/ package innerclasses; public class E07_InnerClassAccess { private int i = 10; private void f() { System.out.println("E07_InnerClassAccess.f"); } class Inner { void g() { i++; f(); } } public void h() { Inner in = new Inner(); in.g(); System.out.println("i = " + i); } public static void main(String args[]) { E07_InnerClassAccess ica = new E07_InnerClassAccess(); ica.h(); } } /* Output: E07_InnerClassAccess.f i = 11 *///:~ This exercise shows that inner classes have transparent access to their outerclass objects, even private fields and methods. Exercise 8 //: innerclasses/E08_OuterAccessingInner.java /****************** Exercise 8 ***************** * Determine whether an outer class has access to * the private elements of its inner class. ***********************************************/ 154 Thinking in Java, 4th Edition Annotated Solution Guide package innerclasses; class Outer4 { class Inner { private int j; private void h() { System.out.println("Inner.h called"); System.out.println("Inner.j = " + j); } } public void testInnerAccess() { Inner i = new Inner(); i.j = 47; i.h(); } } public class E08_OuterAccessingInner { public static void main(String args[]) { Outer4 o = new Outer4(); o.testInnerAccess(); } } /* Output: Inner.h called Inner.j = 47 *///:~ As you can see from the output, the accessibility goes both ways. Exercise 9 //: innerclasses/E09_InnerClassInMethod.java /****************** Exercise 9 ***************** * Create an interface with at least one method, * and implement it by defining an * inner class within a method that returns a * reference to your interface. ***********************************************/ package innerclasses; import innerclasses.exercise6.*; public class E09_InnerClassInMethod { public SimpleInterface get() { class SI implements SimpleInterface{ public void f() { System.out.println("SI.f"); } } Inner Classes 155 return new SI(); } public static void main(String args[]) { SimpleInterface si = new E09_InnerClassInMethod().get(); si.f(); } } /* Output: SI.f *///:~ Exercise 10 //: innerclasses/E10_InnerClassInScope.java /****************** Exercise 10 ***************** * Repeat Exercise 9 but define the inner * class within a scope within a method. ************************************************/ package innerclasses; import innerclasses.exercise6.*; public class E10_InnerClassInScope { public SimpleInterface get() { { class SI implements SimpleInterface{ public void f() { System.out.println("SI.f"); } } return new SI(); } } public static void main(String args[]) { SimpleInterface si = new E10_InnerClassInScope().get(); si.f(); } } /* Output: SI.f *///:~ The inner class remains visible only if the return statement is in its scope; if not, the inner class definition goes out of scope. 156 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 11 //: innerclasses/E11_HiddenInnerClass.java /****************** Exercise 11 ***************** * Create a private inner class that implements a * public interface. Write a method that returns * a reference to an instance of the private * inner class, upcast to the interface. Show * that the inner class is completely hidden by * trying to downcast to it. ************************************************/ package innerclasses; import innerclasses.exercise6.*; class Outer5 { private class Inner implements SimpleInterface { public void f() { System.out.println("Outer5.Inner.f"); } } public SimpleInterface get() { return new Inner(); } public Inner get2() { return new Inner(); } } public class E11_HiddenInnerClass { public static void main(String args[]) { Outer5 out = new Outer5(); SimpleInterface si = out.get(); si = out.get2(); // Won't compile -- 'Inner' not visible: //! Inner i1 = out.get2(); //! Inner i2 = (Inner)si; } } ///:~ The public get( ) method returns the private class Inner instance, upcast to SimpleInterface. Notice that get2( ) returns an object of the private class Inner. However, when you call get2( ) from outside of Outer, you can’t use the return value’s actual type because it’s private and visible only inside the class. You can only upcast the return value to a visible base interface. Thus, Outer methods can use the actual type, while methods of other classes must use the upcast result. Inner Classes 157 Exercise 12 //: innerclasses/E12_AnonymousInnerClassAccess.java /****************** Exercise 12 ***************** * Repeat Exercise 7 using an anonymous inner * class. ***********************************************/ package innerclasses; public class E12_AnonymousInnerClassAccess { private int i = 10; private void f() { System.out.println("E12_AnonymousInnerClassAccess.f"); } public void h() { new Object() { void g() { i++; f(); } }.g(); System.out.println("i = " + i); } public static void main(String args[]) { E12_AnonymousInnerClassAccess ica = new E12_AnonymousInnerClassAccess(); ica.h(); } } /* Output: E12_AnonymousInnerClassAccess.f i = 11 *///:~ Exercise 13 //: innerclasses/E13_AnonymousInnerClassInMethod.java /****************** Exercise 13 ***************** * Repeat Exercise 9 using an anonymous inner * class. ***********************************************/ package innerclasses; import innerclasses.exercise6.*; public class E13_AnonymousInnerClassInMethod { public SimpleInterface get() { 158 Thinking in Java, 4th Edition Annotated Solution Guide return new SimpleInterface() { public void f() { System.out.println("SimpleInterface.f"); } }; } public static void main(String args[]) { SimpleInterface si = new E13_AnonymousInnerClassInMethod().get(); si.f(); } } /* Output: SimpleInterface.f *///:~ Exercise 14 //: innerclasses/E14_HorrorShow2.java /****************** Exercise 14 ****************** * Modify interfaces/HorrorShow.java to implement * DangerousMonster and Vampire using anonymous * classes. ************************************************/ package innerclasses; public class E14_HorrorShow2 { public static void main(String[] args) { DangerousMonster barney = new DangerousMonster() { public void menace() {} public void destroy() {} }; HorrorShow.u(barney); HorrorShow.v(barney); Vampire vlad = new Vampire() { public void menace() {} public void destroy() {} public void kill() {} public void drinkBlood() {} }; HorrorShow.u(vlad); HorrorShow.v(vlad); HorrorShow.w(vlad); } } ///:~ Inner Classes 159 Exercise 15 //: innerclasses/E15_ReturningAnonymousIC.java /****************** Exercise 15 ***************** * Create a class with a non-default constructor * (one with arguments) and no default constructor * (no "no-arg" constructor). Create a second class * with a method that returns a reference to * an object of the first class. Create the object * you return by making an anonymous inner * class inherit from the first class. ***********************************************/ package innerclasses; class NoDefault { private int i; public NoDefault(int i) { this.i = i; } public void f() { System.out.println("NoDefault.f"); } } class Second { public NoDefault get1(int i) { // Doesn't override any methods: return new NoDefault(i) {}; } public NoDefault get2(int i) { // Overrides f(): return new NoDefault(i) { public void f() { System.out.println("Second.get2.f"); } }; } } public class E15_ReturningAnonymousIC { public static void main(String args[]) { Second sec = new Second(); NoDefault nd = sec.get1(47); nd.f(); nd = sec.get2(99); nd.f(); } } /* Output: NoDefault.f Second.get2.f 160 Thinking in Java, 4th Edition Annotated Solution Guide *///:~ In get1( ), you inherit NoDefault in the anonymous inner class without overriding any methods; usually you’ll override a method when you inherit, as in get2( ). Exercise 16 //: innerclasses/E16_AnonymousCycleFactories.java /****************** Exercise 16 ****************** * Use anonymous inner classes to modify the * solution to Exercise 18 from the Interfaces chapter. ***********************************************/ package innerclasses; interface Cycle { int wheels(); } interface CycleFactory { Cycle getCycle(); } class Unicycle implements Cycle { public int wheels() { return 1; } public static CycleFactory factory = new CycleFactory() { public Unicycle getCycle() { return new Unicycle(); } }; } class Bicycle implements Cycle { public int wheels() { return 2; } public static CycleFactory factory = new CycleFactory() { public Bicycle getCycle() { return new Bicycle(); } }; } class Tricycle implements Cycle { public int wheels() { return 3; } public static CycleFactory factory = new CycleFactory() { public Tricycle getCycle() { return new Tricycle(); } }; } Inner Classes 161 public class E16_AnonymousCycleFactories { public static void ride(CycleFactory fact) { Cycle c = fact.getCycle(); System.out.println("Num. of wheels: " + c.wheels()); } public static void main(String[] args) { ride(Unicycle.factory); ride(Bicycle.factory); ride(Tricycle.factory); } } /* Output: Num. of wheels: 1 Num. of wheels: 2 Num. of wheels: 3 *///:~ Exercise 17 //: innerclasses/E17_AnonymousTossingFramework.java /****************** Exercise 17 ****************** * Use anonymous inner classes to modify the solution * to Exercise 19 from the Interfaces chapter. ***********************************************/ package innerclasses; interface Tossing { boolean event(); } interface TossingFactory { Tossing getTossing(); } class CoinTossing implements Tossing { private int events; private static final int EVENTS = 2; public boolean event() { System.out.println("Coin tossing event " + events); return ++events != EVENTS; } public static TossingFactory factory = new TossingFactory() { public CoinTossing getTossing() { return new CoinTossing(); } }; } class DiceTossing implements Tossing { 162 Thinking in Java, 4th Edition Annotated Solution Guide private int events; private static final int EVENTS = 6; public boolean event() { System.out.println("Dice tossing event " + events); return ++events != EVENTS; } public static TossingFactory factory = new TossingFactory() { public DiceTossing getTossing() { return new DiceTossing(); } }; } public class E17_AnonymousTossingFramework { public static void simulate(TossingFactory fact) { Tossing t = fact.getTossing(); while(t.event()) ; } public static void main(String[] args) { simulate(CoinTossing.factory); simulate(DiceTossing.factory); } } /* Output: Coin tossing event 0 Coin tossing event 1 Dice tossing event 0 Dice tossing event 1 Dice tossing event 2 Dice tossing event 3 Dice tossing event 4 Dice tossing event 5 *///:~ Exercise 18 //: innerclasses/E18_NestedClass.java /****************** Exercise 18 ***************** * Create a class containing a nested class. * In main(), create an instance of the nested * class. ************************************************/ package innerclasses; public class E18_NestedClass { Inner Classes 163 static class Nested { void f() { System.out.println("Nested.f"); } } public static void main(String args[]) { Nested ne = new Nested(); ne.f(); } } class Other { // Specifying the nested type outside // the scope of the class: void f() { E18_NestedClass.Nested ne = new E18_NestedClass.Nested(); } } /* Output: Nested.f *///:~ You can refer to just the class name when inside the method of a class with a defined nested (static inner) class, but outside the class, you must specify the outer class and nested class, as shown in Other, above. Exercise 19 //: innerclasses/E19_InnerInsideInner.java /****************** Exercise 19 ***************** * Create a class containing an inner class that * itself contains an inner class. Repeat this * using static inner classes. Note the names of * the .class files produced by the compiler. ***********************************************/ package innerclasses; public class E19_InnerInsideInner { class Inner1 { class Inner2 { void f() {} } Inner2 makeInner2() { return new Inner2(); } } Inner1 makeInner1() { return new Inner1(); } static class Nested1 { static class Nested2 { void f() {} 164 Thinking in Java, 4th Edition Annotated Solution Guide } void f() {} } public static void main(String args[]) { new E19_InnerInsideInner.Nested1().f(); new E19_InnerInsideInner.Nested1.Nested2().f(); E19_InnerInsideInner x1 = new E19_InnerInsideInner(); E19_InnerInsideInner.Inner1 x2 = x1.makeInner1(); E19_InnerInsideInner.Inner1.Inner2 x3 = x2.makeInner2(); x3.f(); } } ///:~ The class names produced are: E19_InnerInsideInner.class E19_InnerInsideInner$Inner1.class E19_InnerInsideInner$Inner1$Inner2.class E19_InnerInsideInner$Nested1.class E19_InnerInsideInner$Nested1$Nested2.class Exercise 20 //: innerclasses/E20_InterfaceWithNested.java /****************** Exercise 20 ***************** * Create an interface containing a nested class. * Implement this interface and create an * instance of the nested class. ***********************************************/ package innerclasses; interface WithNested { class Nested { int i; public Nested(int i) { this.i = i; } void f() { System.out.println("Nested.f"); } } } class B2 implements WithNested {} public class E20_InterfaceWithNested { public static void main(String args[]) { B2 b = new B2(); WithNested.Nested ne = new WithNested.Nested(5); Inner Classes 165 ne.f(); } } /* Output: Nested.f *///:~ Even if an interface itself has no use, a nested class defined within it can still be useful. If we define Nested within WithNested, that just means we locate its name there, since all elements of an interface are public. Nested has no added access to the elements of WithNested. Exercise 21 //: innerclasses/E21_InterfaceWithNested2.java /****************** Exercise 21 ****************** * Create an interface with a nested class * and a static method that calls the methods * of your interface and displays the results. * Implement your interface and pass an instance of * your implementation to the method. ***********************************************/ package innerclasses; interface I { void f(); void g(); class Nested { static void call(I impl) { System.out.println("Calling I.f()"); impl.f(); System.out.println("Calling I.g()"); impl.g(); } } } public class E21_InterfaceWithNested2 { public static void main(String[] args) { I impl = new I() { public void f() {} public void g() {} }; I.Nested.call(impl); } } /* Output: Calling I.f() 166 Thinking in Java, 4th Edition Annotated Solution Guide Calling I.g() *///:~ Notice that we use an anonymous inner class to implement interface I. It’s generally clearer to list all methods of an interface, then define nested classes. Exercise 22 //: innerclasses/E22_GetRSelector.java /****************** Exercise 22 ***************** * Implement reverseSelector() in Sequence.java. ***********************************************/ package innerclasses; class Sequence3 { private Object[] objects; private int next; public Sequence3(int size) { objects = new Object[size]; } public void add(Object x) { if(next < objects.length) objects[next++] = x; } private class ReverseSelector implements Selector { int i = objects.length - 1; public boolean end() { return i < 0; } public Object current() { return objects[i]; } public void next() { if(i >= 0) i--; } } private class SequenceSelector implements Selector { private int i; public boolean end() { return i == objects.length; } public Object current() { return objects[i]; } public void next() { if(i < objects.length) i++; } } public Selector selector() { return new SequenceSelector(); } public Selector reverseSelector() { return new ReverseSelector(); } } public class E22_GetRSelector { public static void main(String[] args) { Sequence3 sequence = new Sequence3(10); Inner Classes 167 for(int i = 0; i < 10; i++) sequence.add(Integer.toString(i)); Selector selector = sequence.reverseSelector(); while(!selector.end()) { System.out.print(selector.current() + " "); selector.next(); } } } /* Output: 9 8 7 6 5 4 3 2 1 0 *///:~ This is a copy-and-paste solution with minor logic changes in the ReverseSelector class. Exercise 23 //: innerclasses/E23_UAB.java /****************** Exercise 23 ***************** * Create an interface U with three methods. * Create a class A with a method that produces a * reference to a U by building an anonymous * inner class. Create a second class B that * contains an array of U. B should have one * method that accepts and stores a reference to * a U in the array, a second method that sets a * reference in the array (specified by the * method argument) to null, and a third method * that moves through the array and calls the * methods in U. In main(), create a group of A * objects and a single B. Fill the B with U * references produced by the A objects. Use the * B to call back into all the A objects. Remove * some of the U references from the B. ***********************************************/ package innerclasses; interface U { void f(); void g(); void h(); } class A { public U getU() { return new U() { 168 Thinking in Java, 4th Edition Annotated Solution Guide public void f() { System.out.println("A.f"); } public void g() { System.out.println("A.g"); } public void h() { System.out.println("A.h"); } }; } } class B { U[] ua; public B(int size) { ua = new U[size]; } public boolean add(U elem) { for(int i = 0; i < ua.length; i++) { if(ua[i] == null) { ua[i] = elem; return true; } } return false; // Couldn't find any space } public boolean setNull(int i) { if(i < 0 || i >= ua.length) return false; // Value out of bounds // (Normally throw an exception) ua[i] = null; return true; } public void callMethods() { for(int i = 0; i < ua.length; i++) if(ua[i] != null) { ua[i].f(); ua[i].g(); ua[i].h(); } } } public class E23_UAB { public static void main(String args[]) { A[] aa = { new A(), new A(), new A() }; B b = new B(3); for(int i = 0; i < aa.length; i++) b.add(aa[i].getU()); b.callMethods(); System.out.println("****"); b.setNull(0); Inner Classes 169 b.callMethods(); } } /* Output: A.f A.g A.h A.f A.g A.h A.f A.g A.h **** A.f A.g A.h A.f A.g A.h *///:~ Notice that we remove the zero element. Exercise 24 //: innerclasses/E24_GreenhouseInnerEvent.java // {Args: 5000} /****************** Exercise 24 ***************** * Add Event inner classes that turn fans on and * off in GreenhouseControls.java. Configure * GreenhouseController.java to use these new * Event objects. ***********************************************/ package innerclasses; import innerclasses.controller.*; class GreenhouseControlsWithFan extends Controller { private boolean light = false; public class LightOn extends Event { public LightOn(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn on the light. light = true; } public String toString() { return "Light is on"; } 170 Thinking in Java, 4th Edition Annotated Solution Guide } public class LightOff extends Event { public LightOff(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn off the light. light = false; } public String toString() { return "Light is off"; } } private boolean fan = false; public class FanOn extends Event { public FanOn(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn on the Fan. fan = true; } public String toString() { return "Fan is on"; } } public class FanOff extends Event { public FanOff(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn off the Fan. fan = false; } public String toString() { return "Fan is off"; } } private boolean water = false; public class WaterOn extends Event { public WaterOn(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. water = true; } public String toString() { return "Greenhouse water is on"; } } public class WaterOff extends Event { public WaterOff(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. water = false; } public String toString() { Inner Classes 171 return "Greenhouse water is off"; } } private String thermostat = "Day"; public class ThermostatNight extends Event { public ThermostatNight(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. thermostat = "Night"; } public String toString() { return "Thermostat on night setting"; } } public class ThermostatDay extends Event { public ThermostatDay(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. thermostat = "Day"; } public String toString() { return "Thermostat on day setting"; } } // An example of an action() that inserts a // new one of itself into the event list: public class Bell extends Event { public Bell(long delayTime) { super(delayTime); } public void action() { addEvent(new Bell(delayTime)); } public String toString() { return "Bing!"; } } public class Restart extends Event { private Event[] eventList; public Restart(long delayTime, Event[] eventList) { super(delayTime); this.eventList = eventList; for(Event e : eventList) addEvent(e); } public void action() { for(Event e : eventList) { 172 Thinking in Java, 4th Edition Annotated Solution Guide e.start(); // Rerun each event addEvent(e); } start(); // Rerun this Event addEvent(this); } public String toString() { return "Restarting system"; } } public static class Terminate extends Event { public Terminate(long delayTime) { super(delayTime); } public void action() { System.exit(0); } public String toString() { return "Terminating"; } } } public class E24_GreenhouseInnerEvent { public static void main(String[] args) { GreenhouseControlsWithFan gc = new GreenhouseControlsWithFan(); // Instead of hard-wiring, you could parse // configuration information from a text file here: gc.addEvent(gc.new Bell(900)); Event[] eventList = { gc.new ThermostatNight(0), gc.new LightOn(200), gc.new FanOn(300), gc.new LightOff(400), gc.new FanOff(500), gc.new WaterOn(600), gc.new WaterOff(800), gc.new ThermostatDay(1400) }; gc.addEvent(gc.new Restart(2000, eventList)); if(args.length == 1) gc.addEvent(new GreenhouseControlsWithFan .Terminate(new Integer(args[0]))); gc.run(); } } /* Output: Bing! Thermostat on night setting Light is on Fan is on Light is off Fan is off Inner Classes 173 Greenhouse water is on Greenhouse water is off Thermostat on day setting Restarting system Terminating *///:~ This is basically a copy and paste exercise, but it helps ensure that you understand the structure of the program. Exercise 25 //: innerclasses/E25_GreenhouseController.java // {Args: 5000} /****************** Exercise 25 ****************** * Inherit from GreenhouseControls in * GreenhouseControls.java to add Event inner * classes that turn water mist generators on * and off. Write a new version of * GreenhouseController.java to use these new * Event objects. ***********************************************/ package innerclasses; import innerclasses.controller.*; class GreenhouseControlsWithWMG extends GreenhouseControls { private boolean generator = false; public class WatermistGeneratorOn extends Event { public WatermistGeneratorOn(long delayTime) { super(delayTime); } public void action() { generator = true; } public String toString() { return "Water mist generator is on"; } } public class WatermistGeneratorOff extends Event { public WatermistGeneratorOff(long delayTime) { super(delayTime); } public void action() { generator = false; } public String toString() { 174 Thinking in Java, 4th Edition Annotated Solution Guide return "Water mist generator is off"; } } } public class E25_GreenhouseController { public static void main(String[] args) { GreenhouseControlsWithWMG gc = new GreenhouseControlsWithWMG(); // Instead of hard-wiring, you could parse // configuration information from a text file here: gc.addEvent(gc.new Bell(900)); Event[] eventList = { gc.new ThermostatNight(0), gc.new LightOn(200), gc.new LightOff(400), gc.new WaterOn(600), gc.new WaterOff(800), gc.new ThermostatDay(1400), gc.new WatermistGeneratorOn(1600), gc.new WatermistGeneratorOff(1800) }; gc.addEvent(gc.new Restart(2000, eventList)); if(args.length == 1) gc.addEvent(new GreenhouseControlsWithWMG .Terminate(new Integer(args[0]))); gc.run(); } } /* Output: Bing! Thermostat on night setting Light is on Light is off Greenhouse water is on Greenhouse water is off Thermostat on day setting Water mist generator is on Water mist generator is off Restarting system Terminating *///:~ Exercise 26 //: innerclasses/E26_InnerClassInheritance.java /****************** Exercise 26 ***************** Inner Classes 175 * Create a class with an inner class that has a * non-default constructor (one that takes * arguments). Create a second class with an inner * class that inherits from the first inner class. ************************************************/ package innerclasses; class WithNonDefault { class Inner { int i; public Inner(int i) { this.i = i; } public Inner() { i = 47; } public void f() { System.out.println("Inner.f"); } } } public class E26_InnerClassInheritance { class Inner2 extends WithNonDefault.Inner { // Won't compile -- WithNonDefault not available: //! public Inner2(int i) { //! super(i); //! } public Inner2(WithNonDefault wnd, int i) { wnd.super(i); } public void f() { System.out.println("Inner2.f"); super.f(); } } public static void main(String args[]) { WithNonDefault wnd = new WithNonDefault(); E26_InnerClassInheritance ici = new E26_InnerClassInheritance(); Inner2 i2 = ici.new Inner2(wnd, 47); i2.f(); } } /* Output: Inner2.f Inner.f *///:~ We use the new expression from the outer class object to create an instance of an inner class. To create an instance of one inner class inheriting from another, we provide the constructor with an instance of the outer base class, so creating a new Inner2 object is doubly complex. However, the Inner2 object now has an intimate connection with the objects WithNonDefault and 176 Thinking in Java, 4th Edition Annotated Solution Guide E26_InnerClassInheritance; this creates a private mediation between the two. (See Mediator in the design patterns literature.) Inner Classes 177 Holding Your Objects Exercise 1 //: holding/E01_Gerbil.java /****************** Exercise 1 ****************** * Create a new class called Gerbil with an int * gerbilNumber initialized in the constructor. * Give it a method called hop() that prints out * which gerbil number this is, and that it’s hopping. * Create an ArrayList and add Gerbil objects to * the List. Now use the get() method to move * through the List and call hop() for each Gerbil. ***********************************************/ package holding; import java.util.*; class Gerbil { private final int gerbilNumber; Gerbil(int gerbilNumber) { this.gerbilNumber = gerbilNumber; } public String toString() { return "gerbil " + gerbilNumber; } public void hop() { System.out.println(this + " is hopping"); } } public class E01_Gerbil { public static void main(String args[]) { ArrayList gerbils = new ArrayList(); for(int i = 0; i < 10; i++) gerbils.add(new Gerbil(i)); for(int i = 0; i < gerbils.size(); i++) gerbils.get(i).hop(); } } /* Output: gerbil 0 is hopping gerbil 1 is hopping gerbil 2 is hopping 179 gerbil gerbil gerbil gerbil gerbil gerbil gerbil *///:~ 3 4 5 6 7 8 9 is is is is is is is hopping hopping hopping hopping hopping hopping hopping Exercise 2 //: holding/E02_SimpleCollection2.java /****************** Exercise 2 ****************** * Modify SimpleCollection.java to use a Set for c. ***********************************************/ package holding; import java.util.*; public class E02_SimpleCollection2 { public static void main(String[] args) { Collection c = new HashSet(); for(int i = 0; i < 10; i++) c.add(i); // Autoboxing for(Integer i : c) System.out.print(i + ", "); } } /* Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, *///:~ Generally, a Set does not hold elements in insertion order. Exercise 3 //: holding/E03_UnlimitedSequence.java /****************** Exercise 3 ****************** * Modify innerclasses/Sequence.java so you * can add any number of elements to it. ***********************************************/ package holding; import java.util.*; class UnlimitedSequence { private final List items = new ArrayList(); 180 Thinking in Java, 4th Edition Annotated Solution Guide public void add(Object x) { items.add(x); } private class SequenceSelector implements Selector { private int i; public boolean end() { return i == items.size(); } public Object current() { return items.get(i); } public void next() { if(i < items.size()) i++; } } public Selector selector() { return new SequenceSelector(); } } public class E03_UnlimitedSequence { public static void main(String[] args) { UnlimitedSequence sequence = new UnlimitedSequence(); for(int i = 0; i < 10; i++) sequence.add(Integer.toString(i)); Selector selector = sequence.selector(); while(!selector.end()) { System.out.print(selector.current() + " "); selector.next(); } } } /* Output: 0 1 2 3 4 5 6 7 8 9 *///:~ Exercise 4 //: holding/E04_MovieNameGenerator.java /****************** Exercise 4 ****************** * Create a generator class that produces String objects * with the names of characters from your favorite * movie each time you call next(), and then loops * around to the beginning of the character list * when it runs out of names. Use this generator to * fill an array, an ArrayList, a LinkedList, a * HashSet, a LinkedHashSet, and a TreeSet, then * print each container. ***********************************************/ package holding; import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; class MovieNameGenerator implements Generator { Holding Your Objects 181 String[] characters = { "Grumpy", "Happy", "Sleepy", "Dopey", "Doc", "Sneezy", "Bashful", "Snow White", "Witch Queen", "Prince" }; int next; public String next() { String r = characters[next]; next = (next + 1) % characters.length; return r; } } public class E04_MovieNameGenerator { private static final MovieNameGenerator mng = new MovieNameGenerator(); static String[] fill(String[] array) { for(int i = 0; i < array.length; i++) array[i] = mng.next(); return array; } static Collection fill(Collection collection) { for(int i = 0; i < 5; i++) collection.add(mng.next()); return collection; } public static void main(String[] args) { print(Arrays.toString(fill(new String[5]))); print(fill(new ArrayList())); print(fill(new LinkedList())); print(fill(new HashSet())); print(fill(new LinkedHashSet())); print(fill(new TreeSet())); } } /* Output: [Grumpy, Happy, Sleepy, Dopey, Doc] [Sneezy, Bashful, Snow White, Witch Queen, Prince] [Grumpy, Happy, Sleepy, Dopey, Doc] [Snow White, Witch Queen, Sneezy, Bashful, Prince] [Grumpy, Happy, Sleepy, Dopey, Doc] [Bashful, Prince, Sneezy, Snow White, Witch Queen] *///:~ All data generators use the simple Generator parameterized interface, introduced later in TIJ4. 182 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 5 //: holding/E05_IntegerListFeatures.java /****************** Exercise 5 ****************** * Use Integers instead of Pets to modify * ListFeatures.java (remember autoboxing). * Explain any difference in results. ***********************************************/ package holding; import java.util.*; import static net.mindview.util.Print.*; public class E05_IntegerListFeatures { static Random rand = new Random(47); public static void main(String[] args) { List ints = new ArrayList( Arrays.asList(1, 2, 3, 4, 5, 6, 7)); print("1: " + ints); ints.add(8); print("2: " + ints); print("3: " + ints.contains(8)); ints.remove(Integer.valueOf(8)); Integer i = ints.get(2); print("4: " + i + " " + ints.indexOf(i)); Integer j = Integer.valueOf(1); print("5: " + ints.indexOf(j)); print("6: " + ints.remove(j)); print("7: " + ints.remove(i)); print("8: " + ints); ints.add(3, 0); print("9: " + ints); List sub = ints.subList(1, 4); print("subList: " + sub); print("10: " + ints.containsAll(sub)); Collections.sort(sub); print("sorted subList: " + sub); print("11: " + ints.containsAll(sub)); Collections.shuffle(sub, rand); print("shuffled subList: " + sub); print("12: " + ints.containsAll(sub)); List copy = new ArrayList(ints); sub = Arrays.asList(ints.get(1), ints.get(4)); print("sub: " + sub); copy.retainAll(sub); print("13: " + copy); copy = new ArrayList(ints); Holding Your Objects 183 copy.remove(2); print("14: " + copy); copy.removeAll(sub); print("15: " + copy); copy.set(1, 9); print("16: " + copy); copy.addAll(2, sub); print("17: " + copy); print("18: " + ints.isEmpty()); ints.clear(); print("19: " + ints); print("20: " + ints.isEmpty()); ints.addAll(Arrays.asList(1, 2, 3, 4)); print("21: " + ints); Object[] o = ints.toArray(); print("22: " + o[3]); Integer[] ia = ints.toArray(new Integer[0]); print("22: " + ia[3]); } } /* Output: 1: [1, 2, 3, 4, 5, 6, 7] 2: [1, 2, 3, 4, 5, 6, 7, 8] 3: true 4: 3 2 5: 0 6: true 7: true 8: [2, 4, 5, 6, 7] 9: [2, 4, 5, 0, 6, 7] subList: [4, 5, 0] 10: true sorted subList: [0, 4, 5] 11: true shuffled subList: [4, 0, 5] 12: true sub: [4, 6] 13: [4, 6] 14: [2, 4, 5, 6, 7] 15: [2, 5, 7] 16: [2, 9, 7] 17: [2, 9, 4, 6, 7] 18: false 19: [] 20: true 21: [1, 2, 3, 4] 22: 4 22: 4 184 Thinking in Java, 4th Edition Annotated Solution Guide *///:~ List behavior varies depending on equals( ), as TIJ4 explains. Two Integers are equal if their contents are identical in the output. Be vigilant with overloaded methods like remove( ); it’s easy to make mistakes due to autoboxing. If, for example, you type remove(2) instead of remove(Integer.valueOf(2)) you remove the third element from the list (as the first element’s index is 0), instead of an element whose value is 2. Exercise 6 //: holding/E06_StringListFeatures.java /****************** Exercise 6 ****************** * Using Strings instead of Pets, modify * ListFeatures.java . Explain any difference in * results. ***********************************************/ package holding; import java.util.*; import static net.mindview.util.Print.*; public class E06_StringListFeatures { static Random rand = new Random(47); public static void main(String[] args) { List strs = new ArrayList( Arrays.asList("A", "B", "C", "D", "E", "F", "G")); print("1: " + strs); strs.add("H"); print("2: " + strs); print("3: " + strs.contains("H")); strs.remove("H"); String s1 = strs.get(2); print("4: " + s1 + " " + strs.indexOf(s1)); String s2 = "A"; print("5: " + strs.indexOf(s2)); print("6: " + strs.remove(s2)); print("7: " + strs.remove(s1)); print("8: " + strs); strs.add(3, "0"); print("9: " + strs); List sub = strs.subList(1, 4); print("subList: " + sub); print("10: " + strs.containsAll(sub)); Collections.sort(sub); print("sorted subList: " + sub); Holding Your Objects 185 print("11: " + strs.containsAll(sub)); Collections.shuffle(sub, rand); print("shuffled subList: " + sub); print("12: " + strs.containsAll(sub)); List copy = new ArrayList(strs); sub = Arrays.asList(strs.get(1), strs.get(4)); print("sub: " + sub); copy.retainAll(sub); print("13: " + copy); copy = new ArrayList(strs); copy.remove(2); print("14: " + copy); copy.removeAll(sub); print("15: " + copy); copy.set(1, "I"); print("16: " + copy); copy.addAll(2, sub); print("17: " + copy); print("18: " + strs.isEmpty()); strs.clear(); print("19: " + strs); print("20: " + strs.isEmpty()); strs.addAll(Arrays.asList("A", "B", "C", "D")); print("21: " + strs); Object[] o = strs.toArray(); print("22: " + o[3]); String[] sa = strs.toArray(new String[0]); print("22: " + sa[3]); } } /* Output: 1: [A, B, C, D, E, F, G] 2: [A, B, C, D, E, F, G, H] 3: true 4: C 2 5: 0 6: true 7: true 8: [B, D, E, F, G] 9: [B, D, E, 0, F, G] subList: [D, E, 0] 10: true sorted subList: [0, D, E] 11: true shuffled subList: [D, 0, E] 12: true sub: [D, F] 13: [D, F] 186 Thinking in Java, 4th Edition Annotated Solution Guide 14: [B, D, 15: [B, E, 16: [B, I, 17: [B, I, 18: false 19: [] 20: true 21: [A, B, 22: D 22: D *///:~ E, F, G] G] G] D, F, G] C, D] Again, be careful when using overloaded methods. Exercise 7 //: holding/E07_TestList.java /****************** Exercise 7 ***************** * Create a class and make an initialized array * of your class objects. Fill a List from * your array. Create a subset of your List using * subList(), then remove this subset from * your List. ***********************************************/ package holding; import java.util.*; class IDClass { private static int counter; private int count = counter++; public String toString() { return "IDClass " + count; } } public class E07_TestList { public static void main(String args[]) { IDClass[] idc = new IDClass[10]; for(int i = 0; i < idc.length; i++) idc[i] = new IDClass(); List lst = new ArrayList( Arrays.asList(idc)); System.out.println("lst = " + lst); List subSet = lst.subList(lst.size()/4, lst.size()/2); System.out.println("subSet = " + subSet); Holding Your Objects 187 // The semantics of the sub list become undefined if the // backing list is structurally modified! //! lst.removeAll(subSet); subSet.clear(); System.out.println("lst = " + lst); } } /* Output: lst = [IDClass 0, IDClass 1, IDClass 2, IDClass 3, IDClass 4, IDClass 5, IDClass 6, IDClass 7, IDClass 8, IDClass 9] subSet = [IDClass 2, IDClass 3, IDClass 4] lst = [IDClass 0, IDClass 1, IDClass 5, IDClass 6, IDClass 7, IDClass 8, IDClass 9] *///:~ The methods asList( ) and subList( ) return immutable Lists because they are “backed” by the underlying array and list, respectively. If you structurally modify the backing list as we did in the commented-out section, you get a concurrent modification exception. Therefore, the program operates on the sublist instead of the backing list. Alternatively, avoid errors by creating a separate copy of the returned sublist and use that as an argument to removeAll( ). Exercise 8 //: holding/E08_GerbilIterator.java /****************** Exercise 8 ****************** * Modify Exercise 1 so it uses an Iterator to * move through the List while calling hop(). ***********************************************/ package holding; import java.util.*; public class E08_GerbilIterator { public static void main(String args[]) { ArrayList gerbils = new ArrayList(); for(int i = 0; i < 10; i++) gerbils.add(new Gerbil(i)); for(Iterator it = gerbils.iterator(); it.hasNext(); it.next().hop()); } } /* Output: gerbil 0 is hopping gerbil 1 is hopping gerbil 2 is hopping gerbil 3 is hopping 188 Thinking in Java, 4th Edition Annotated Solution Guide gerbil gerbil gerbil gerbil gerbil gerbil *///:~ 4 5 6 7 8 9 is is is is is is hopping hopping hopping hopping hopping hopping Exercise 9 //: holding/E09_SequenceIterator.java /****************** Exercise 9 ****************** * Modify innerclasses/Sequence.java so that * Sequence works with an Iterator instead of a * Selector. ***********************************************/ package holding; import java.util.*; class Sequence2 { private Object[] items; private int next; public Sequence2(int size) { items = new Object[size]; } public void add(Object x) { if(next < items.length) items[next++] = x; } private class SequenceIterator implements Iterator { private int i; public boolean hasNext() { return i < items.length; } public Object next() { if(hasNext()) return items[i++]; throw new NoSuchElementException(); } public void remove() { throw new UnsupportedOperationException(); } } public Iterator iterator() { return new SequenceIterator(); } } public class E09_SequenceIterator { Holding Your Objects 189 public static void main(String[] args) { Sequence2 sequence = new Sequence2(10); for(int i = 0; i < 10; i++) sequence.add(Integer.toString(i)); for(Iterator it = sequence.iterator(); it.hasNext();) System.out.print(it.next() + " "); } } /* Output: 0 1 2 3 4 5 6 7 8 9 *///:~ Exercise 10 //: holding/E10_RodentIterator.java /****************** Exercise 10 ****************** * change Exercise 9 in the Polymorphism chapter * to use an ArrayList to hold the Rodents and an * Iterator to move through their sequence. ***********************************************/ package holding; import java.util.*; import static net.mindview.util.Print.*; class Rodent { public void hop() { print("Rodent hopping"); } public void scurry() { print("Rodent scurrying"); } public void reproduce() { print("Making more Rodents"); } public String toString() { return "Rodent"; } } class Mouse extends Rodent { public void hop() { print("Mouse hopping"); } public void scurry() { print("Mouse scurrying"); } public void reproduce() { print("Making more Mice"); } public String toString() { return "Mouse"; } } class Hamster extends Rodent { public void hop() { print("Hamster hopping"); } public void scurry() { print("Hamster scurrying"); } public void reproduce() { print("Making more Hamsters"); } public String toString() { return "Hamster"; } } public class E10_RodentIterator { 190 Thinking in Java, 4th Edition Annotated Solution Guide public static void main(String args[]) { ArrayList rodents = new ArrayList( Arrays.asList( new Rodent(), new Mouse(), new Hamster())); Rodent r; for(Iterator it = rodents.iterator(); it.hasNext();) { r = it.next(); r.hop(); r.scurry(); r.reproduce(); print(r); } } } /* Output: Rodent hopping Rodent scurrying Making more Rodents Rodent Mouse hopping Mouse scurrying Making more Mice Mouse Hamster hopping Hamster scurrying Making more Hamsters Hamster *///:~ Exercise 11 //: holding/E11_IterToString.java /****************** Exercise 11 ***************** * Write a method that uses an Iterator to step * through a Collection and print the toString() * of each object in the container. Fill all the * different types of Collections with objects and * apply your method to each container. ***********************************************/ package holding; import java.util.*; public class E11_IterToString { public static void printToStrings(Iterator it) { while(it.hasNext()) System.out.println(it.next().toString()); Holding Your Objects 191 } @SuppressWarnings("unchecked") public static void main(String args[]) { List> ca = Arrays.>asList( new ArrayList(), new LinkedList(), new HashSet(), new TreeSet()); for(Collection c : ca) E04_MovieNameGenerator.fill(c); for(Collection c : ca) printToStrings(c.iterator()); } } /* Output: Grumpy Happy Sleepy Dopey Doc Sneezy Bashful Snow White Witch Queen Prince Happy Doc Sleepy Grumpy Dopey Bashful Prince Sneezy Snow White Witch Queen *///:~ Exercise 12 //: holding/E12_ListIterators.java /****************** Exercise 12 ***************** * Create two Lists of the same size, * and populate one of them. Use ListIterators * to insert elements from the first List into * the second in reverse order. (You may want to * explore a number of different ways to solve 192 Thinking in Java, 4th Edition Annotated Solution Guide * this problem.) ***********************************************/ package holding; import java.util.*; public class E12_ListIterators { static void reverse(List list) { ListIterator fwd = list.listIterator(); ListIterator rev = list.listIterator(list.size()); int mid = list.size() >> 1; for(int i = 0; i < mid; i++) { Integer tmp = fwd.next(); fwd.set(rev.previous()); rev.set(tmp); } } public static void main(String[] args) { List src = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List dest = new LinkedList(src); System.out.println("source: " + src); System.out.println("destination: " + dest); reverse(dest); System.out.println("source: " + src); System.out.println("destination: " + dest); } } /* Output: source: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] destination: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] source: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] destination: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] *///:~ We fill the destination list with elements from the source list, then reverse their order using reverse( ). Practice by finding other solutions. Exercise 13 //: holding/E13_GreenhouseLinkedList.java // {Args: 5000} /****************** Exercise 13 ***************** * In the innerclasses/GreenhouseController.java * example, the class Controller uses an ArrayList. * Change the code to use a LinkedList instead, and * use an Iterator to cycle through the set of events. Holding Your Objects 193 ***********************************************/ package holding; import java.util.*; import innerclasses.controller.Event; class Controller { // List changed to a LinkedList: private List eventList = new LinkedList(); public void addEvent(Event c) { eventList.add(c); } public void run() { while(eventList.size() > 0) { Iterator it = new LinkedList(eventList).iterator(); while(it.hasNext()) { Event e = it.next(); if(e.ready()) { System.out.println(e); e.action(); it.remove(); } } } } } class GreenhouseControls extends Controller { private boolean light = false; public class LightOn extends Event { public LightOn(long delayTime) { super(delayTime); } public void action() { light = true; } public String toString() { return "Light is on"; } } public class LightOff extends Event { public LightOff(long delayTime) { super(delayTime); } public void action() { light = false; } public String toString() { return "Light is off"; } } private boolean water = false; public class WaterOn extends Event { public WaterOn(long delayTime) { super(delayTime); } public void action() { water = true; } public String toString() { return "Greenhouse water is on"; } } public class WaterOff extends Event { 194 Thinking in Java, 4th Edition Annotated Solution Guide public WaterOff(long delayTime) { super(delayTime); } public void action() { water = false; } public String toString() { return "Greenhouse water is off"; } } private String thermostat = "Day"; public class ThermostatNight extends Event { public ThermostatNight(long delayTime) { super(delayTime); } public void action() { thermostat = "Night"; } public String toString() { return "Thermostat on night setting"; } } public class ThermostatDay extends Event { public ThermostatDay(long delayTime) { super(delayTime); } public void action() { thermostat = "Day"; } public String toString() { return "Thermostat on day setting"; } } public class Bell extends Event { public Bell(long delayTime) { super(delayTime); } public void action() { addEvent(new Bell(delayTime)); } public String toString() { return "Bing!"; } } public class Restart extends Event { private Event[] eventList; public Restart(long delayTime, Event[] eventList) { super(delayTime); this.eventList = eventList; for(Event e : eventList) addEvent(e); } public void action() { for(Event e : eventList) { e.start(); addEvent(e); } start(); addEvent(this); Holding Your Objects 195 } public String toString() { return "Restarting system"; } } public static class Terminate extends Event { public Terminate(long delayTime) { super(delayTime); } public void action() { System.exit(0); } public String toString() { return "Terminating"; } } } public class E13_GreenhouseLinkedList { public static void main(String[] args) { GreenhouseControls gc = new GreenhouseControls(); gc.addEvent(gc.new Bell(900)); Event[] eventList = { gc.new ThermostatNight(0), gc.new LightOn(200), gc.new LightOff(400), gc.new WaterOn(600), gc.new WaterOff(800), gc.new ThermostatDay(1400) }; gc.addEvent(gc.new Restart(2000, eventList)); if(args.length == 1) gc.addEvent( new GreenhouseControls.Terminate( new Integer(args[0]))); gc.run(); } } /* Output: Bing! Thermostat on night setting Light is on Light is off Greenhouse water is on Greenhouse water is off Thermostat on day setting Restarting system Terminating *///:~ Here we simply copy all the classes from TIJ4 into a single file, only modifying Controller. 196 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 14 //: holding/E14_MiddleInsertion.java /****************** Exercise 14 ***************** * Create an empty LinkedList. Using a * ListIterator, add Integers to the List by always * inserting them in the middle of the List. ***********************************************/ package holding; import java.util.*; public class E14_MiddleInsertion { public static void main(String[] args) { LinkedList list = new LinkedList(); ListIterator it = list.listIterator(); for(int i = 1; i <= 10; i++) { it.add(i); if(i % 2 == 0) it.previous(); } System.out.println(list); } } /* Output: [1, 3, 5, 7, 9, 10, 8, 6, 4, 2] *///:~ You must calibrate the implicit cursor position every second iteration to keep the insertion point at the middle of the list. Exercise 15 //: holding/E15_Evaluator.java /****************** Exercise 15 ***************** * Stacks are often used to evaluate expressions * in programming languages. Using * net.mindview.util.Stack, evaluate the following * expression, where '+' means "push the following * letter onto the stack," and '-' means "pop the * top of the stack and print it": * "+U+n+c---+e+r+t---+a-+i-+n+t+y---+ -+r+u--+l+e+s---" ***********************************************/ package holding; import net.mindview.util.Stack; public class E15_Evaluator { Holding Your Objects 197 private final static Stack stack = new Stack(); private static void evaluate(String expr) { char data[] = expr.toCharArray(); for(int i = 0; i < data.length;) switch(data[i++]) { case '+' : stack.push(data[i++]); break; case '-' : System.out.print(stack.pop()); } } public static void main(String[] args) { evaluate( "+U+n+c---+e+r+t---+a-+i-+n+t+y---+ -+r+u--+l+e+s---"); } } /* Output: cnUtreaiytn ursel *///:~ Of course, a real-world program also needs some error handling logic (e.g., to avert deadlock in case of improper input). Exercise 16 //: holding/E16_Vowels.java /****************** Exercise 16 ***************** * Create a Set of the vowels. Working from * UniqueWords.java, count and display the number of * vowels in each input word, and also display the * total number of vowels in the input file. ***********************************************/ package holding; import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; public class E16_Vowels { private final static Set vowels = new HashSet(Arrays.asList('a', 'e', 'o', 'u', 'i', 'A', 'E', 'O', 'U', 'I')); public static void main(String[] args) { HashSet processedWords = new HashSet(); int fileVowels = 0; int wordVowels; for(String word : new TextFile("E16_Vowels.java", "\\W+")) { 198 Thinking in Java, 4th Edition Annotated Solution Guide wordVowels = 0; for(char letter : word.toCharArray()) if(vowels.contains(letter)) wordVowels++; if(!processedWords.contains(word)) { processedWords.add(word); print(word + " has " + wordVowels + " vowel(s)"); } fileVowels += wordVowels; } print("Total number of vowels in file: " + fileVowels); } } /* Output: (Sample) holding has 2 vowel(s) E16_Vowels has 3 vowel(s) java has 2 vowel(s) Exercise has 4 vowel(s) 16 has 0 vowel(s) Create has 3 vowel(s) ... contains has 3 vowel(s) add has 1 vowel(s) print has 1 vowel(s) has has 1 vowel(s) vowel has 2 vowel(s) s has 0 vowel(s) Total has 2 vowel(s) Total number of vowels in file: 240 *///:~ Exercise 17 //: holding/E17_GerbilMap.java /****************** Exercise 17 ****************** * Move the Gerbil class from Exercise 1 * into a Map, and associate each Gerbil (the value) * with it's name as a String (the key). * Use an Iterator for the keySet() to move * through the Map, look up the Gerbil for each key, * print out the key, and tell the Gerbil to hop(). ***********************************************/ package holding; import java.util.*; import java.util.Map.Entry; public class E17_GerbilMap { Holding Your Objects 199 public static void main(String args[]) { Map map = new HashMap(); map.put("Fuzzy", new Gerbil(1)); map.put("Spot", new Gerbil(2)); map.put("Joe", new Gerbil(3)); map.put("Ted", new Gerbil(4)); map.put("Heather", new Gerbil(5)); Iterator> it = map.entrySet().iterator(); while(it.hasNext()) { Entry entry = it.next(); System.out.print(entry.getKey() + ": "); entry.getValue().hop(); } } } /* Output: Ted: gerbil 4 is hopping Heather: gerbil 5 is hopping Spot: gerbil 2 is hopping Joe: gerbil 3 is hopping Fuzzy: gerbil 1 is hopping *///:~ The hashing function (described in TIJ4) does not store objects in entry order, so use a LinkedHashMap if you want to maintain that order. Exercise 18 //: holding/E18_MapOrder.java /******************* Exercise 18 ************************ * Fill a HashMap with key-value pairs. Print the results * to show ordering by hash code. Extract the pairs, sort * by key, and place the result into a LinkedHashMap. * Show that insertion order is maintained. *******************************************************/ package holding; import java.util.*; import net.mindview.util.*; public class E18_MapOrder { public static void main(String[] args) { Map m1 = new HashMap(Countries.capitals(25)); System.out.println(m1); String[] keys = m1.keySet().toArray(new String[0]); Arrays.sort(keys); 200 Thinking in Java, 4th Edition Annotated Solution Guide Map m2 = new LinkedHashMap(); for(String key : keys) m2.put(key, m1.get(key)); System.out.println(m2); } } /* Output: {KENYA=Nairobi, THE GAMBIA=Banjul, CHAD=N'djamena, CAPE VERDE=Praia, COTE D'IVOIR (IVORY COAST)=Yamoussoukro, GUINEA=Conakry, COMOROS=Moroni, CAMEROON=Yaounde, ANGOLA=Luanda, EGYPT=Cairo, BURKINA FASO=Ouagadougou, BENIN=Porto-Novo, BURUNDI=Bujumbura, ETHIOPIA=Addis Ababa, BOTSWANA=Gaberone, DJIBOUTI=Dijibouti, CONGO=Brazzaville, CENTRAL AFRICAN REPUBLIC=Bangui, ERITREA=Asmara, ALGERIA=Algiers, EQUATORIAL GUINEA=Malabo, GHANA=Accra, GABON=Libreville, BISSAU=Bissau, LESOTHO=Maseru} {ALGERIA=Algiers, ANGOLA=Luanda, BENIN=Porto-Novo, BISSAU=Bissau, BOTSWANA=Gaberone, BURKINA FASO=Ouagadougou, BURUNDI=Bujumbura, CAMEROON=Yaounde, CAPE VERDE=Praia, CENTRAL AFRICAN REPUBLIC=Bangui, CHAD=N'djamena, COMOROS=Moroni, CONGO=Brazzaville, COTE D'IVOIR (IVORY COAST)=Yamoussoukro, DJIBOUTI=Dijibouti, EGYPT=Cairo, EQUATORIAL GUINEA=Malabo, ERITREA=Asmara, ETHIOPIA=Addis Ababa, GABON=Libreville, GHANA=Accra, GUINEA=Conakry, KENYA=Nairobi, LESOTHO=Maseru, THE GAMBIA=Banjul} *///:~ Exercise 19 //: holding/E19_SetOrder.java /******************* Exercise 19 ***************** * Repeat Exercise 18 with a HashSet and * LinkedHashSet. *************************************************/ package holding; import java.util.*; import net.mindview.util.*; public class E19_SetOrder { public static void main(String[] args) { Set s1 = new HashSet(Countries.names(25)); System.out.println(s1); String[] elements = s1.toArray(new String[0]); Arrays.sort(elements); Set s2 = new LinkedHashSet(); Holding Your Objects 201 for(String element : elements) s2.add(element); System.out.println(s2); } } /* Output: [KENYA, THE GAMBIA, CHAD, CAPE VERDE, COTE D'IVOIR (IVORY COAST), GUINEA, COMOROS, CAMEROON, ANGOLA, EGYPT, BURKINA FASO, BENIN, BURUNDI, ETHIOPIA, BOTSWANA, DJIBOUTI, CONGO, CENTRAL AFRICAN REPUBLIC, ERITREA, ALGERIA, EQUATORIAL GUINEA, GHANA, GABON, BISSAU, LESOTHO] [ALGERIA, ANGOLA, BENIN, BISSAU, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, COTE D'IVOIR (IVORY COAST), DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, GHANA, GUINEA, KENYA, LESOTHO, THE GAMBIA] *///:~ Exercise 20 //: holding/E20_VowelsInfo.java /****************** Exercise 20 ***************** * Modify Exercise 16 to count the occurrence of * each vowel. ***********************************************/ package holding; import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; public class E20_VowelsInfo { private final static Set vowels = new HashSet(Arrays.asList('a', 'e', 'o', 'u', 'i', 'A', 'E', 'O', 'U', 'I')); static void updateStat(Map stat, char letter) { Character ch = Character.toLowerCase(letter); Integer freq = stat.get(ch); stat.put(ch, freq == null ? 1 : freq + 1); } public static void main(String[] args) { HashMap fileStat = new HashMap(); HashSet processedWords = new HashSet(); HashMap wordStat = new HashMap(); for(String word : 202 Thinking in Java, 4th Edition Annotated Solution Guide new TextFile("E20_VowelsInfo.java", "\\W+")) { wordStat.clear(); for(char letter : word.toCharArray()) if(vowels.contains(letter)) { updateStat(wordStat, letter); updateStat(fileStat, letter); } if(!processedWords.contains(word)) { processedWords.add(word); print("Vowels in " + word + ": " + wordStat); } } print("*************************"); print("Vowels in the whole file: " + fileStat); } } /* Output: (Sample) Vowels in holding: {o=1, i=1} Vowels in E20_VowelsInfo: {o=2, i=1, e=2} Vowels in java: {a=2} Vowels in Exercise: {i=1, e=3} Vowels in 20: {} Vowels in Modify: {o=1, i=1} Vowels in 16: {} Vowels in so: {o=1} ... Vowels in Vowels: {o=1, e=1} Vowels in in: {i=1} Vowels in whole: {o=1, e=1} Vowels in file: {i=1, e=1} ************************* Vowels in the whole file: {o=49, i=56, a=79, u=15, e=96} *///:~ Try to accelerate the code, processing only the first occurrence of each word. Exercise 21 //: holding/E21_WordsInfo.java /****************** Exercise 21 ***************** * Using a Map, follow the form of * UniqueWords.java to create a program that counts * the occurrence of words in a file. Sort the * results using Collections.sort() with a second * argument of String.CASE_INSENSITIVE_ORDER (to * produce an alphabetic sort), and display the result. ***********************************************/ Holding Your Objects 203 package holding; import java.util.*; import net.mindview.util.*; public class E21_WordsInfo { public static void main(String[] args) { Map wordsStat = new HashMap(); for(String word : new TextFile("E21_WordsInfo.java", "\\W+")) { Integer freq = wordsStat.get(word); wordsStat.put(word, freq == null ? 1 : freq + 1); } List keys = new ArrayList(wordsStat.keySet()); Collections.sort(keys, String.CASE_INSENSITIVE_ORDER); for(String key : keys) System.out.println(key + " => " + wordsStat.get(key)); } } /* Output: (Sample) 0 => 1 1 => 2 21 => 1 a => 4 alphabetic => 1 an => 1 and => 1 args => 1 ... W => 1 with => 1 word => 3 words => 1 wordsStat => 5 *///:~ Exercise 22 //: holding/E22_WordsInfo2.java /****************** Exercise 22 ***************** * Modify the previous exercise so that it uses a * class containing a String and a count field to * store each different word, and a Set of these * objects to maintain the list of words. ***********************************************/ package holding; 204 Thinking in Java, 4th Edition Annotated Solution Guide import java.util.*; import net.mindview.util.*; class WordCounter { public static final Comparator CASE_INSENSITIVE_ORDER = new Comparator() { public int compare(WordCounter o1, WordCounter o2) { return o1.word.compareToIgnoreCase(o2.word); } }; private final String word; private int frequency; WordCounter(String word) { this.word = word; frequency = 1; } void incFrequency() { ++frequency; } String getWord() { return word; } int getFrequency() { return frequency; } public boolean equals(Object o) { return o instanceof WordCounter && word.equals(((WordCounter)o).word); } public int hashCode() { return word.hashCode(); } } public class E22_WordsInfo2 { static void updateStat(Iterator it, WordCounter wc) { while(it.hasNext()) { WordCounter currentWC = it.next(); if(currentWC.equals(wc)) currentWC.incFrequency(); } } public static void main(String[] args) { Set stat = new HashSet(); for(String word : new TextFile("E22_WordsInfo2.java", "\\W+")) { WordCounter wc = new WordCounter(word); if(stat.contains(wc)) updateStat(stat.iterator(), wc); else stat.add(wc); } List l = new ArrayList(stat); Holding Your Objects 205 Collections.sort( l, WordCounter.CASE_INSENSITIVE_ORDER); for(WordCounter wc : l) System.out.println(wc.getWord() + " => " + wc.getFrequency()); } } /* Output: (Sample) 0 => 1 1 => 1 22 => 1 a => 4 add => 1 and => 2 args => 1 ... util => 2 void => 3 W => 1 wc => 9 while => 1 word => 13 WordCounter => 19 words => 1 *///:~ The WordCounter class contains a String and a frequency field to store each different word. It uses equals( ) and hashCode( ) methods, respectively, to store class instances inside a HashSet. We created a custom Comparator to use the Collections.sort( ) method in the last exercise. Here it acts like String.CASE_INSENSITIVE_ORDER. Without a HashMap, the situation gets more complicated, which shows how useful it is to keep your code base as small as possible (and boost productivity) by learning the classes offered by the JDK. Additionally, try a TreeSet instead of a bare HashSet to maintain the list of words, and compare what (if any) improvement this makes to the program. Exercise 23 //: holding/E23_MoreProbable.java /****************** Exercise 23 ***************** * Starting with Statistics.java, create a * program that runs the test repeatedly and * looks to see if any one number tends to appear 206 Thinking in Java, 4th Edition Annotated Solution Guide * more than the others in the results. ***********************************************/ package holding; import java.util.*; import java.util.Map.Entry; class Counter { int i = 1; public String toString() { return Integer.toString(i); } } class HistoUnit implements Comparable { Counter counter; Integer val; public HistoUnit(Counter counter, Integer val) { this.counter = counter; this.val = val; } public int compareTo(HistoUnit o) { int lv = o.counter.i; int rv = counter.i; return (lv < rv ? -1 : (lv == rv ? 0 : 1)); } public String toString() { return "Value = " + val + ", Occurrences = " + counter.i + "\n"; } } public class E23_MoreProbable { private static Random rand = new Random(47); public static void main(String[] args) { Map m = new HashMap(); for(int i = 0; i < 10000000; i++) { // Produce a number between 0 and 100: int r = rand.nextInt(100); if(m.containsKey(r)) m.get(r).i++; else m.put(r, new Counter()); } // Make a histogram: List lst = new ArrayList(); Iterator> it = m.entrySet().iterator(); while(it.hasNext()) { Holding Your Objects 207 Entry entry = it.next(); lst.add(new HistoUnit( entry.getValue(), entry.getKey())); } Collections.sort(lst); System.out.println("lst = " + lst); } } /* Output: (Sample) lst = [Value = 34, Occurrences = 100764 , Value = 92, Occurrences = 100660 , Value = 55, Occurrences = 100539 , Value = 81, Occurrences = 100503 ... , Value = 6, Occurrences = 99466 , Value = 16, Occurrences = 99427 , Value = 53, Occurrences = 99310 , Value = 86, Occurrences = 99095 ] *///:~ To optimize built-in functionality, we make a list of objects called HistoUnit and sort based on the value of Counter with Collections.sort( ). Printing the list shows the results in order of occurrence. There is no preferred value. To determine how well the randomization function works, you could calculate the distribution and standard deviation. Exercise 24 //: holding/E24_MapOrder2.java /******************* Exercise 24 ************************** * Fill a LinkedHashMap with String keys and objects. * Extract the pairs, sort them based on the keys, and * re-insert them into the Map. *********************************************************/ package holding; import java.util.*; import net.mindview.util.*; public class E24_MapOrder2 { public static void main(String[] args) { Map m1 = new LinkedHashMap( Countries.capitals(25)); System.out.println(m1); String[] keys = m1.keySet().toArray(new String[0]); 208 Thinking in Java, 4th Edition Annotated Solution Guide Arrays.sort(keys); Map m2 = new LinkedHashMap(); for(String key : keys) m2.put(key, m1.get(key)); System.out.println(m2); } } /* Output: {ALGERIA=Algiers, ANGOLA=Luanda, BENIN=Porto-Novo, BOTSWANA=Gaberone, BURKINA FASO=Ouagadougou, BURUNDI=Bujumbura, CAMEROON=Yaounde, CAPE VERDE=Praia, CENTRAL AFRICAN REPUBLIC=Bangui, CHAD=N'djamena, COMOROS=Moroni, CONGO=Brazzaville, DJIBOUTI=Dijibouti, EGYPT=Cairo, EQUATORIAL GUINEA=Malabo, ERITREA=Asmara, ETHIOPIA=Addis Ababa, GABON=Libreville, THE GAMBIA=Banjul, GHANA=Accra, GUINEA=Conakry, BISSAU=Bissau, COTE D'IVOIR (IVORY COAST)=Yamoussoukro, KENYA=Nairobi, LESOTHO=Maseru} {ALGERIA=Algiers, ANGOLA=Luanda, BENIN=Porto-Novo, BISSAU=Bissau, BOTSWANA=Gaberone, BURKINA FASO=Ouagadougou, BURUNDI=Bujumbura, CAMEROON=Yaounde, CAPE VERDE=Praia, CENTRAL AFRICAN REPUBLIC=Bangui, CHAD=N'djamena, COMOROS=Moroni, CONGO=Brazzaville, COTE D'IVOIR (IVORY COAST)=Yamoussoukro, DJIBOUTI=Dijibouti, EGYPT=Cairo, EQUATORIAL GUINEA=Malabo, ERITREA=Asmara, ETHIOPIA=Addis Ababa, GABON=Libreville, GHANA=Accra, GUINEA=Conakry, KENYA=Nairobi, LESOTHO=Maseru, THE GAMBIA=Banjul} *///:~ Exercise 25 //: holding/E25_WordsInfo3.java /****************** Exercise 25 ***************** * Create a Map>. Use * net.mindview.TextFile to open a text file and * read it in a word at a time (use "\\W+" as the * second argument to the TextFile constructor). * Count the words as you read them in, and for each * word in the file, record in the ArrayList * the word count associated with that word - this is, * in effect, the location in the file where that * word was found. ***********************************************/ package holding; import java.util.*; import net.mindview.util.*; Holding Your Objects 209 public class E25_WordsInfo3 { public static void main(String[] args) { Map> stat = new HashMap>(); int wordCount = 0; for(String word : new TextFile("E25_WordsInfo3.java", "\\W+")) { ArrayList loc = stat.get(word); if(loc == null) { loc = new ArrayList(); stat.put(word, loc); } loc.add(++wordCount); } System.out.println(stat); } } /* Output: (Sample) {holding=[1], and=[21, 48], argument=[35], time=[29], where=[76], loc=[120, 125, 127, 134, 135], file=[20, 54, 75], ArrayList=[10, 58, 99, 105, 118, 129], use=[30], 0=[109], Count=[40], associated=[63], out=[139], Map=[8, 97], that=[65, 77], println=[140], null=[126], main=[94], open=[17], each=[50], as=[32, 43], constructor=[39], a=[7, 18, 25, 28], location=[72], in=[24, 47, 52, 56, 69, 73], Use=[12], read=[22, 45], for=[49, 110], the=[33, 37, 41, 53, 57, 60, 71, 74], word=[26, 51, 61, 66, 78, 112, 123, 133], class=[89], put=[132], new=[102, 113, 128], with=[64], them=[46], 25=[5], second=[34], you=[44], Integer=[11, 59, 100, 106, 119, 130], text=[19], it=[23], util=[83, 87], this=[67], static=[92], record=[55], get=[122], W=[31, 117], import=[81, 84], net=[13, 85], public=[88, 91], wordCount=[108, 137], stat=[101, 121, 131, 141], void=[93], effect=[70], mindview=[14, 86], E25_WordsInfo3=[2, 90, 115], String=[9, 95, 98, 104, 111], add=[136], Create=[6], args=[96], words=[42], int=[107], System=[138], found=[80], at=[27], was=[79], to=[16, 36], java=[3, 82, 116], TextFile=[15, 38, 114], if=[124], HashMap=[103], Exercise=[4], is=[68], count=[62]} *///:~ Exercise 26 //: holding/E26_WordsInfo4.java /****************** Exercise 26 ***************** * Take the resulting Map from the previous * exercise and recreate the order of the words as 210 Thinking in Java, 4th Edition Annotated Solution Guide * they appeared in the original file. ***********************************************/ package holding; import java.util.*; import net.mindview.util.*; public class E26_WordsInfo4 { public static void main(String[] args) { Map> stat = new HashMap>(); int wordCount = 0; List origWords = new TextFile("E26_WordsInfo4.java", "\\W+"); for(String word : origWords) { ArrayList loc = stat.get(word); if(loc == null) { loc = new ArrayList(); stat.put(word, loc); } loc.add(++wordCount); } // Now recreate the original order of the words. // We will use an inverted structure, where the key // is the position of the word in the file. Also, // we will sort words based on their positions. TreeMap words = new TreeMap(); for(Map.Entry> entry : stat.entrySet()) for(Integer pos : entry.getValue()) words.put(pos, entry.getKey()); // Test the correctness. System.out.println(origWords); System.out.println(words.values()); } } /* Output: (Sample) [holding, E26_WordsInfo4, java, Exercise, 26, Take, the, resulting, Map, from, the, previous, exercise, and, recreate, the, order, of, the, words, as, they, appeared, in, the, original, file, import, java, util, import, net, mindview, util, public, class, E26_WordsInfo4, public, static, void, main, String, args, Map, String, ArrayList, Integer, stat, new, HashMap, String, ArrayList, Integer, int, wordCount, 0, List, String, origWords, new, TextFile, E26_WordsInfo4, java, W, for, String, word, origWords, ArrayList, Integer, loc, stat, get, word, if, loc, null, loc, new, ArrayList, Integer, stat, put, word, loc, loc, Holding Your Objects 211 add, wordCount, Now, recreate, the, original, order, of, the, words, We, will, use, an, inverted, structure, where, the, key, is, the, position, of, the, word, in, the, file, Also, we, will, sort, words, based, on, their, positions, TreeMap, Integer, String, words, new, TreeMap, Integer, String, for, Map, Entry, String, ArrayList, Integer, entry, stat, entrySet, for, Integer, pos, entry, getValue, words, put, pos, entry, getKey, Test, the, correctness, System, out, println, origWords, System, out, println, words, values] [holding, E26_WordsInfo4, java, Exercise, 26, Take, the, resulting, Map, from, the, previous, exercise, and, recreate, the, order, of, the, words, as, they, appeared, in, the, original, file, import, java, util, import, net, mindview, util, public, class, E26_WordsInfo4, public, static, void, main, String, args, Map, String, ArrayList, Integer, stat, new, HashMap, String, ArrayList, Integer, int, wordCount, 0, List, String, origWords, new, TextFile, E26_WordsInfo4, java, W, for, String, word, origWords, ArrayList, Integer, loc, stat, get, word, if, loc, null, loc, new, ArrayList, Integer, stat, put, word, loc, loc, add, wordCount, Now, recreate, the, original, order, of, the, words, We, will, use, an, inverted, structure, where, the, key, is, the, position, of, the, word, in, the, file, Also, we, will, sort, words, based, on, their, positions, TreeMap, Integer, String, words, new, TreeMap, Integer, String, for, Map, Entry, String, ArrayList, Integer, entry, stat, entrySet, for, Integer, pos, entry, getValue, words, put, pos, entry, getKey, Test, the, correctness, System, out, println, origWords, System, out, println, words, values] *///:~ Map.Entry holds one key-value pair of a map. You can retrieve all entries of a map by calling the map’s entrySet( ) method. Exercise 27 //: holding/E27_CommandQueue.java /****************** Exercise 27 ***************** * Write a class called Command that contains a * String and has a method operation() that * displays the String. Write a second class with * a method that fills a Queue with Command objects * and returns it. Pass the filled Queue to a method * in a third class that consumes the objects in the 212 Thinking in Java, 4th Edition Annotated Solution Guide * Queue and calls their operation() methods. ***********************************************/ package holding; import java.util.*; class Command { private final String cmd; Command(String cmd) { this.cmd = cmd; } public void operation() { System.out.println(cmd); } } class Producer { public static void produce(Queue q) { q.offer(new Command("load")); q.offer(new Command("delete")); q.offer(new Command("save")); q.offer(new Command("exit")); } } class Consumer { public static void consume(Queue q) { while(q.peek() != null) q.remove().operation(); } } public class E27_CommandQueue { public static void main(String[] args) { Queue cmds = new LinkedList(); Producer.produce(cmds); Consumer.consume(cmds); } } /* Output: load delete save exit *///:~ This classic producer-consumer scenario uses the queue to transfer objects from one area of a program (producer) to another (consumer), with the main( ) method acting as a controller. Holding Your Objects 213 Exercise 28 //: holding/E28_PriorityQueue.java /****************** Exercise 28 ***************** * Fill a PriorityQueue (using offer()) with * Double values created using java.util.Random, * then remove the elements using poll() and * display them. ***********************************************/ package holding; import java.util.*; public class E28_PriorityQueue { static Random rand = new Random(47); public static void printQ(Queue queue) { for(Object data = queue.poll(); data != null; data = queue.poll()) System.out.print(data + " "); System.out.println(); } public static void main(String[] args) { PriorityQueue priorityQueue = new PriorityQueue(); for(int i = 0; i < 10; i++) priorityQueue.offer(rand.nextDouble()); printQ(priorityQueue); } } /* Output: 0.0508673570556899 0.16020656493302599 0.18847866977771732 0.2613610344283964 0.2678662084200585 0.5166020801268457 0.5309454508634242 0.7271157860730044 0.7620665811558285 0.8037155449603999 *///:~ Exercise 29 //: holding/E29_PriorityQueueSubtlety.java // {ThrowsException} /****************** Exercise 29 ***************** * Create a simple class that inherits from Object * and contains no members, and show that you cannot * successfully add multiple elements of that class * to a PriorityQueue. This issue will be fully * explained in the Containers in Depth chapter. ***********************************************/ 214 Thinking in Java, 4th Edition Annotated Solution Guide package holding; import java.util.*; class Dummy {} public class E29_PriorityQueueSubtlety { public static void main(String[] args) { PriorityQueue priorityQueue = new PriorityQueue(); System.out.println("Adding 1st instance..."); priorityQueue.offer(new Dummy()); System.out.println("Adding 2nd instance..."); priorityQueue.offer(new Dummy()); } } ///:~ The JDK documentation states: “A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).” The second insertion will trigger an exception. Exercise 30 //: holding/E30_CollectionSequence2.java /****************** Exercise 30 ***************** * Modify CollectionSequence.java so that it does * not inherit from AbstractCollection, but instead * implements Collection. ***********************************************/ package holding; import java.util.*; import typeinfo.pets.*; class CollectionSequence2 extends PetSequence implements Collection { static void display(Iterator it) { while(it.hasNext()) { Pet p = it.next(); System.out.print(p.id() + ":" + p + " "); } System.out.println(); } static void display(Collection pets) { for(Pet p : pets) System.out.print(p.id() + ":" + p + " "); System.out.println(); } Holding Your Objects 215 public int size() { return pets.length; } public Iterator iterator() { return new Iterator() { private int index; public boolean hasNext() { return index < pets.length; } public Pet next() { return pets[index++]; } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } // Other methods, which are also need to be provided. public boolean add(Pet o) { throw new UnsupportedOperationException(); } public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } public void clear() { throw new UnsupportedOperationException(); } public boolean contains(Object o) { if(o == null) return false; for(int i = 0; i < pets.length; i++) if(o.equals(pets[i])) return true; return false; } public boolean containsAll(Collection c) { Iterator it = c.iterator(); while (it.hasNext()) if(!contains(it.next())) return false; return true; } public boolean isEmpty() { return pets.length == 0; } public boolean remove(Object o) { throw new UnsupportedOperationException(); } public boolean removeAll(Collection c) { throw new UnsupportedOperationException(); } public boolean retainAll(Collection c) { throw new UnsupportedOperationException(); } public Object[] toArray() { Object[] result = new Object[pets.length]; 216 Thinking in Java, 4th Edition Annotated Solution Guide System.arraycopy(pets, 0, result, 0, pets.length); return result; } @SuppressWarnings("unchecked") public T[] toArray(T[] a) { if (a.length < pets.length) a = (T[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), pets.length); T[] result = a; System.arraycopy(pets, 0, result, 0, pets.length); if (result.length > pets.length) result[pets.length] = null; return result; } } public class E30_CollectionSequence2 { public static void main(String[] args) { CollectionSequence2 c = new CollectionSequence2(); CollectionSequence2.display(c); CollectionSequence2.display(c.iterator()); System.out.println( Arrays.toString(c.toArray(new Pet[0]))); } } /* Output: 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Manx] *///:~ In the minimalist approach used here, optional operations (as marked in the interface description of Collection) are not supported, nor is equals( ). We also override hashCode( ) and toString( ). Even with these restrictions, the code base is much bigger than that of AbstractCollection in the original version. Note the use of System.arraycopy( ) to copy the arrays quickly inside toArray( ). Exercise 31 //: holding/E31_IterableRandomShapeGenerator.java /****************** Exercise 31 ***************** * Modify polymorphism/shape/RandomShapeGenerator.java * to make it Iterable. You’ll need to add a * constructor that takes the number of elements Holding Your Objects 217 * that you want the iterator to produce before * stopping. Verify that it works. ***********************************************/ package holding; import java.util.*; import polymorphism.shape.*; class RandomShapeGenerator implements Iterable { private Random rand = new Random(47); private final int quantity; RandomShapeGenerator(int quantity) { this.quantity = quantity; } public Iterator iterator() { return new Iterator() { private int count; public boolean hasNext() { return count < quantity; } public Shape next() { ++count; return nextShape(); } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } private Shape nextShape() { switch(rand.nextInt(3)) { default: case 0: return new Circle(); case 1: return new Square(); case 2: return new Triangle(); } } } public class E31_IterableRandomShapeGenerator { public static void main(String[] args) { RandomShapeGenerator rsg = new RandomShapeGenerator(10); for(Shape shape : rsg) System.out.println(shape.getClass().getSimpleName()); } } /* Output: Triangle Triangle 218 Thinking in Java, 4th Edition Annotated Solution Guide Square Triangle Square Triangle Square Triangle Circle Square *///:~ Exercise 32 //: holding/E32_MultiIterableNonCollectionSeq.java /****************** Exercise 32 ***************** * Following the example of MultiIterableClass, * add reversed() and randomized() methods to * NonCollectionSequence.java, as well as making * NonCollectionSequence implement Iterable, and * show that all the approaches work in foreach * statements. ***********************************************/ package holding; import java.util.*; import typeinfo.pets.*; class PetSequence { protected Pet[] pets = Pets.createArray(8); } class NonCollectionSequence extends PetSequence implements Iterable { public Iterable reversed() { return new Iterable() { public Iterator iterator() { return new Iterator() { int current = pets.length - 1; public boolean hasNext() { return current > -1; } public Pet next() { return pets[current--]; } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } }; } public Iterable randomized() { Holding Your Objects 219 return new Iterable() { public Iterator iterator() { List shuffled = new ArrayList(Arrays.asList(pets)); Collections.shuffle(shuffled, new Random(47)); return shuffled.iterator(); } }; } public Iterator iterator() { return new Iterator() { private int index = 0; public boolean hasNext() { return index < pets.length; } public Pet next() { return pets[index++]; } public void remove() { // Not implemented throw new UnsupportedOperationException(); } }; } } public class E32_MultiIterableNonCollectionSeq { public static void main(String[] args) { NonCollectionSequence nc = new NonCollectionSequence (); for(Pet pet : nc.reversed()) System.out.print(pet + " "); System.out.println(); for(Pet pet : nc.randomized()) System.out.print(pet + " "); System.out.println(); for(Pet pet : nc) System.out.print(pet + " "); } } /* Output: Manx Pug Cymric Pug Mutt Cymric Manx Rat Pug Mutt Pug Rat Manx Manx Cymric Cymric Rat Manx Cymric Mutt Pug Cymric Pug Manx *///:~ 220 Thinking in Java, 4th Edition Annotated Solution Guide Error Handling with Exceptions Exercise 1 //: exceptions/E01_SimpleException.java /****************** Exercise 1 ****************** * Create a class with a main() that throws an * object of class Exception inside a try block. * Give the constructor for Exception a String * argument. Catch the exception inside a catch * clause and print the String argument. Add a * finally clause and print a message to prove * you were there. ***********************************************/ package exceptions; public class E01_SimpleException { public static void main(String args[]) { try { throw new Exception("An exception in main"); } catch(Exception e) { System.out.println( "e.getMessage() = " + e.getMessage()); } finally { System.out.println("In finally clause"); } } } /* Output: e.getMessage() = An exception in main In finally clause *///:~ Exercise 2 //: exceptions/E02_NullReference.java /****************** Exercise 2 ****************** * Define an object reference and initialize it * to null. Try to call a method through this 221 * reference. Now wrap the code in a try-catch * clause to catch the exception. ***********************************************/ package exceptions; public class E02_NullReference { public static void main(String args[]) { String s = null; // Causes a NullPointerException: //! s.toString(); try { s.toString(); } catch(Exception e) { System.out.println("Caught exception " + e); } } } /* Output: Caught exception java.lang.NullPointerException *///:~ When you catch the exception, the program runs to completion. Exercise 3 //: exceptions/E03_ArrayIndexBounds.java /****************** Exercise 3 ****************** * Write code to generate and catch an * ArrayIndexOutOfBoundsException. ***********************************************/ package exceptions; public class E03_ArrayIndexBounds { public static void main(String args[]) { char[] array = new char[10]; try { array[10] = 'x'; } catch(ArrayIndexOutOfBoundsException e) { System.out.println("e = " + e); } } } /* Output: e = java.lang.ArrayIndexOutOfBoundsException: 10 *///:~ 222 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 4 //: exceptions/E04_ExceptionClass.java /****************** Exercise 4 ****************** * Create your own exception class using the * extends keyword. Write a constructor for this * class that takes a String argument and stores * it inside the object with a String reference. * Write a method that prints out the stored * String. Create a try-catch clause to exercise * your new exception. ***********************************************/ package exceptions; // Following the instructions to the letter: class MyException extends Exception { String msg; public MyException(String msg) { this.msg = msg; } public void printMsg() { System.out.println("msg = " + msg); } } // Or take a more clever approach, // noting that string storage and printing are // built into Exception: class MyException2 extends Exception { public MyException2(String s) { super(s); } } public class E04_ExceptionClass { public static void main(String args[]) { try { throw new MyException("MyException message"); } catch(MyException e) { e.printMsg(); } try { throw new MyException2("MyException2 message"); } catch(MyException2 e) { System.out.println( "e.getMessage() = " + e.getMessage()); } } Error Handling with Exceptions 223 } /* Output: msg = MyException message e.getMessage() = MyException2 message *///:~ Exercise 5 //: exceptions/E05_Resumption.java /****************** Exercise 5 ****************** * Create your own resumption-like behavior using * a while loop that repeats until an exception * is no longer thrown. ***********************************************/ package exceptions; class ResumerException extends Exception {} class Resumer { static int count = 3; static void f() throws ResumerException { if(--count > 0) throw new ResumerException(); } } public class E05_Resumption { public static void main(String args[]) { while(true) { try { Resumer.f(); } catch(ResumerException e) { System.out.println("Caught " + e); continue; } System.out.println("Got through..."); break; } System.out.println("Successful execution"); } } /* Output: Caught exceptions.ResumerException Caught exceptions.ResumerException Got through... Successful execution *///:~ 224 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 6 //: exceptions/E06_LoggingExceptions.java /****************** Exercise 6 ****************** * Create two exception classes, each of which * performs its own logging automatically. * Demonstrate that these work. ***********************************************/ package exceptions; import java.util.logging.*; import java.io.*; class LoggingException1 extends Exception { private static Logger logger = Logger.getLogger("LoggingException1"); public LoggingException1() { StringWriter trace = new StringWriter(); printStackTrace(new PrintWriter(trace)); logger.severe(trace.toString()); } } class LoggingException2 extends Exception { private static Logger logger = Logger.getLogger("LoggingException2"); public LoggingException2() { StringWriter trace = new StringWriter(); printStackTrace(new PrintWriter(trace)); logger.severe(trace.toString()); } } public class E06_LoggingExceptions { public static void main(String[] args) { try { throw new LoggingException1(); } catch(LoggingException1 e) { System.err.println("Caught " + e); } try { throw new LoggingException2(); } catch(LoggingException2 e) { System.err.println("Caught " + e); } } } /* Output: (45% match) Error Handling with Exceptions 225 Sep 12, 2007 3:10:36 PM exceptions.LoggingException1 SEVERE: exceptions.LoggingException1 at exceptions.E06_LoggingExceptions.main(E06_LoggingExceptions. java:34) Caught exceptions.LoggingException1 Sep 12, 2007 3:10:36 PM exceptions.LoggingException2 SEVERE: exceptions.LoggingException2 at exceptions.E06_LoggingExceptions.main(E06_LoggingExceptions. java:39) Caught exceptions.LoggingException2 *///:~ Each exception employs its own logger instance, LoggingException1 and LoggingException2, respectively. Exercise 7 //: exceptions/E07_LoggedArrayIndexBounds.java /****************** Exercise 7 ****************** * Modify Exercise 3 so that the catch clause logs * the results. ***********************************************/ package exceptions; import java.util.logging.*; import java.io.*; public class E07_LoggedArrayIndexBounds { private static Logger logger = Logger.getLogger("E07_LoggedArrayIndexBounds"); static void logException(Exception e) { StringWriter trace = new StringWriter(); e.printStackTrace(new PrintWriter(trace)); logger.severe(trace.toString()); } public static void main(String args[]) { char[] array = new char[10]; try { array[10] = 'x'; } catch(ArrayIndexOutOfBoundsException e) { logException(e); } } 226 Thinking in Java, 4th Edition Annotated Solution Guide } /* Output: (80% match) 2005.09.16. 10:51:22 E07_LoggedArrayIndexBounds logException SEVERE: java.lang.ArrayIndexOutOfBoundsException: 10 at E07_LoggedArrayIndexBounds.main(E07_LoggedArrayIndexBounds.j ava:20) *///:~ Exercise 8 //: exceptions/E08_ExceptionSpecification.java /****************** Exercise 8 ****************** * Write a class with a method that throws an * exception of the type created in Exercise 4. * Try compiling it without an exception * specification to see what the compiler says. * Add the appropriate exception specification. * Try out your class and its exception inside a * try-catch clause. ***********************************************/ package exceptions; class Thrower { public void f() { // Compiler gives an error: "unreported // exception MyException; must be caught // or declared to be thrown" //! throw new MyException("Inside f()"); } public void g() throws MyException { throw new MyException("Inside g()"); } } public class E08_ExceptionSpecification { public static void main(String args[]) { Thrower t = new Thrower(); try { t.g(); } catch(MyException e) { e.printMsg(); } } } /* Output: msg = Inside g() *///:~ Error Handling with Exceptions 227 Exercise 9 //: exceptions/E09_CatchAll.java /****************** Exercise 9 ****************** * Create three new types of exceptions. Write a * class with a method that throws all three. In * main(), call the method but only use a single * catch clause that will catch all three types * of exceptions. ***********************************************/ package exceptions; class class class class ExBase extends Exception {} Ex1 extends ExBase {} Ex2 extends ExBase {} Ex3 extends ExBase {} class Thrower2 { void f() throws Ex1, Ex2, Ex3 { throw new Ex1(); // You aren't forced to throw all the // exceptions in the specification. } } public class E09_CatchAll { public static void main(String args[]) { Thrower2 t = new Thrower2(); try { t.f(); } catch(ExBase e) { System.out.println("caught " + e); } catch(Exception e) { System.out.println("caught " + e); } } } /* Output: caught exceptions.Ex1 *///:~ We create a common base class for all three exceptions, then catch the common base exception. Alternatively, you can just catch Exception, from which all exceptions inherit. 228 Thinking in Java, 4th Edition Annotated Solution Guide Exercise 10 //: exceptions/E10_ChangeException.java /****************** Exercise 10 ****************** * Create a class with two methods, f() and g(). * In g(), throw an exception of a new type that * you define. In f(), call g(), catch its * exception and, in the catch clause, throw a * different exception (of a second type that you * define). Test your code in main(). ***********************************************/ package exceptions; class AnException extends Exception {} class AnotherException extends Exception {} public class E10_ChangeException { public void g() throws AnException { throw new AnException(); } public void f() throws AnotherException { try { g(); } catch(AnException e) { throw new AnotherException(); } } public static void main(String args[]) { E10_ChangeException ce = new E10_ChangeException(); try { ce.f(); } catch(AnotherException e) { System.out.println("Caught " + e); } } } /* Output: Caught exceptions.AnotherException *///:~ Once the catch clause grabs the exception, it’s handled, and you can do anything with it, including throw another exception. Exercise 11 //: exceptions/E11_ChangeToRuntimeException.java Error Handling with Exceptions 229 // {ThrowsException} /****************** Exercise 11 ****************** * Repeat the previous exercise, but inside the * catch clause, wrap g()'s exception in a * RuntimeException. ***********************************************/ package exceptions; class AnException2 extends Exception {} public class E11_ChangeToRuntimeException { public void g() throws AnException2 { throw new AnException2(); } public void f() { try { g(); } catch(AnException2 e) { throw new RuntimeException(e); } } public static void main(String args[]) { E11_ChangeToRuntimeException ce = new E11_ChangeToRuntimeException(); ce.f(); } } ///:~ RuntimeException now envelopes Exception, so we do not need a try block. Exercise 12 //: exceptions/E12_SequenceExceptions.java // {ThrowsException} /************************ Exercise 12 ******************** * Modify innerclasses/Sequence.java so that it throws an * appropriate exception if you try to put in too many * elements. *********************************************************/ package exceptions; class SequenceFullException extends RuntimeException {} class Sequence2 { private Object[] objects; private int next; 230 Thinking in Java, 4th Edition Annotated Solution Guide public Sequence2(int size) { objects = new Object[size]; } public void add(Object x) { if(next < objects.length) objects[next++] = x; else throw new SequenceFullException(); } private class SequenceSelector implements Selector { private int i; public boolean end() { return i == objects.length; } public Object current() { return objects[i]; } public void next() { if(i < objects.length) i++; } } public Selector selector() { return new SequenceSelector(); } } public class E12_SequenceExceptions { public static void main(String[] args) { Sequence2 sequence = new Sequence2(10); for(int i = 0; i < 11; i++) sequence.add(Integer.toString(i)); } } ///:~ SequenceFullException is more appropriate than RuntimeException because it indicates a programmer error. Exercise 13 //: exceptions/E13_Finally.java // {ThrowsException} /****************** Exercise 13 ***************** * Modify Exercise 9 by adding a finally clause. * Verify that your finally clause is executed, even * if a NullPointerException is thrown. ***********************************************/ package exceptions; public class E13_Finally { public static void throwNull() { throw new NullPointerException(); } public static void main(String args[]) { Thrower2 t = new Thrower2(); Error Handling with Exceptions 231 try { t.f(); } catch(ExBase e) { System.err.println("caught " + } finally { System.out.println("In finally } try { throwNull(); t.f(); } catch(ExBase e) { System.err.println("caught " + } finally { System.out.println("In finally } e); clause A"); e); clause B"); } } ///:~ The output is: caught Ex1 In finally clause A In finally clause B Exception in thread "main" java.lang.NullPointerException at E13_Finally.throwNull(E13_Finally.java:10) at E13_Finally.main(E13_Finally.java:22) Although the code doesn’t catch the NullPointerException, it still executes the finally clause. Exercise 14 //: exceptions/E14_OnOffSwitch.java /****************** Exercise 14 ***************** * Show that OnOffSwitch.java can fail by * throwing a RuntimeException inside the try * block. ***********************************************/ package exceptions; public class E14_OnOffSwitch { static Switch sw = new Switch(); static void f() throws OnOffException1, OnOffException2 { throw new RuntimeException("Inside try"); } public static void main(String[] args) { 232 Thinking in Java, 4th Edition Annotated Solution Guide try { try { sw.on(); // Code that can throw exceptions... f(); sw.off(); } catch(OnOffException1 e) { System.out.println("OnOffException1"); sw.off(); } catch(OnOffException2 e) { System.out.println("OnOffException2"); sw.off(); } } catch(RuntimeException e) { System.out.println(sw); System.out.println("Oops! the exception '" + e + "' slipped through without " + "turning the switch off!"); } } } /* Output: on on Oops! the exception 'java.lang.RuntimeException: Inside try' slipped through without turning the switch off! *///:~ Exercise 15 //: exceptions/E15_WithFinally.java /****************** Exercise 15 ***************** * Show that WithFinally.java doesn't fail by * throwing a RuntimeException inside the try * block. ***********************************************/ package exceptions; public class E15_WithFinally { static Switch sw = new Switch(); static void f() throws OnOffException1, OnOffException2 { throw new RuntimeException("Inside try"); } public static void main(String[] args) { try { try { sw.on(); Error Handling with Exceptions 233 // Code that can throw exceptions... f(); } catch(OnOffException1 e) { System.out.println("OnOffException1"); } catch(OnOffException2 e) { System.out.println("OnOffException2"); } finally { sw.off(); } } catch(RuntimeException e) { System.out.println("Exception '" + e + "'. Did the switch get turned off?"); System.out.println(sw); } } } /* Output: on off Exception 'java.lang.RuntimeException: Inside try'. Did the switch get turned off? off *///:~ The finally clause guarantees the necessary cleanup, even with an unexpected exception. Exercise 16 //: exceptions/E16_CADSystem.java /****************** Exercise 16 ***************** * Modify reusing/CADSystem.java to demonstrate * that returning from the middle of a try-finally * will still perform proper cleanup. ***********************************************/ package exceptions; public class E16_CADSystem { public static void main(String[] args) { reusing.CADSystem x = new reusing.CADSystem(47); try { return; } finally { x.dispose(); } } } /* Output: 234 Thinking in Java, 4th Edition Annotated Solution Guide Shape constructor Shape constructor Drawing Line: 0, 0 Shape constructor Drawing Line: 1, 1 Shape constructor Drawing Line: 2, 4 Shape constructor Drawing Circle Shape constructor Drawing Triangle Combined constructor CADSystem.dispose() Erasing Triangle Shape dispose Erasing Circle Shape dispose Erasing Line: 2, 4 Shape dispose Erasing Line: 1, 1 Shape dispose Erasing Line: 0, 0 Shape dispose Shape dispose *///:~ Exercise 17 //: exceptions/E17_Frog.java /****************** Exercise 17 ***************** * Modify polymorphism/Frog.java so that it uses * try-finally to guarantee proper cleanup, and * show that this works even if you return from the * middle of the try-finally. ***********************************************/ package exceptions; // Frog.dispose() is protected, cannot be called directly class Frog2 extends polymorphism.Frog { protected void dispose() { super.dispose(); } } public class E17_Frog { public static void main(String[] args) { Frog2 frog = new Frog2(); try { Error Handling with Exceptions 235 // No return in the middle... } finally { frog.dispose(); } frog = new Frog2(); try { // With return in the middle... return; } finally { frog.dispose(); } } } /* Output: Creating Characteristic is alive Creating Description Basic Living Creature LivingCreature() Creating Characteristic has heart Creating Description Animal not Vegetable Animal() Creating Characteristic can live in water Creating Description Both water and land Amphibian() Creating Characteristic Croaks Creating Description Eats Bugs Frog() Frog dispose disposing Description Eats Bugs disposing Characteristic Croaks Amphibian dispose disposing Description Both water and land disposing Characteristic can live in water Animal dispose disposing Description Animal not Vegetable disposing Characteristic has heart LivingCreature dispose disposing Description Basic Living Creature disposing Characteristic is alive Creating Characteristic is alive Creating Description Basic Living Creature LivingCreature() Creating Characteristic has heart Creating Description Animal not Vegetable Animal() Creating Characteristic can live in water Creating Description Both water and land Amphibian() Creating Characteristic Croaks 236 Thinking in Java, 4th Edition Annotated Solution Guide Creating Description Eats Bugs Frog() Frog dispose disposing Description Eats Bugs disposing Characteristic Croaks Amphibian dispose disposing Description Both water and land disposing Characteristic can live in water Animal dispose disposing Description Animal not Vegetable disposing Characteristic has heart LivingCreature dispose disposing Description Basic Living Creature disposing Characteristic is alive *///:~ As you see, both cases elicit the proper cleanup. Exercise 18 //: exceptions/E18_LostMessage.java /****************** Exercise 18 ***************** * Add a second level of exception loss to * LostMessage.java so that the HoHumException is * itself replaced by a third exception. ***********************************************/ package exceptions; class YetAnotherException extends Exception { public String toString() { return "Yet another exception"; } } class LostMessage2 { void f() throws VeryImportantException { throw new VeryImportantException(); } void dispose() throws HoHumException { throw new HoHumException(); } void cleanup() throws YetAnotherException { throw new YetAnotherException(); } } Error Handling with Exceptions 237 public class E18_LostMessage { public static void main(String[] args) { try { LostMessage2 lm = new LostMessage2(); try { try { lm.f(); } finally { lm.dispose(); } } finally { lm.cleanup(); } } catch(Exception e) { System.out.println(e); } } } /* Output: Yet another exception *///:~ Again, the outer try block enables the program to run to completion. Note, however, that YetAnotherException appears in the output, not VeryImportantException or HoHumException. Exercise 19 //: exceptions/E19_GuardedLostMessage.java /****************** Exercise 19 ***************** * Repair the problem in LostMessage.java by * guarding the call in the finally clause. ***********************************************/ package exceptions; public class E19_GuardedLostMessage { void f() throws VeryImportantException { throw new VeryImportantException(); } void dispose() throws HoHumException { throw new HoHumException(); } public static void main(String[] args) { try { LostMessage lm = new LostMessage(); try { lm.f(); 238 Thinking in Java, 4th Edition Annotated Solution Guide } finally { try { lm.dispose(); } catch(HoHumException e) { System.out.println(e); } } } catch(Exception e) { System.out.println(e); } } } /* Output: A trivial exception A very important exception! *///:~ This time, the outer try block properly reports VeryImportantException. Exercise 20 //: exceptions/E20_UmpireArgument.java /****************** Exercise 20 ***************** * Modify StormyInning.java by adding an * UmpireArgument exception type and methods * that throw this exception. Test the modified * hierarchy. ***********************************************/ package exceptions; class class class class BaseballException extends Exception {} Foul extends BaseballException {} Strike extends BaseballException {} UmpireArgument extends BaseballException {} abstract class Inning { Inning() throws BaseballException {} void event () throws BaseballException {} abstract void atBat() throws Strike, Foul, UmpireArgument; abstract void decision() throws UmpireArgument; void walk() {} // Throws nothing } class StormException extends Exception {} class RainedOut extends StormException {} class PopFoul extends Foul {} Error Handling with Exceptions 239 interface Storm { public void event() throws RainedOut; public void rainHard() throws RainedOut; } class StormyInning extends Inning implements Storm { StormyInning() throws RainedOut, BaseballException {} StormyInning(String s) throws Foul, BaseballException {} public void rainHard() throws RainedOut {} public void event() {} void atBat() throws PopFoul, UmpireArgument { throw new UmpireArgument(); } void decision() throws UmpireArgument { throw new UmpireArgument(); } } public class E20_UmpireArgument { public static void main(String[] args) { // Same code as before, still catches // the new exception: try { StormyInning si = new StormyInning(); si.atBat(); } catch(PopFoul e) { System.out.println("Pop foul"); } catch(RainedOut e) { System.out.println("Rained out"); } catch(BaseballException e) { System.out.println("Generic error"); } // Strike not thrown in derived version. try { Inning i = new StormyInning(); i.atBat(); } catch(Strike e) { System.out.println("Strike"); } catch(Foul e) { System.out.println("Foul"); } catch(RainedOut e) { System.out.println("Rained out"); } catch(BaseballException e) { 240 Thinking in Java, 4th Edition Annotated Solution Guide System.out.println("Generic baseball exception"); } // Or you can add code to catch the // specific type of exception: try { StormyInning si = new StormyInning(); si.atBat(); si.decision(); } catch(PopFoul e) { System.out.println("Pop foul"); } catch(RainedOut e) { System.out.println("Rained out"); } catch(UmpireArgument e) { System.out.println( "Argument with the umpire"); } catch(BaseballException e) { System.out.println("Generic error"); } } } /* Output: Generic error Generic baseball exception Argument with the umpire *///:~ The exception hierarchy in the first two lines allows us to add new exceptions without forcing changes to existing code. Exercise 21 //: exceptions/E21_ConstructorExceptions.java /****************** Exercise 21 ***************** * Demonstrate that a derived-class constructor * cannot catch exceptions thrown by its * base-class constructor. ***********************************************/ package exceptions; class Except1 extends Exception { public Except1(String s) { super(s); } } class BaseWithException { public BaseWithException() throws Except1 { throw new Except1("thrown by BaseWithException"); } Error Handling with Exceptions 241 } class DerivedWE extends BaseWithException { // Produces compile-time error: // unreported exception Except1 // ! public DerivedWE() {} // Gives compile error: call to super must be // first statement in constructor: //! public DerivedWE() { //! try { //! super(); //! } catch(Except1 ex1) { //! } //! } public DerivedWE() throws Except1 {} } public class E21_ConstructorExceptions { public static void main(String args[]) { try { new DerivedWE(); } catch(Except1 ex1) { System.out.println("Caught " + ex1); } } } /* Output: Caught exceptions.Except1: thrown by BaseWithException *///:~ Remember, when you throw an exception, the compiler creates no object. This is why a derived-class constructor cannot catch a base-class constructor exception: it can’t “recover” from the exception failure, since there’s no baseclass sub-object. Exercise 22 //: exceptions/E22_FailingConstructor.java /****************** Exercise 22 ***************** * Create a class called FailingConstructor with a * constructor that might fail partway through the * construction process and throw an exception. In * main(), write code that properly guards against * this failure. ***********************************************/ package exceptions; 242 Thinking in Java, 4th Edition Annotated Solution Guide class ConstructionException extends Exception {} class FailingConstructor { FailingConstructor(boolean fail) throws ConstructionException { if(fail) throw new ConstructionException(); } } public class E22_FailingConstructor { public static void main(String args[]) { for(boolean b = false; ; b = !b) try { System.out.println("Constructing..."); FailingConstructor fc = new FailingConstructor(b); try { System.out.println("Processing..."); } finally { System.out.println("Cleanup..."); } } catch(ConstructionException e) { System.out.println("Construction has failed: " + e); break; } } } /* Output: Constructing... Processing... Cleanup... Constructing... Construction has failed: exceptions.ConstructionException *///:~ This program shows both the success and failure of the construction process. The code follows the idiom presented in TIJ4. Exercise 23 //: exceptions/E23_FailingConstructor2.java /****************** Exercise 23 ***************** * Add a class with a dispose() method to the * previous exercise. Modify FailingConstructor so * that the constructor creates one of these * disposable objects as a member object, after which * the constructor might throw an exception, after * which it creates a second disposable member object. Error Handling with Exceptions 243 * Write code to properly guard against failure, and * in main() verify that all possible failure * situations are covered. ***********************************************/ package exceptions; class WithDispose { private final int id; WithDispose(int id) { this.id = id; } void dispose() { System.out.println("WithDispose.dispose(): " + id); } } class FailingConstructor2 { private final WithDispose wd1, wd2; FailingConstructor2(boolean fail) throws ConstructionException { wd1 = new WithDispose(1); try { // "Simulate" other code that might throw exceptions if(fail) throw new ConstructionException(); } catch(ConstructionException e) { wd1.dispose(); throw e; } wd2 = new WithDispose(2); } } public class E23_FailingConstructor2 { public static void main(String args[]) { for(boolean b = false; ; b = !b) try { System.out.println("Constructing..."); FailingConstructor2 fc = new FailingConstructor2(b); try { System.out.println("Processing..."); } finally { // We do not have a decent method to call for // cleanup! System.out.println("Cleanup..."); } } catch(ConstructionException e) { System.out.println("Construction has failed: " + e); break; } 244 Thinking in Java, 4th Edition Annotated Solution Guide } } /* Output: Constructing... Processing... Cleanup... Constructing... WithDispose.dispose(): 1 Construction has failed: exceptions.ConstructionException *///:~ Exercise 24 //: exceptions/E24_FailingConstructor3.java /****************** Exercise 24 ***************** * Add a dispose() method to the FailingConstructor * class and write code to properly use this class. ***********************************************/ package exceptions; class FailingConstructor3 { private final WithDispose wd1, wd2; FailingConstructor3(boolean fail) throws ConstructionException { wd1 = new WithDispose(1); try { // "Simulate" other code that might throw exceptions if(fail) throw new ConstructionException(); } catch(ConstructionException e) { wd1.dispose(); throw e; } wd2 = new WithDispose(2); } void dispose() { wd2.dispose(); wd1.dispose(); } } public class E24_FailingConstructor3 { public static void main(String args[]) { for(boolean b = false; ; b = !b) try { System.out.println("Constructing..."); FailingConstructor3 fc = new FailingConstructor3(b); try { Error Handling with Exceptions 245 System.out.println("Processing..."); } finally { System.out.println("Cleanup..."); fc.dispose(); } } catch(ConstructionException e) { System.out.println("Construction has failed: " + e); break; } } } /* Output: Constructing... Processing... Cleanup... WithDispose.dispose(): 2 WithDispose.dispose(): 1 Constructing... WithDispose.dispose(): 1 Construction has failed: exceptions.ConstructionException *///:~ Exercise 25 //: exceptions/E25_ThreeLevelExceptions.java /****************** Exercise 25 ****************** * Create a three-level hierarchy of exceptions. * Now create a base class A with a method that * throws an exception at the base of your * hierarchy. Inherit B from A and override the * method so it throws an exception at level two * of your hierarchy. Repeat by inheriting class * C from B. In main(), create a C and upcast it * to A, then call the method. ***********************************************/ package exceptions; class Level1Exception extends Exception {} class Level2Exception extends Level1Exception {} class Level3Exception extends Level2Exception {} class A { public void f() throws Level1Exception { throw new Level1Exception(); } } 246 Thinking in Java, 4th Edition Annotated Solution Guide class B extends A { public void f() throws Level2Exception { throw new Level2Exception(); } } class C extends B { public void f() throws Level3Exception { throw new Level3Exception(); } } public class E25_ThreeLevelExceptions { public static void main(String args[]) { A a = new C(); try { a.f(); } catch(Level1Exception e) { System.out.println("Caught " + e); } } } /* Output: Caught exceptions.Level3Exception *///:~ The compiler forces you to catch a Level1Exception because that’s what A.f( ) throws. Exercise 26 //: exceptions/E26_MainException.java // {ThrowsException} /****************** Exercise 26 ****************** * Change the file name string in MainException.java to * name a file that doesn't exist. Run the program and * note the result ************************************************/ package exceptions; import java.io.*; public class E26_MainException { // Pass all exceptions to the console: public static void main(String[] args) throws Exception { // Open the file: FileInputStream file = new FileInputStream("DoesnotExist.file"); Error Handling with Exceptions 247 // Use the file ... // Close the file: file.close(); } } ///:~ The output is: Exception in thread "main" java.io.FileNotFoundException: DoesnotExist.file (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at E27_MainException.main(E27_MainException.java:14) Exercise 27 //: exceptions/E27_RuntimeArrayIndexBounds.java // {ThrowsException} /****************** Exercise 27 ****************** * Modify Exercise 3 to convert the exception to a * RuntimeException. ***********************************************/ package exceptions; public class E27_RuntimeArrayIndexBounds { public static void main(String args[]) { char[] array = new char[10]; try { array[10] = 'x'; } catch(ArrayIndexOutOfBoundsException e) { throw new RuntimeException(e); } } } ///:~ Exercise 28 //: exceptions/E28_RuntimeExceptionClass.java // {ThrowsException} /****************** Exercise 28 ****************** * Modify Exercise 4 so that the custom exception * class inherits from RuntimeException, and show 248 Thinking in Java, 4th Edition Annotated Solution Guide * that the compiler allows you to leave out the * try block. ***********************************************/ package exceptions; class MyRuntimeException extends RuntimeException { public MyRuntimeException(String s) { super(s); } } public class E28_RuntimeExceptionClass { public static void main(String args[]) { throw new MyRuntimeException("MyRuntimeException msg"); } } ///:~ MyRuntimeException is now inherited from RuntimeException, so we do not need a try block. Exercise 29 //: exceptions/E29_StormyInning2.java // {ThrowsException} /****************** Exercise 29 ****************** * Modify all the exception types in StormyInning.java * so that they extend RuntimeException, and show * that no exception specifications or try blocks are * necessary. Remove the ‘//!’ comments and show how * the methods can be compiled without specifications. ***********************************************/ package exceptions; class BaseballException2 extends RuntimeException {} class Foul2 extends BaseballException2 {} class Strike2 extends BaseballException2 {} abstract class Inning2 { Inning2() {} public void event() {} abstract void atBat(); void walk() {} } class StormException2 extends RuntimeException {} class RainedOut2 extends StormException2 {} class PopFoul2 extends Foul2 {} Error Handling with Exceptions 249 interface Storm2 { void event(); void rainHard(); } class StormyInning2 extends Inning2 implements Storm2 { StormyInning2() {} StormyInning2(String s) {} public void rainHard() {} void atBat() { throw new PopFoul2(); } } public class E29_StormyInning2 { public static void main(String[] args) { StormyInning2 si = new StormyInning2(); si.atBat(); } } ///:~ Exercise 30 //: exceptions/E30_Human.java /****************** Exercise 30 ****************** * Modify main() in Human.java so that the * technique in TurnOffChecking.java is used to * handle the different types of exceptions. ***********************************************/ package exceptions; public class E30_Human { static void throwRuntimeException(int type) { try { switch(type) { case 0: throw new Annoyance(); case 1: throw new Sneeze(); default: return; } } catch(Exception e) { // Adapt to unchecked: throw new RuntimeException(e); } } public static void main(String[] args) { // Let RuntimeExceptions go out of the method: throwRuntimeException(2); // Or let catch exceptions: for(int i = 0; i < 2; i++) 250 Thinking in Java, 4th Edition Annotated Solution Guide try { throwRuntimeException(i); } catch(RuntimeException re) { try { throw re.getCause(); } catch(Sneeze e) { System.out.println("Caught Sneeze"); } catch(Annoyance e) { System.out.println("Caught Annoyance"); } catch(Throwable t) { System.out.println(t); } } } } /* Output: Caught Annoyance Caught Sneeze *///:~ Error Handling with Exceptions 251 Strings Exercise 1 Invoke the javap tool (with javap –c SprinklerSystem) to generate this simplified output for the toString( ) method of the SprinklerSystem class: 0: 3: 4: 7: 9: 12: 13: 16: 19: 21: 24: 26: 29: 30: 33: 36: 38: 41: 43: 46: 47: 50: 53: 55: 58: 60: 63: 64: 67: 70: 72: 75: 77: 80: 81: new dup invokespecial ldc #7; invokevirtual aload_0 getfield invokevirtual ldc #10; invokevirtual ldc #11; invokevirtual aload_0 getfield invokevirtual ldc #10; invokevirtual ldc #13; invokevirtual aload_0 getfield invokevirtual ldc #10; invokevirtual ldc #15; invokevirtual aload_0 getfield invokevirtual ldc #17; invokevirtual ldc #18; invokevirtual aload_0 getfield #5; //class StringBuilder #6; //StringBuilder."":() #8; //StringBuilder.append:() #9; #8; //StringBuilder.append:() #8; //StringBuilder.append:() #8; //StringBuilder.append:() #12; #8; //StringBuilder.append:() #8; //StringBuilder.append:() #8; //StringBuilder.append:() #14; #8; //StringBuilder.append:() #8; //StringBuilder.append:() #8; //StringBuilder.append:() #16; #8; //StringBuilder.append:() #8; //StringBuilder.append:() #8; //StringBuilder.append:() #19; 253 84: 87: 89: 92: 94: 97: 98: 101: 104: 106: 109: 111: 114: 115: 118: 121: 124: invokevirtual ldc #10; invokevirtual ldc #21; invokevirtual aload_0 getfield invokevirtual ldc #10; invokevirtual ldc #24; invokevirtual aload_0 getfield invokevirtual invokevirtual areturn #20; //StringBuilder.append:() #8; //StringBuilder.append:() #8; //StringBuilder.append:() #22; #23; //StringBuilder.append:() #8; //StringBuilder.append:() #8; //StringBuilder.append:() #4; #25; //StringBuilder.append:() #26; //StringBuilder.toString:() The operations are simple (e.g., no looping is involved), so the compiler generates reasonably optimized code. Note that you create only one instance of StringBuilder. Though different indexes refer to StringBuilder.append( ), each overloaded version has a unique index. Exercise 2 //: strings/E02_RepairInfinite.java /****************** Exercise 2 ****************** * Repair InfiniteRecursion.java. ************************************************/ package strings; import java.util.*; public class E02_RepairInfinite { public String toString() { return " E02_RepairInfinite address: " + super.toString() + "\n"; } public static void main(String[] args) { List v = new ArrayList(); for(int i = 0; i < 10; i++) v.add(new E02_RepairInfinite()); System.out.println(v); } 254 Thinking in Java, 4th Edition Annotated Solution Guide } /* Output: (70% match) [ E02_RepairInfinite address: E02_RepairInfinite@3e25a5 , E02_RepairInfinite address: E02_RepairInfinite@19821f , E02_RepairInfinite address: E02_RepairInfinite@addbf1 , E02_RepairInfinite address: E02_RepairInfinite@42e816 , E02_RepairInfinite address: E02_RepairInfinite@9304b1 , E02_RepairInfinite address: E02_RepairInfinite@190d11 , E02_RepairInfinite address: E02_RepairInfinite@a90653 , E02_RepairInfinite address: E02_RepairInfinite@de6ced , E02_RepairInfinite address: E02_RepairInfinite@c17164 , E02_RepairInfinite address: E02_RepairInfinite@1fb8ee3 ] *///:~ Exercise 3 //: strings/E03_TurtleRedirected.java /****************** Exercise 3 ****************** * Modify Turtle.java so that it sends all output * to System.err. ***********************************************/ package strings; import java.util.*; class Turtle { private final String name; private final Formatter f; public Turtle(String name, Formatter f) { this.name = name; this.f = f; } public void move(int x, int y) { f.format("%s The Turtle is at (%d,%d)\n", name, x, y); } } public class E03_TurtleRedirected { public static void main(String[] args) { Formatter f = new Formatter(System.err); Turtle tommy = new Turtle("Tommy",f), terry = new Turtle("Terry",f); tommy.move(0,0); terry.move(4,8); tommy.move(3,4); terry.move(2,5); tommy.move(3,3); Strings 255 terry.move(3,3); } } /* Output: Tommy The Turtle is at Terry The Turtle is at Tommy The Turtle is at Terry The Turtle is at Tommy The Turtle is at Terry The Turtle is at *///:~ (0,0) (4,8) (3,4) (2,5) (3,3) (3,3) Exercise 4 //: strings/E04_CustomizableReceipt.java /****************** Exercise 4 ****************** * Modify Receipt.java so that the widths are all * controlled by a single set of constant values. * The goal is to allow you to easily change a * width by changing a single value in one place. ************************************************/ package strings; import java.util.*; class Receipt { public static final int ITEM_WIDTH = 15; public static final int QTY_WIDTH = 5; public static final int PRICE_WIDTH = 10; private static final String TITLE_FRMT = "%-" + ITEM_WIDTH + "s %" + QTY_WIDTH + "s %" + PRICE_WIDTH + "s\n"; private static final String ITEM_FRMT = "%-" + ITEM_WIDTH + "." + ITEM_WIDTH + "s %" + QTY_WIDTH + "d %" + PRICE_WIDTH + ".2f\n"; private static final String TOTAL_FRMT = "%-" + ITEM_WIDTH + "s %" + QTY_WIDTH + "s %" + PRICE_WIDTH + ".2f\n"; private double total = 0; Formatter f = new Formatter(System.out, Locale.US); public void printTitle() { f.format(TITLE_FRMT, "Item", "Qty", "Price"); f.format(TITLE_FRMT, "----", "---", "-----"); } public void print(String name, int qty, double price) { f.format(ITEM_FRMT, name, qty, price); total += price; } 256 Thinking in Java, 4th Edition Annotated Solution Guide public void printTotal() { f.format(TOTAL_FRMT, "Tax", "", total*0.06); f.format(TITLE_FRMT, "", "", "-----"); f.format(TOTAL_FRMT, "Total", "", total * 1.06); } } public class E04_CustomizableReceipt { public static void main(String[] args) { Receipt receipt = new Receipt(); receipt.printTitle(); receipt.print("Jack's Magic Beans", 4, 4.25); receipt.print("Princess Peas", 3, 5.1); receipt.print("Three Bears Porridge", 1, 14.29); receipt.printTotal(); } } /* Output: Item Qty Price ---------Jack's Magic Be 4 4.25 Princess Peas 3 5.10 Three Bears Por 1 14.29 Tax 1.42 ----Total 25.06 *///:~ Exercise 5 //: strings/E05_ComplexConversion.java /****************** Exercise 5 ****************** * For each of the basic conversion types in the * above table, write the most complex formatting * expression possible. That is, use all the * possible format specifiers available for that * conversion type. ************************************************/ package strings; import java.math.*; import java.util.*; import static net.mindview.util.Print.*; public class E05_ComplexConversion { public static void main(String[] args) { Formatter f = new Formatter(System.out, Locale.US); Strings 257 char u = 'a'; print("u = 'a'"); f.format("s: %1$-#10s\n", u); f.format("c: %1$-10c\n", u); f.format("b: %1$-10.10b\n", u); f.format("h: %1$-10.10h\n", u); int v = 1000; print("v = 1000"); f.format("d 1: %1$(,0+10d\n", v); f.format("d 2: %1$-(, 10d\n", v); f.format("d 3, v = -v: %1$-(, 10d\n", -v); f.format("c, v = 121: %1$-10c\n", 121); f.format("b: %1$-10.10b\n", v); f.format("s: %1$#-10.10s\n", v); f.format("x: %1$-#10x\n", v); f.format("h: %1$-10.10h\n", v); BigInteger w = new BigInteger("50000000000000"); print("w = new BigInteger(\"50000000000000\")"); f.format("d 1: %1$(,0+10d\n", w); f.format("d 2: %1$-(, 10d\n", w); f.format("d 3, w = -w: %1$-(, 10d\n", w.negate()); f.format("b: %1$-10.10b\n", w); f.format("s: %1$#-10.10s\n", w); f.format("x 1: %1$(0+10x\n", w); f.format("x 2: %1$-( 10x\n", w); f.format("x 3, w = -w: %1$-( 10x\n", w.negate()); f.format("h: %1$-10.10h\n", w); double x = 179.543; print("x = 179.543"); f.format("b: %1$-10.10b\n", x); f.format("s: %1$#-10.10s\n", x); f.format("f 1: %1$#(,0+10.2f\n", x); f.format("f 2: %1$#(,- 10.2f\n", x); f.format("f 3, x = -x: %1$#(,0+10.2f\n", -x); f.format("e 1: %1$#(0+10.2e\n", x); f.format("e 2: %1$#(- 10.2e\n", x); f.format("e 3, x = -x: %1$#(0+10.2e\n", -x); f.format("h: %1$-10.10h\n", x); Object y = new Object(); print("y = new Object()"); f.format("b: %1$-10.10b\n", y); f.format("s: %1$#-10.10s\n", y); f.format("h: %1$-10.10h\n", y); boolean z = false; print("z = false"); f.format("b: %1$-10.10b\n", z); f.format("s: %1$#-10.10s\n", z); 258 Thinking in Java, 4th Edition Annotated Solution Guide f.format("h: %1$-10.10h\n", z); // A special no argument conversion type f.format("%%: %-10%"); } } /* Output: (95% match) u = 'a' s: a c: a b: true h: 61 v = 1000 d 1: +00001,000 d 2: 1,000 d 3, v = -v: (1,000) c, v = 121: y b: true s: 1000 x: 0x3e8 h: 3e8 w = new BigInteger("50000000000000") d 1: +50,000,000,000,000 d 2: 50,000,000,000,000 d 3, w = -w: (50,000,000,000,000) b: true s: 5000000000 x 1: +2d79883d2000 x 2: 2d79883d2000 x 3, w = -w: (2d79883d2000) h: 8842a1a7 x = 179.543 b: true s: 179.543 f 1: +000179.54 f 2: 179.54 f 3, x = -x: (00179.54) e 1: +01.80e+02 e 2: 1.80e+02 e 3, x = -x: (1.80e+02) h: 1ef462c y = new Object() b: true s: java.lang. h: 3e25a5 z = false b: false s: false h: 4d5 Strings 259 %: % *///:~ The above program includes format specifiers not mentioned in TIJ4 (the JDK documentation elaborates on all of them). The format specifiers applicable to a particular conversion type depends on the variable type, as well. We omitted incompatible cases where a conversion type is not appropriate for a particular variable type. Note that not all combinations of format specifiers are possible. For example, giving both the ‘-’ and ‘0’ flags throws an IllegalFormatFlagsException. We omitted the ‘,’ flag for the ‘e’ because it’s inapplicable as a conversion type (though the J2SE5 API documentation mistakenly says otherwise). Exercise 6 //: strings/E06_ClassDump.java /****************** Exercise 6 ****************** * Create a class that contains int, long, float * and double fields. Create a toString() method * for this class that uses String.format(), and * demonstrate that your class works correctly. ************************************************/ package strings; import java.util.*; import static java.lang.String.*; class DataHolder { int intField = 1; long longField = 2L; float floatField = 3.0f; double doubleField = 4.0; public String toString() { StringBuilder result = new StringBuilder("DataHolder: \n"); result.append( format(Locale.US, "intField: %d\n", intField)); result.append( format(Locale.US, "longField: %d\n", longField)); result.append( format(Locale.US, "floatField: %f\n", floatField)); result.append( format(Locale.US, "doubleField: %e\n", doubleField)); return result.toString(); } } 260 Thinking in Java, 4th Edition Annotated Solution Guide public class E06_ClassDump { public static void main(String[] args) { System.out.println(new DataHolder()); } } /* Output: DataHolder: intField: 1 longField: 2 floatField: 3.000000 doubleField: 4.000000e+00 *///:~ Exercise 7 //: strings/E07_SentenceChecker.java /****************** Exercise 7 ****************** * Using the documentation for java.util.regex.Pattern * as a resource, write and test a regular expression * that checks a sentence to see that it begins with a * capital letter and ends with a period. ************************************************/ package strings; public class E07_SentenceChecker { public static boolean matches(String text) { return text.matches("\\p{javaUpperCase}.*\\."); } public static void main(String[] args) { System.out.println(matches("This is correct.")); System.out.println(matches("bad sentence 1.")); System.out.println(matches("Bad sentence 2")); System.out.println(matches("This is also correct...")); } } /* Output: true false false true *///:~ It’s easy to test for a capital letter in a regular expression, but not in all languages. This exercise highlights the difficulty of writing multilingual applications. In the rest of the text, we use only U.S. English. Strings 261 Exercise 8 //: strings/E08_Splitting2.java /****************** Exercise 8 ****************** * Split the string Splitting.knights on the words * "the" or "you." ************************************************/ package strings; import java.util.*; public class E08_Splitting2 { public static void split(String regex) { System.out.println( Arrays.toString(Splitting.knights.split(regex))); } public static void main(String[] args) { split("the|you"); } } /* Output: [Then, when , have found , shrubbery, , must cut down , mightiest tree in , forest... with... a herring!] *///:~ Exercise 9 //: strings/E09_Replacing2.java /****************** Exercise 9 ****************** * Using the documentation for java.util.regex.Pattern * as a resource, replace all the vowels in * Splitting.knights with underscores. ************************************************/ package strings; public class E09_Replacing2 { public static void main(String[] args) { System.out.println(Splitting.knights.replaceAll( "(?i)[aeiou]","_")); } } /* Output: Th_n, wh_n y__ h_v_ f__nd th_ shr_bb_ry, y__ m_st c_t d_wn th_ m_ght__st tr__ _n th_ f_r_st... w_th... _ h_rr_ng! *///:~ 262 Thinking in Java, 4th Edition Annotated Solution Guide The embedded flag expression (?i) enables case-insensitive matching. The regular expression demonstrates how to handle mixed uppercase/lowercase vowels, (though none occur above). Exercise 10 //: strings/E10_CheckForMatch.java /********************** Exercise 10 ********************** * For the phrase "Java now has regular expressions" * evaluate whether the following expressions will find a * match: * * ^Java * \Breg.* * n.w\s+h(a|i)s * s? * s* * s+ * s{4} * s{1}. * s{0,3} ********************************************************/ package strings; import java.util.regex.*; public class E10_CheckForMatch { public static void main(String[] args) { String source = "Java now has regular expressions"; String[] regEx = {"^Java", "\\Breg.*", "n.w\\s+h(a|i)s", "s?", "s*", "s+", "s{4}", "s{1}.", "s{0,3}"}; System.out.println("Source string: " + source); for(String pattern : regEx) { System.out.println( "Regular expression: \"" + pattern + "\""); Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(source); while(m.find()) { System.out.println("Match \"" + m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1)); } } } } /* Output: Strings 263 Source string: Java now has regular expressions Regular expression: "^Java" Match "Java" at positions 0-3 Regular expression: "\Breg.*" Regular expression: "n.w\s+h(a|i)s" Match "now has" at positions 5-11 Regular expression: "s?" Match "" at positions 0--1 Match "" at positions 1-0 Match "" at positions 2-1 Match "" at positions 3-2 Match "" at positions 4-3 Match "" at positions 5-4 Match "" at positions 6-5 Match "" at positions 7-6 Match "" at positions 8-7 Match "" at positions 9-8 Match "" at positions 10-9 Match "s" at positions 11-11 Match "" at positions 12-11 Match "" at positions 13-12 Match "" at positions 14-13 Match "" at positions 15-14 Match "" at positions 16-15 Match "" at positions 17-16 Match "" at positions 18-17 Match "" at positions 19-18 Match "" at positions 20-19 Match "" at positions 21-20 Match "" at positions 22-21 Match "" at positions 23-22 Match "" at positions 24-23 Match "" at positions 25-24 Match "s" at positions 26-26 Match "s" at positions 27-27 Match "" at positions 28-27 Match "" at positions 29-28 Match "" at positions 30-29 Match "s" at positions 31-31 Match "" at positions 32-31 Regular expression: "s*" Match "" at positions 0--1 Match "" at positions 1-0 Match "" at positions 2-1 Match "" at positions 3-2 Match "" at positions 4-3 Match "" at positions 5-4 264 Thinking in Java, 4th Edition Annotated Solution Guide Match "" at positions 6-5 Match "" at positions 7-6 Match "" at positions 8-7 Match "" at positions 9-8 Match "" at positions 10-9 Match "s" at positions 11-11 Match "" at positions 12-11 Match "" at positions 13-12 Match "" at positions 14-13 Match "" at positions 15-14 Match "" at positions 16-15 Match "" at positions 17-16 Match "" at positions 18-17 Match "" at positions 19-18 Match "" at positions 20-19 Match "" at positions 21-20 Match "" at positions 22-21 Match "" at positions 23-22 Match "" at positions 24-23 Match "" at positions 25-24 Match "ss" at positions 26-27 Match "" at positions 28-27 Match "" at positions 29-28 Match "" at positions 30-29 Match "s" at positions 31-31 Match "" at positions 32-31 Regular expression: "s+" Match "s" at positions 11-11 Match "ss" at positions 26-27 Match "s" at positions 31-31 Regular expression: "s{4}" Regular expression: "s{1}." Match "s " at positions 11-12 Match "ss" at positions 26-27 Regular expression: "s{0,3}" Match "" at positions 0--1 Match "" at positions 1-0 Match "" at positions 2-1 Match "" at positions 3-2 Match "" at positions 4-3 Match "" at positions 5-4 Match "" at positions 6-5 Match "" at positions 7-6 Match "" at positions 8-7 Match "" at positions 9-8 Match "" at positions 10-9 Match "s" at positions 11-11 Strings 265 Match "" at positions 12-11 Match "" at positions 13-12 Match "" at positions 14-13 Match "" at positions 15-14 Match "" at positions 16-15 Match "" at positions 17-16 Match "" at positions 18-17 Match "" at positions 19-18 Match "" at positions 20-19 Match "" at positions 21-20 Match "" at positions 22-21 Match "" at positions 23-22 Match "" at positions 24-23 Match "" at positions 25-24 Match "ss" at positions 26-27 Match "" at positions 28-27 Match "" at positions 29-28 Match "" at positions 30-29 Match "s" at positions 31-31 Match "" at positions 32-31 *///:~ Exercise 11 //: strings/E11_CheckForMatch2.java /************************** Exercise 11 ****************** *Apply the regular expression * (?i)((^[aeiou])|(\s+[aeiou]))\w+?[aeiou]\b * to * "Arline ate eight apples and one orange while Anita * hadn't any" *********************************************************/ package strings; import java.util.regex.*; public class E11_CheckForMatch2 { public static void main(String[] args) { Pattern p = Pattern.compile( "(?i)((^[aeiou])|(\\s+[aeiou]))\\w+?[aeiou]\\b"); Matcher m = p.matcher("Arline ate eight apples and " + "one orange while Anita hadn't any"); while(m.find()) { System.out.println("Match \"" + m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1)); } 266 Thinking in Java, 4th Edition Annotated Solution Guide } } /* Output: Match "Arline" at positions 0-5 Match " ate" at positions 6-9 Match " one" at positions 27-30 Match " orange" at positions 31-37 Match " Anita" at positions 44-49 *///:~ Exercise 12 //: strings/E12_Groups2.java /************************** Exercise 12 ****************** * Modify Groups.java to count all unique words * with no initial capital letter. *********************************************************/ package strings; import java.util.*; import java.util.regex.*; import static net.mindview.util.Print.*; public class E12_Groups2 { public static void main(String[] args) { Set words = new HashSet(); Matcher m = Pattern.compile("\\b((?![A-Z])\\w+)\\b") .matcher(Groups.POEM); while(m.find()) words.add(m.group(1)); print("Number of unique words = " + words.size()); print(words.toString()); } } /* Output: Number of unique words = 25 [catch, jaws, shun, raths, were, brillig, borogoves, mome, frumious, son, the, claws, in, my, wabe, gimble, and, bird, that, bite, slithy, mimsy, outgrabe, gyre, toves] *///:~ The regular expression represented by \b\w+\b allows you to perform a “whole words” search (in Java it becomes \\b\\w+\\b). The program tests for initial capitals with a negative lookahead. A lookaround (the common name for lookahead and lookbehind) is sometimes indispensable, and often makes the whole regular expression seem more intuitive. As an additional exercise, rewrite this expression with no lookaround. Strings 267 Exercise 13 //: strings/E13_StartEnd2.java /************************** Exercise 13 ****************** * Modify StartEnd.java so that it uses Groups.POEM as * input, but still produces positive outputs for find(), * lookingAt() and matches(). *********************************************************/ package strings; import java.util.regex.*; import static net.mindview.util.Print.*; public class E13_StartEnd2 { private static class Display { private boolean regexPrinted = false; private String regex; Display(String regex) { this.regex = regex; } void display(String message) { if(!regexPrinted) { print(regex); regexPrinted = true; } print(message); } } static void examine(String s, String regex) { Display d = new Display(regex); Pattern p = Pattern.compile(regex); Matcher m = p.matcher(s); while(m.find()) d.display("find() '" + m.group() + "' start = "+ m.start() + " end = " + m.end()); if(m.lookingAt()) // No reset() necessary d.display("lookingAt() start = " + m.start() + " end = " + m.end()); if(m.matches()) // No reset() necessary d.display("matches() start = " + m.start() + " end = " + m.end()); } public static void main(String[] args) { for(String in : Groups.POEM.split("\n")) { print("input : " + in); for(String regex : new String[] {"\\w*ere\\w*", "\\w*at", "t\\w+", "T.*?."}) examine(in, regex); } 268 Thinking in Java, 4th Edition Annotated Solution Guide } } /* Output: input : Twas brillig, and the slithy toves t\w+ find() 'the' start = 18 end = 21 find() 'thy' start = 25 end = 28 find() 'toves' start = 29 end = 34 T.*?. find() 'Tw' start = 0 end = 2 lookingAt() start = 0 end = 2 matches() start = 0 end = 34 input : Did gyre and gimble in the wabe. t\w+ find() 'the' start = 23 end = 26 input : All mimsy were the borogoves, \w*ere\w* find() 'were' start = 10 end = 14 t\w+ find() 'the' start = 15 end = 18 input : And the mome raths outgrabe. \w*at find() 'rat' start = 13 end = 16 t\w+ find() 'the' start = 4 end = 7 find() 'ths' start = 15 end = 18 find() 'tgrabe' start = 21 end = 27 input : input : Beware the Jabberwock, my son, t\w+ find() 'the' start = 7 end = 10 input : The jaws that bite, the claws that catch. \w*at find() 'that' start = 9 end = 13 find() 'that' start = 30 end = 34 find() 'cat' start = 35 end = 38 t\w+ find() 'that' start = 9 end = 13 find() 'te' start = 16 end = 18 find() 'the' start = 20 end = 23 find() 'that' start = 30 end = 34 find() 'tch' start = 37 end = 40 T.*?. find() 'Th' start = 0 end = 2 lookingAt() start = 0 end = 2 matches() start = 0 end = 41 input : Beware the Jubjub bird, and shun t\w+ Strings 269 find() 'the' start = 7 end = 10 input : The frumious Bandersnatch. \w*at find() 'Bandersnat' start = 13 end = 23 t\w+ find() 'tch' start = 22 end = 25 T.*?. find() 'Th' start = 0 end = 2 lookingAt() start = 0 end = 2 matches() start = 0 end = 26 *///:~ Exercise 14 //: strings/E14_SplitDemo2.java /************************** Exercise 14 ****************** * Rewrite SplitDemo using String.split(). *********************************************************/ package strings; import java.util.*; import static net.mindview.util.Print.*; public class E14_SplitDemo2 { public static void main(String[] args) { String input = "This!!unusual use!!of exclamation!!points"; print(Arrays.toString(input.split("!!"))); // Only do the first three: print(Arrays.toString(input.split("!!", 3))); } } /* Output: [This, unusual use, of exclamation, points] [This, unusual use, of exclamation!!points] *///:~ Exercise 15 //: strings/E15_JGrep2.java // {Args: E15_JGrep2.java "\b[Ssct]\w+" CASE_INSENSITIVE} /*********************** Exercise 15 ********************* * Modify JGrep.java to accept flags as arguments (e.g., * Pattern.CASE_INSENSITIVE, Pattern.MULTILINE). *********************************************************/ package strings; import java.util.regex.*; 270 Thinking in Java, 4th Edition Annotated Solution Guide import net.mindview.util.*; import static net.mindview.util.Print.*; public class E15_JGrep2 { public static void main(String[] args) throws Exception { if(args.length < 3) { print("Usage: java E15_JGrep2 file regex pattern"); print("pattern can take one of the following values"); print("CANON_EQ, CASE_INSENSITIVE, COMMENTS, " + "DOTALL, MULTILINE, UNICODE_CASE, UNIX_LINES"); System.exit(0); } int flag = 0; if(args[2].equalsIgnoreCase("CANON_EQ")) { flag = Pattern.CANON_EQ; } else if(args[2].equalsIgnoreCase("CASE_INSENSITIVE")) { flag = Pattern.CASE_INSENSITIVE; } else if(args[2].equalsIgnoreCase("COMMENTS")) { flag = Pattern.COMMENTS; } else if(args[2].equalsIgnoreCase("DOTALL")) { flag = Pattern.DOTALL; } else if(args[2].equalsIgnoreCase("MULTILINE")) { flag = Pattern.MULTILINE; } else if(args[2].equalsIgnoreCase("UNICODE_CASE")) { flag = Pattern.UNICODE_CASE; } else if(args[2].equalsIgnoreCase("UNIX_LINES")) { flag = Pattern.UNIX_LINES; } Pattern p = Pattern.compile(args[1], flag); // Iterate through the lines of the input file: int index = 0; Matcher m = p.matcher(""); for(String line : new TextFile(args[0])) { m.reset(line); while(m.find()) print(index++ + ": " + m.group() + ": " + m.start()); } } } /* Output: (Sample) 0: strings: 4 1: Ssct: 30 2: CASE_INSENSITIVE: 40 3: to: 21 4: CASE_INSENSITIVE: 11 5: static: 7 Strings 271 6: class: 7 7: static: 9 8: String: 26 9: throws: 41 10: can: 21 11: take: 25 12: the: 37 13: CANON_EQ: 13 14: CASE_INSENSITIVE: 23 15: COMMENTS: 41 16: System: 6 17: CANON_EQ: 33 18: CANON_EQ: 21 19: CASE_INSENSITIVE: 35 20: CASE_INSENSITIVE: 21 21: COMMENTS: 40 22: COMMENTS: 21 23: compile: 24 24: through: 15 25: the: 23 26: the: 36 27: String: 8 28: TextFile: 26 29: start: 12 *///:~ Exercise 16 //: strings/E16_JGrep3.java // {Args: E16_JGrep3.java java} /********************** Exercise 16 ********************* * Modify JGrep.java to accept a directory name or a file * name as argument (if a directory is provided, search * should include all files in the directory). * Hint: you can generate a list of filenames with * File[] files = new File(".").listFiles(); ********************************************************/ package strings; import java.io.*; import java.util.regex.*; import net.mindview.util.*; public class E16_JGrep3 { public static void main(String[] args) throws Exception { if(args.length < 2) { System.out.println( 272 Thinking in Java, 4th Edition Annotated Solution Guide "Usage: java E16_JGrep3 file regex"); System.exit(0); } File file = new File(args[0]); File[] files = null; if(file.isDirectory()) files = file.listFiles(); else files = new File[]{new File(args[0])}; Pattern p = Pattern.compile(args[1]); Matcher m = p.matcher(""); for(File f : files) { System.out.println("-- File:" + f.getName() + "--"); // Iterate through the lines of the input file: int index = 0; for(String line : new TextFile(f.getAbsolutePath())) { m.reset(line); while(m.find()) System.out.println(index++ + ": " + m.group() + ": " + m.start()); } } } } /* Output: (Sample) -- File:E16_JGrep3.java-0: java: 23 1: java: 21 2: java: 26 3: java: 16 4: java: 7 5: java: 7 6: java: 16 *///:~ Exercise 17 This exercise looks like the previous ones; you just follow the program template, applying the right regular expression to extract comments from a Java source file. However, it’s not that simple. We need state information to distinguish between a real comment and text that looks like a comment (for example, "// this is part of a string and not a comment"). The program needs more than a search facility based in regular expression to locate itself inside the source code: it needs a miniature Java source code parser. We can either write a parser or enhance an existing general parser (e.g., to display comments). We can reuse a stable base code to great advantage, as most of the work is already done. Strings 273 Compare the alternative methods in the next three solutions. (The second one amounts to a miniature tutorial for JavaCC.) Just provide the same input for both and check the differences in the outputs. Alternative A //: strings/E17_JCommentExtractor.java // {Args: E17_JCommentExtractor.java} /********************** Exercise 17 ********************* * Write a program that reads a Java source-code file (you * provide the file name on the command line) and displays * all the comments. ********************************************************/ package strings; import java.util.regex.*; import net.mindview.util.*; public class E17_JCommentExtractor { static final String CMNT_EXT_REGEX = "(?x)(?m)(?s) # Comments, Multiline & Dotall: ON\n" + "/\\* # Match START OF THE COMMENT\n" + "(.*?) # Match all chars\n" + "\\*/ # Match END OF THE COMMENT\n" + "| //(.*?)$ # OR Match C++ style comments\n"; public static void main(String[] args) throws Exception { if(args.length < 1) { System.out.println( "Usage: java E17_JCommentExtractor file"); System.exit(0); } String src = TextFile.read(args[0]); Pattern p = Pattern.compile(CMNT_EXT_REGEX); Matcher m = p.matcher(src); while(m.find()) System.out.println(m.group(1) != null ? m.group(1) : m.group(2)); } } /* Output: (Sample) : strings/E17_JCommentExtractor.java {Args: E17_JCommentExtractor.java} ********************* Exercise 17 ********************* * Write a program that reads a Java source-code file (you * provide the file name on the command line) and displays * all the comments. ******************************************************* (.*?)$ # OR Match C++ style comments\n"; 274 Thinking in Java, 4th Edition Annotated Solution Guide /:~ *///:~ We must remove the comment markers to compile the new source code (created by the automated test system). The (?x) flag enables the so-called COMMENTS mode, white space is ignored, and embedded comments starting with # are ignored until the end of a line. However, the program is flawed; it erroneously reports as a comment part of the regular expression pattern clearly situated inside a Java string (like (.*?)$ # OR Match C++ style comments\n";). Alternative B JavaCC is a parser generator for Java applications (free at https://javacc.dev.java.net/). A parser generator converts a grammar specification to a Java program that recognizes the grammar. We can cover only part of the topic of parser generators here, so we look at only those changes we must make to an existing J2SE5 grammar file (Java1.5.jj). Assume JavaCC is installed and that we can build the example in the examples\JavaGrammars\1.5 directory (see its README file for instructions). Java1.5.jj contains everything to produce a fully functional J2SE5 parser. At this level the parser signals only whether the input is a legal Java program, but as that solves 99% of exercises 17, 18 and 19, we present the solution for all three at once. The program, Java Code Sniffer, accepts two command line arguments, the input file and the option to indicate a processing choice (“C” for Display Comments, “S” for Display String Literals, and “N” for Display Class Names). An invalid invocation causes usage instructions to appear onscreen. We build the program with the CodeSniffer.jj grammar file and the Token.java file (from the examples\JavaGrammars\1.5 directory). Follow these steps to create CodeSniffer.jj: 1. Copy Java1.5.jj into a local folder and rename it CodeSniffer.jj; 2. Copy Token.java into the CodeSniffer.jj folder (this may not be necessary for later releases of JavaCC); 3. Open CodeSniffer.jj in a text editor; 4. Insert the following code fragment right after the line import java.io.*: Strings 275 import static net.mindview.util.Print.*; enum OpMode { DisplayComments, DisplayStrings, DisplayClassNames } 5. Replace the main( ) method with the following code: static OpMode mode; public static void main(String args[]) { JavaParser parser; if (args.length == 2) { print( "Java Code Sniffer Version 1.0: Reading from file " + args[0] + " . . ."); try { parser = new JavaParser( new java.io.FileInputStream(args[0])); } catch (java.io.FileNotFoundException e) { print("Java Code Sniffer Version 1.0: File " + args[0] + " not found."); return; } if (args[1].equalsIgnoreCase("c")) { mode = OpMode.DisplayComments; print("DISPLAYING COMMENTS...\n"); } else if (args[1].equalsIgnoreCase("s")) { mode = OpMode.DisplayStrings; print("DISPLAYING STRING LITERALS...\n"); } else if (args[1].equalsIgnoreCase("n")) { mode = OpMode.DisplayClassNames; print("DISPLAYING CLASS NAMES...\n"); } else { usage(); return; } } else { usage(); return; } try { parser.CompilationUnit(); print("\nJava Code Sniffer Version 1.0: Java program" + " parsed successfully."); } catch (ParseException e) { print(e.getMessage()); print("\nJava Code Sniffer Version 1.0: Encountered " + 276 Thinking in Java, 4th Edition Annotated Solution Guide "errors during parse."); } } private static void usage() { print("Java Code Sniffer Version 1.0: Usage is:"); print(" java JavaParser file