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