Preface xviiChapter 1 Java Fundamentals 27 2.1 The Parts of a Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272.2 The print and println Methods, and the Java API . . . . . . . . . . . . 332.3 Variables and Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392.4 Primitive Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442.5 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542.6 Combined Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 632.7 Conversion between Primitive Data Types . . . . . . . . . . . . . . . . . . . . . 642.8 Creating Named Constants with final . . . . . . . . . . . . . . . . . . . . . . 682.9 The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702.10 Scope. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752.11 Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772.12 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822.13 Reading Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842.14 Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922.15 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Review Questions and Exercises 100 Programming Challenges 105Chapter 2 Decision Structures 109 3.1 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1093.2 The if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1193.3 Nested if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1223.4 The if-else-if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1293.5 Logical Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1353.6 Comparing String Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1433.7 More about Variable Declaration and Scope . . . . . . . . . . . . . . . . . . 1493.8 The Conditional Operator (Optional) . . . . . . . . . . . . . . . . . . . . . . . . 1503.9 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1523.10 Creating Objects with the DecimalFormat Class. . . . . . . . . . . . . . 1593.11 The printf Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1643.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Review Questions and Exercises 169 Programming Challenges 174 Chapter 3 Loops and Files 179 4.1 The Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . 1794.2 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1834.3 Using the while Loop for Input Validation . . . . . . . . . . . . . . . . . . . 1904.4 The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1944.5 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1974.6 Running Totals and Sentinel Values . . . . . . . . . . . . . . . . . . . . . . . . . 2064.7 Nested Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2114.8 The break and continue Statements (Optional) . . . . . . . . . . . . . 2124.9 Deciding Which Loop to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2134.10 Introduction to File Input and Output . . . . . . . . . . . . . . . . . . . . . . . 2134.11 The Random Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2334.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Review Questions and Exercises 236 Programming Challenges 242Chapter 4 Methods 247 5.1 Introduction to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2475.2 Passing Arguments to a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 2575.3 More about Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2695.4 Returning a Value from a Method . . . . . . . . . . . . . . . . . . . . . . . . . . 2715.5 Problem Solving with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2805.6 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Review Questions and Exercises 285 Programming Challenges 289 Chapter 5 A First Look at Classes 297 6.1 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2976.2 Instance Fields and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3206.3 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3256.4 Overloading Methods and Constructors. . . . . . . . . . . . . . . . . . . . . . 3346.5 Scope of Instance Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3426.6 Packages and import Statements . . . . . . . . . . . . . . . . . . . . . . . . . . 3436.7 Focus on Object-Oriented Design: Finding the Classesand Their Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3456.8 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Review Questions and Exercises 352 Programming Challenges 357Chapter 6 A First Look at GUI Applications 363 7.1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3637.2 Creating Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3667.3 Equipping GUI Classes with a main Method . . . . . . . . . . . . . . . . . . 3947.4 Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3967.5 Radio Buttons and Check Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 4127.6 Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4237.7 Focus on Problem Solving: Extending Classes from JPanel . . . . . . 4267.8 Splash Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4387.9 Using Console Output to Debug a GUI Application . . . . . . . . . . . . . 4397.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444 Review Questions and Exercises 444 Programming Challenges 448 Chapter 7 Arrays and the ArrayList Class 451 8.1 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4518.2 Processing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4618.3 Passing Arrays as Arguments to Methods. . . . . . . . . . . . . . . . . . . . . 4708.4 Some Useful Array Algorithms and Operations. . . . . . . . . . . . . . . . . 4748.5 Returning Arrays from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4878.6 String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4898.7 Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4928.8 The Sequential Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 4958.9 Two-Dimensional Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4988.10 Arrays with Three or More Dimensions . . . . . . . . . . . . . . . . . . . . . . 5108.11 Command-Line Arguments and Variable-Length Argument Lists . . . 5118.12 The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5158.13 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523 Review Questions and Exercises 523 Programming Challenges 528Chapter 8 A Second Look at Classes and Objects 533 9.1 Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5339.2 Passing Objects as Arguments to Methods. . . . . . . . . . . . . . . . . . . . 5409.3 Returning Objects from Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . 5439.4 The toString Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5459.5 Writing an equals Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5499.6 Methods That Copy Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5529.7 Aggregation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5559.8 The this Reference Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5689.9 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5719.10 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5809.11 Focus on Object-Oriented Design: Class Collaboration . . . . . . . . . . 5829.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 Review Questions and Exercises 587 Programming Challenges 591 Chapter 9 Text Processing and More about Wrapper Classes 597 10.1 Introduction to Wrapper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 59710.2 Character Testing and Conversion with the Character Class. . . . . 59810.3 More String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60610.4 The StringBuilder Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62010.5 Tokenizing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62810.6 Wrapper Classes for the Numeric Data Types. . . . . . . . . . . . . . . . . . 63610.7 Focus on Problem Solving: The TestScoreReader Class. . . . . . . . 63910.8 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643 Review Questions and Exercises 643 Programming Challenges 647Chapter 10 Inheritance 653 11.1 What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65311.2 Calling the Superclass Constructor . . . . . . . . . . . . . . . . . . . . . . . . . 66611.3 Overriding Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67411.4 Protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68311.5 Chains of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68911.6 The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69511.7 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69711.8 Abstract Classes and Abstract Methods . . . . . . . . . . . . . . . . . . . . . . 70211.9 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70911.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721 Review Questions and Exercises 722 Programming Challenges 727 Chapter 11 Exceptions and Advanced File I/O 733 12.1 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73312.2 Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75512.3 Advanced Topics: Binary Files, Random Access Files,and Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76112.4 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778 Review Questions and Exercises 778 Programming Challenges 784Chapter 12 Advanced GUI Applications 787 13.1 The Swing and AWT Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . 78713.2 Read-Only Text Fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78813.3 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79013.4 Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80613.5 Displaying Images in Labels and Buttons . . . . . . . . . . . . . . . . . . . . . 81213.6 Mnemonics and Tool Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81813.7 File Choosers and Color Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . 82013.8 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82413.9 More about Text Components: Text Areas and Fonts. . . . . . . . . . . . 83313.10 Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83713.11 Look and Feel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84213.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844 Review Questions and Exercises 845 Programming Challenges 850 Chapter 13 Applets and More 855 14.1 Introduction to Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85514.2 A Brief Introduction to HTML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85714.3 Creating Applets with Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86614.4 Using AWT for Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87514.5 Drawing Shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88014.6 Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90114.7 Timer Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91114.8 Playing Audio. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91514.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920 Review Questions and Exercises 920 Programming Challenges 927Chapter 14 Recursion 929 15.1 Introduction to Recursion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92915.2 Solving Problems with Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 93215.3 Examples of Recursive Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93715.4 The Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94315.5 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947 Review Questions and Exercises 948 Programming Challenges 951 Chapter 15 Stacks and Queues 1219 21.1 Stacks and Their Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121921.2 Array Implementation of Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . 122221.3 Linked Implementation of Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . 122821.4 Queues and Their Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . 123321.5 Array Implementation of Queues . . . . . . . . . . . . . . . . . . . . . . . . . . 123321.6 Linked List Implementation of Queues . . . . . . . . . . . . . . . . . . . . . . 124421.7 Generic Implementation of Stacks and Queues . . . . . . . . . . . . . . . 124921.8 Queues and Breadth-First Search . . . . . . . . . . . . . . . . . . . . . . . . . . 125221.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1255 Review Questions and Exercises 1256 Programming Challenges 1258Chapter 16 Binary Trees, AVL Trees, and Priority Queues 1261 22.1 Binary Trees and Their Applications . . . . . . . . . . . . . . . . . . . . . . . . 126122.2 Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127122.3 AVL Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128822.4 Priority Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130122.5 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1318 Review Questions and Exercises 1318 Programming Challenges 1321 Index 1325
Les mer