CHAPTER 1 Introduction to Computers and Java 3 Objectives 4 Prerequisites 4 1.1 Computer Basics 4 Hardware and Memory 5 Programs 7 Programming Languages and Compilers 8 Java Byte-Code 9 Linking 12 1.2 Designing Programs 12 Object-Oriented Programming 13 Encapsulation 14 Polymorphism 15 Inheritance 16 If You Know Some Other Programming Language 17 Ngorithms 17 Reusable Components 18 Testing and Debugging 18 6otcha: Coping with Gotchas 19 6otcha: Hidden Errors 20 1.3 A Sip of Java 20 History of the Java Language 20 Applets 21 A First Java Application Program 22 Compiling a Java Program or Class 28 Running a Java Program 29 Preview Examples of Applets Optional 30 Chapter Summary 33 Answers to Self-Test Questions 34 Programming Projects 36 CHAPTER 2 Primitive Types, Strings, and Console I/O 39 Objectives 40 Prerequisites 40 2.1 Primitive Types and Expressions 40 Variables 41 Java Identifiers 44 6otcha: Java Is Case-Sensitive 46 Primitive Types 46 Assignment Statements 47 Specialized Assignment Operators 49 Simple Input and Output 49 Number Constants 51 Assignment Compatibilities 52 Type Casting 53 Java Tip: Type Casting a Character to an Integer 55 Programming Tip: Initialize Variables 56 Gotcha: Imprecision in Floating-Point Numbers 57 Arithmetic Operators 58 Parentheses and Precedence Rules 60 Case Study: Vending Machine Change 62 Increment and Decrement Operators 66 More about the Increment and Decrement Operators 67 2.2 The Class String 68 String Constants and Variables 68 Concatenation of Strings 69 Classes 70 String Methods 71 String Processing 75 Escape Characters 75 The Unicode Character Set 77 2.3 Keyboard and Screen I/O 79 Screen Output 79 Input Using SavitchIn 82 More Input Methods 85 Gotcha: readInt and readDoub3 e 86 Programming Tip: Echo Input 87 2.4 Documentation and Style 88 Programming Tip: Use Meaningful Names for Variables 88 Documentation and Comments 89 Indenting 90 Named Constants 92 Chapter Summary 95 Answers to Self-Test Questions 96 Programming Projects 99 CHAPTER 3 Flow of Control 103 Objectives 104 Prerequisites 104 3.1 Branching Statements 104 The if-else Statement 104 Introduction to Boolean Expressions 108 Gotcha: Using==with Strings 111 Programming Tip: Alphabetical Order 113 Nested Statements and Compound Statements 115 Java Tip: Matching else and if 117 Multibranch if-el se Statements 118 Programming Example: Assigning Letter Grades 119 The switch Statement 122 Gotcha: Omitting a break Statement 126 The Conditional Operator Optional 126 3.2 Java Loop Statements 128 while Statements 129 Java Tip: A while Loop Can Perform Zero Iterations 133 The do-while Statement 133 Programming Example: Bug Infestation 137 Gotcha: Infinite Loops 139 The for Statement 141 the Camma in for Statements Optional 145 Gotcha: Extra Semicolon in a Loop Statement 146 Java Tip: Choosing a Loop Statement 148 The break Statement in Loops 148 Gotcha: Misuse of break Statements 150 The exit Method 150 3.3 Programming with Loops 152 The Loop Body 152 Initializing Statements 154 Ending a Loop 155 Programming Example: Nested Loops 156 Programming Tip: Avoid Declaring Variables in a Loop Body 157 Loop Bugs 157 Tracing Variables 161 3.4 The Type boolean 162 Boolean Expressions and Boolean Variables 163 Programming Tip: Naming Boolean Variables 164 Precedence Rules 165 Input and Output of Boolean Values 168 Case Study: Using a Boolean Variable to End a Loop 168 Chapter Summary 172 Answers to Self-Test Questions 172 Programming Projects 177 CHAPTER 4 Defining Classes and Methods 181 Objectives 182 Prerequisites 182 4.1 Class and Method Definitions 182 Class Files and Separate Compilation 184 Instance Variables 185 Using Methods 168 void Method Definitions 191 Methods That Return a Value 193 Java Tip: Use of return in void Methods 196 The this Parameter 198 Local Variables 200 Blocks 202 Gotcha:Variables Declared in a Block 202 Java Tip: Declaring Variables in a for Statement 203 Parameters of a Primitive Type 203 Gotcha: Use of the Terms Parameter and Argument 209 Summary of Class and Method Definition Syntax 209 4.2 Information Hiding and Encapsulation 210 Information Hiding 210 Programming Tip: Parameter Names Are Local to the Method 211 Precondition and Postcondition Comments 211 Java Tip: Assertion Checks 212 The publ i c and private Modifiers 214 Programming Tip: Instance Variables Should Be private 217 Programming Example: A Purchase Class 221 Encapsulation 225 Automatic Documentation with javadoc 228 UML Class Diagrams 228 4.3 Objects and Reference 230 Variables of a Class Type and Objects 230 Gotcha: Use of = and == with Variables of a Class Type 235 Java Tip: Define an equal s Method for Your Classes 235 Programming Example: A Species Class 239 Boolean-Valued Methods 242 Class Parameters 244 Comparing Class Parameters and Primitive-Type Parameters 247 Chapter Summary 251 Answers to Self-Test Questions 251 Programming Projects 257 CHAPTER 5 More About Objects and Methods 261 Objectives 262 Prerequisites 262 5.1 Programming with Methods 263 Methods Calling Methods 263 Programming Tip: Make Helping Methods Private 268 Java Tip: Make the compiler Happy 268 Gotcha: Null Pointer Exception 269 5.2 Static Methods and Static Variables 271 Static Methods 271 Gotcha: Invoking a Nonstatic Method Within a Static Method 273 Java Tip: You Can Put a main in Any Class 275 Static Variables Optional 277 The Math Class 279 Integer, Double, and Other Wrapper Classes 282 5.3 Designing Methods 286 Case Study: Formatting Output 287 Top-Down Design 291 Testing Methods 293 5.4 Overloading 295 Overloading Basics 295 Programming Example: A Pet Class 298 Gotcha: Overloading and Automatic Type Conversion 298 Gotcha: You Cannot Overload on the Basis of the Returned Type 303 Programming Example: A Class for Money 305 5.5 Constructors 312 Defining Constructors 312 Programming Tip: You Can Use Other Methods in a Constructor 320 Gotcha: Omitting the Default Constructor 321 Gotcha: Many Wrapper Classes Have No Default Constructor 321 5.6 Information Hiding Revisited 322 Gotcha: Privacy Leaks 322 5.7 Packages 326 Packages and importing 326 Package Names and Directodes 326 Gotcha: Not Including the Current Directory in Your Class Path 329 Name Clashes 330 Chapter Summary 331 Answers to Self-Test Questions 332 Programming Projects 337 CHAPTER 6 Arrays 341 Objectives 342 Prerequisites 342 6.1 Array Basics 343 Creating and Accessing Arrays 343 Array Details 344 Programming Tip: Use Singular Array Names 348 The length Instance Variable 349 Java Tip: Array Indices Start with Zero 349 Programming Tip: Use a for Loop to Step Through an Array 351 Gotcha: Array Index Out of Bounds 351 Initializing Arrays 352 6.2 Arrays in Classes and Methods 354 Case Study: Sales Report 354 Indexed Vadablas as Method Arguments 361 Entire Arrays as Method Arguments 364 Arguments for the Method mai n 364 Gotcha: Use of = and == with Arrays 366 Methods That Return Arrays 369 6.3 Progrannning with Arrays and Classes 372 Programming Example: A Specialized List Class 373 Partially Filled Arrays 380 Searching an Array 380 Gotcha: Returning an Array Instance Variable 381 6.4 Sorting Arrays 383 Selection Sort 383 Other Sorting Algorithms 388 6.5 Multidimensional Arrays 388 Multidimensional-Array Basics 390 Multidimensional-Array Parameters and Returned Values 392 Implementation of Multidimensional Arrays 394 Bagged Arrays OpUonal 397 Programming Example: Employee Time Records 398 Chapter Summary 404 Answers to Self-Test Questions 405 Programming Projects 410 CHAPTER 7 Inheritance 415 Objectives 416 Prerequisites 416 7.1 Inheritance Basics 416 Programming Example: A Person Class 417 Derived Classes 418 Overriding Method Definitions 421 Overriding Versus Overloading 422 The final Modifier 422 Gotcha: Use of Private Instance Variables from the Base Class 423 Programming Tip: Assume That Your Coworkers Are Malicious 424 Gotcha: Private Methods Are Not Inherited 424 UML Inheritance Diagrams 424 7.2 Programming with Inheritance 427 Constructors in Derived Classes 427 The this Method OpUonal 428 Call to an Overridden Method 429 Programming Example: Multilevel Derived Classes 430 A Subtle Point About Overloading and Overriding Optional 435 Java Tip: You Cannot Use Multiple supers 435 Programming Tip: An Object Can Have More than One Type 436 Programming Tip: Is a and Has a Relations 439 The Class Object 440 Case Study: Character Graphics 442 Abstract Classes 452 Interlaces Optional 456 7.3 Dynamic Binding and Polymorphism 458 Dynamic Binding 458 Type Checking and Dynamic Binding 460 Dynamic Binding with toString 451 Polymorphism 462 Java Tip: A Better equal s Method Optional 463 Chapter Summary 465 Answers to Self-Test Questions 466 Programming Projects 471 CHAPTER 8 Exception Handling 475 Objectives 476 Prerequisites 476 8.1 Basic Exception Handling 477 Exceptions in Java 477 Predefined Exception Classes 489 ArrayIndexOutOfBoundsException Alternative Ordering 490 8.2 Defining Exception Classes 491 Defining Your Own Exception Classes 491 Java Tip: Preserve getMessage When You Define Exception Classes 496 Programming Tip: When to Define an Exception Class 497 8.3 Using Exception Classes 502 Declaring Exceptions Passing the Buck 502 Exceptions That De Not Need To Be Caught 506 The Assertion Error Class Optional 507 Multiple Throws and Catches 507 Java Tip: Catch the More Specific Exception First 510 Programming Tip: Exception Handling and Information Hiding 511 Gotcha: Overuse of Exceptions 513 Programming Tip: When to Throw an Exception 513 Gotcha: Nested try-catch Blocks 515 The finally Block Optional 515 Rethrowing an Exception Optional 516 Case Study'' A Line-Oriented Calculator 517 Chapter Summary 531 Answers to Self-Test Questions 532 Programming Projects 536 CHAPTER 9 Streams and File I/O 541 Objectives 542 Prerequisites 542 9.1 An Overview of Streams and File I/O 543 The Concept of a Stream 543 Why Use Files for I/O 543 Differences between Text Files and Binary Files 543 9.2 Text-File I/O 545 Text-File Output with PrintWriter 545 Gotcha: A try Block Is a Block 550 Gotcha: Overwriting a File 551 Java Tip: Appending To a Text File 551 Java Tip: Use toString for Text-File Output 553 Text-File Input with BufferedReader 556 Programming Example: Reading a File Name from the Keyboard 560 Java Tip: Using Path Names 562 The StringTokeni zer Class 563 Java Tip: Testing for the End of a Text File 565 The Classes FileReader and FileOutputStream 567 Unwrapping the Class Savi tchIn 569 9.3 The File Class 570 Using the File Class 570 9.4 Basic Binary-File I/O 573 Output to Binary Files, Using ObjectOutputStream 574 Some Details about writeUTF Optional 579 Reading Input from a Binary File, Using ObjectInputStream 580 Gotcha: Using ObjectInputStream with a Text File 584 Gotcha: Defining a Method to Open a Stream 585 The EOFExcepti on Class 586 Gotcha: Forgetting to Check for the End of a File 588 Gotcha: Checking for the End of a File in the Wrong Way 589 The Classes Fi I eInputSt ream and File0utputStream 589 Programming Example: Processing a File of Binary Data 590 9.5 Object I/O with Object Streams 594 Binary I/0 of Class Objects 594 Gotcha: Exceptions, Exceptions, Exceptions 599 The Serializable Interface 599 Gotcha: Mixing Class Types in the Same File 599 Array Objects in Binary Files 600 Chapter Summary 602 Answers to Self-Test Questions 603 Programming Projects 608 CHAPTER 10 Dynamic Data Structures 611 Objectives 612 Prerequisites 612 10.1 Vectors 613 Using Vectors 613 Programming Tip: Adding to a Vector 619 Gotcha: Vector Elements Are of Type Object 620 Comparing Vectors and Arrays 622 Gotcha: Using capacity Instead of size 622 Java Tip: Use trimToSize to Save Memory 624 Gotcha: Using the Method clone 625 Java Tip: Newer Collection Classes Optional 626 10.2 Linked Data Structures 627 Linked Lists 627 Gotcha: Null Pointer Exception 636 Gotcha: Privacy Leaks 637 Inner Classes 638 Node Inner Classes 639 Iterators 639 Programming Tip: Internal and External Iterators 652 Exception Handling with Linked Lists 652 Variations on a Linked List 656 Other Linked Data Structures 658 Chapter Summary 658 Answers to Self-Test Questions 659 Programming Projects 665 CHAPTER 11 Recursion 669 Objectives 670 Prerequisites 670 11.1 The Basics of Recursion 670 Case Study: Digits to Words 671 How Recursion Works 675 Gotcha: Infinite Recursion 679 Recursive versus Iterative DefinItions 682 Recursive Methods That Return a Value 682 11.2 Programming with Recursion 687 Programming Tip: Ask Until the User Gets It Right 687 Case Study: Binary Search 689 Programming Tip: Generalize the Problem 693 Programming Example: Merge Sort--A Recursive Sorting Method 697 Chapter Summary 702 Answers to Self-Test Questions 702 Programming Projects 705 CHAPTER 12 Window Interfaces Using Swing 709 Objectives 710 Prerequisites 710 12.1 Background 711 GUIs---Graphical User Interfaces 711 Event-Driven Programming 711 12.2 Basic Swing Details 713 Gotcha: Save All Your Work before Running a Swing Program 714 Programming Example: A Simple Window 714 Java Tip: Ending a Swing Program 719 Gotcha: Forgetting to Program the Close-Window Button 720 Gotcha: Forgetting to Use getContentPane 720 More about Window Listeners 720 Size Units for Screen Objects 722 More on setVisible 723 Programming Example: A Better Version of Our First Swing Program 725 Programming Example: A Window with Color 728 Some Methods of the Class JFrame 732 Layout Managers 735 12.3 Buttons and Action Listeners 742 Programming Example: Adding Buttons 742 Buttons 744 Action Listeners and Action Events 746 Gotcha: Changing the Parameter List for actionPerformed 750 Interfaces 751 Java Tip: Code a GUI''s Look and Actions Separately 752 The Model-View--Controller Pattern 753 Java Tip: Use the Method setActionCommand 753 12.4 Container Classes 756 The JPanel Class 756 The Eontainer Class 759 Java Tip: Guide for Creating Simple Window Interfaces 762 12.5 Text I/O for GUIs 764 Text Areas and Text Fields 764 Programming Example: Labeling a Text Field 770 Inputting and Outputting Numbers 772 Programming Example: A GUI Adding Machine 775 Catching a NumberFormatExcepti on 779 Chapter Summary 782 Answers to Self-Test Questions 783 Programming Projects 791 CHAPTER 13 Applets and HTML 795 Objectives 796 Prerequisites 796 13.1 Applets 797 Applet Basics 797 Running an Applet 800 Programming Example: An Adder Applet 800 Java Tip: Converting a Swing Application to an Applet 803 Adding Icons to an Applet 803 13.2 Introduction to HTML 806 HTML Basics 806 Programming Tip: A Simple HTML-Document Outline 808 Inserting Hyperlinks 808 Gotcha: Not Using Your Reload Refresh Button 812 Chapter Summary 886 Answers to Self-Test Questions 886 Programming Projects 890 Appendix 1 Keywords 892 Appendix 2 Precedence Rules 893 Appendix 3 Unicode Character Set 894 Appendix 4 SavitchIn 895 Appendix 5 Protected and Package Modifiers 908 Appendix 6 The Decimal Format Class 909 Percent Notation 909 Scientific Notation E-Notation 911 Appendix 7 The Iterator Interface 912 Appendix 8 Cloning 913 Appendix 9 Javadoc 916 Commenting Classes for Use with javadoc 916 Running javadoc 917 Appendix 10 The JOptionPane Class 918 A Sample Program Using JOptionPane 918 Multiline Output Windows 923 Appendix 11 Differences between C and Java 924 Primitive Types 924 Strings 924 Flow of Control 924 Testing for Equality 925 main Method Function and Other Methods 925 Files and Including Files 925 Class and Method Function Definitions 925 No Pointers in Java 925 Method Function Parameters 926 Arrays 926 Garbage Collection 926 Other Comparisons 926 Index 927