Annotated Solution Guide for Thinking in Java Fourth Edition (Bruce Eckel, Ervin Varga) (Z-Library)
技术Author:Bruce Eckel, Ervin Varga
No description
Tags
Support Statistics
¥.00 ·
0times
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
Annotated Solution Guide for Thinking in Java Fourth Edition TIJ4 published February, 2006 Solution Guide published September 2007 Bruce Eckel President, MindView, Inc. Ervin Varga Ph.D. Computer Science, University of Novi Sad, Serbia; Faculty of Technical Sciences ©2007, MindView, Inc. All Rights Reserved
Page
2
(This page has no text content)
Page
3
i 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.
Page
4
(This page has no text content)
Page
5
iii 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 Info- Zip (pre-installed on many Linux distributions, or available at http://www.info- zip.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/.
Page
6
Thinking in Java, 4th Edition Annotated Solution Guide iv 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.
Page
7
v Contents Copyright & Disclamer i About this Document 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
Page
8
Thinking in Java, 4th Edition Annotated Solution Guide vi 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
Page
9
Contents vii 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
Page
10
Thinking in Java, 4th Edition Annotated Solution Guide viii 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
Page
11
Contents ix 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
Page
12
Thinking in Java, 4th Edition Annotated Solution Guide x 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
Page
13
Contents xi 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
Page
14
Thinking in Java, 4th Edition Annotated Solution Guide xii 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
Page
15
Contents xiii 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
Page
16
Thinking in Java, 4th Edition Annotated Solution Guide xiv 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
Page
17
Contents xv 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
Page
18
Thinking in Java, 4th Edition Annotated Solution Guide xvi 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
Page
19
Contents xvii 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
Page
20
Thinking in Java, 4th Edition Annotated Solution Guide xviii 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
The above is a preview of the first 20 pages. Register to read the complete e-book.
Comments 0
Loading comments...
Reply to Comment
Edit Comment