Java 6 'th edition Concepts INTERNATIONAL STUDENT VERSION
CONTENTS PREFACE vii SPECIAL FEATURES xxviii chapter i INTRODUCTION 1 1.1 What Is Programming? 2 J.2 The Anatomy of a Computer 3 1.3 Translating Human-Readable Programs to Machine Code 7 1.4 The Java Programming Language 8 1.5 The Structure of a Simple Program 10 1.6 Compiling and Running a Java Program 14 1.7 Errors 17 1.8 Algorithms 19 chapter 2 AN INTRODUCTION TO OBJECTS AND CLASSES 2.1 Objects, Classes, and Methods 32 2.2 Declaring Variables 34 2.3 Constructing Objects 38 2.4 The API Documentation 40 2.5T Implementing a Test Program 43 2.6 Instance Variables 45 2.7 Specifying the Public Interface of a Class 48 2.8 Providing the Class Implementation 52 2.9T Unit Testing 58 2.10 Local Variables 60 2.11 Object References 62 2.12 Implicit Parameters 65 2.1 3G Graphical Applications and Frame Windows 67 2.14G Drawing on a Component 68 2.1 5G Ellipses, Lines, Text, and Color 72 2.16G Shape Classes 76
xxii Contents chapter 3 FUNDAMENTAL DATATYPES 99 3.1 Number Types 100 3.2 Constants 102 3.3 Arithmetic Operations and Mathematical Functions 107 3.4 Calling Static Methods 115 3.5 Strings 118 3.6 Reading Input 122 CHAPTER 4 DECISIONS 135 4.1 The if Statement 136 4.2 Comparing Values 140 4.3 Multiple Alternatives 149 4.4 Using Boolean Expressions 157 4.5T Code Coverage 162 chapters ITERATION 177 5.1 while Loops 178 5.2 for Loops 185 5.3 Common Loop Algorithms 194 5.4 Nested Loops 202 5.5 Application: Random Numbers and Simulations 205 5.6T Using a Debugger 207 chapter6 ARRAYS AND ARRAY LISTS 225 6.1 Arrays 226 6.2 Array Lists 232 6.3 Wrappers and Auto-boxing 238 6.4 The Enhanced for Loop 239 6.5 Partially Filled Arrays 241 6.6 Common Array Algorithms 243 6.7T Regression Testing 255 6.8 Two-Dimensional Arrays 258
Contents xxiii chapter 7 DESIGNING CLASSES 273 7.1 Discovering Classes 274 7.2 Cohesion and Coupling 275 7.3 Immutable Classes 278 7.4 Side Effects 279 7.5 Preconditions and Postconditions 283 7.6 Static Methods 286 7.7 Static Variables 288 7.8 Scope 291 7.9 Packages 294 7.10T Unit Test Frameworks 300 chapter s INTERFACES AND POLYMORPHISM 31 3 8.1 Using Interfaces for Algorithm Reuse 314 8.2 Converting Between Class and Interface Types 320 8.3 Polymorphism 322 8.4 Using Interfaces for Callbacks 323 8.5 Inner Classes 327 8.6T Mock Objects 329 8.7G Events, Event Sources, and Event Listeners 331 8.8G Using Inner Classes for Listeners 334 8.9G Building Applications with Buttons 336 8.10G Processing Timer Events 8.11 C Mouse Events chapter 9 INHERITANCE 351 9.1 Inheritance Hierarchies 352 9.2 Implementing Subclasses 355 9.3 Overriding Methods 359 9.4 Subclass Construction 362 9.5 Converting Between Subclass and Superclass Types 365 9.6 Polymorphism and Inheritance 367 9.7 Object: The Cosmic Superclass 375 9.8G Using Inheritance to Customize Frames 381
xxiv Contents chapter 10 INPUT/OUTPUT AND EXCEPTION HANDLING 391 10.1 Reading and Writing Text Files 392 10.2 Reading Text Input 395 10.3 Throwing Exceptions 403 10.4 Checked and Unchecked Exceptions 405 10.5 Catching Exceptions 407 10.6 The finally Clause 410 10.7 Designing Your Own Exception Types 412 10.8 Case Study. A Complete Example 413 chapter n OBJECT-ORIENTED DESIGN 425 11.1 The Software Life Cycle 426 11.2 Discovering Classes 430 11.3 Relationships Between Classes 433 11.4 Case Study: Printing an Invoice 436 11.5 Case Study: An Automatic Teller Machine 447 chapter 12 RECURSION 475 12.1 Triangle Numbers 476 12.2 Recursive Helper Methods 484 12.3 The Efficiency of Recursion 486 12.4 Permutations 491 12.5 Mutual Recursions 494 chapter 13 SORTING AND SEARCHING 509 13.1 Selection Sort 510 13.2 Profiling the Selection Sort Algorithm 513 13.3 Analyzing the Performance of the Selection Sort Algorithm 516 13.4 Merge Sort 518 13.5 Analyzing the Merge Sort Algorithm 521 13.6 Searching 524 13.7 Binary Search 526 13.8 Sorting Real Data 529
Contents xxv chapters AN INTRODUCTION TO DATA STRUCTURES 539 14.1 Using Linked Lists 540 14.2 Implementing Linked Lists 545 14.3 Abstract Data Types 556 14.4 Stacks and Queues 559 JffliSEIfJ 15.1 Sets 15.2 Maps 15.3 Hash Tables 15.4 Computing Hash Codes 15.5 Binary Search Trees 15.6 Binary Tree Traversal 15.7 Priority Queues ^QMSfiLtAB^M;. 15.8 Heaps 15.9 The Heapsort Algorithm pterla GENERIC PROGRAMMING 16.1 Generic Classes and Type Parameters 16.2 Implementing Generic Types 16.3 Generic Methods 16.4 Constraining Type Parameters 1 6.5 Type Erasure O aj^ta^e^^ 17.1G Processing Text Input 17.2G Text Areas 17.3G Layout Management 17.4G Choices 17.5G Menus 17.6G Exploring the Swing Documentation Available online in WileyPLUS and at www.wiley.com/go/global/horstmann.
xxvi Contents APPENDICES APPENDIX A THE BASIC LATIN AND LATIN-1 SUBSETS OF UNICODE 573 APPENDIX B JAVA OPERATOR SUMMARY 577 APPENDIX C JAVA RESERVED WORD SUMMARY 579 APPENDIX D THE JAVA LIBRARY 581 APPENDIX E JAVA SYNTAX SUMMARY APPENDIX F HTML SUMMARY APPENDIX C TOOL SUMMARY APPENDIX H JAVADOC SUMMARY APPENDIX I NUMBER SYSTEMS APPENDIXJ BIT AND SHIFT OPERATIONS APPENDIX K UML SUMMARY APPENDIX L JAVA LANGUAGE CODING GUIDELINES GLOSSARY 613 INDEX 627 ILLUSTRATION CREDITS 649