CHAPTER 1 Introduction to Computers and Java 3 Objectives 4 Prerequisites 4 1.1 Computer Basics 5 Hardware and Memory 5 Programs 7 Programming Languages and Compilers 9 Java Byte-Code 10 Class Loader 12 1.2 Designing Programs 13 Object-Oriented Programming 13 Encapsulation 14 Polymorphism 15 Inheritance 16 If You Know Some Other Programming Language 17 Algorithms 17 Reusable Components 18 Testing and Debugging 19 1.3 A Sip of Java 21 History of the Java Language 21 Applets 22 A First Java Application Program 22 Compiling a Java Program or Class 27 Running a Java Program 29 1.4 Graphics Supplement (Optional) 29 Objects and Methods 30 A Sample Graphics Applet 30 Size and Positions of Figures 33 Drawing Arcs 36 Running an Applet 36 Chapter Summary 39 Answers to Self-Test Questions 40 Programming Projects 42 CHAPTER 2 Primitive Types, Strings, and Console I/O 45 Objectives 46 Prerequisites 46 2.1 Primitive Types and Expressions 47 Variables 47 Java Identifiers 51 Primitive Types 52 Assignment Statements 53 Specialized Assignment Operators 55 Simple Screen Output 56 Simple Input 56 Number Constants 58 Assignment Compatibilities 59 Type Casting 61 Programming Tip: Initialize Variables 63 Arithmetic Operators 66 Parentheses and Precedence Rules 67 Case Study: Vending Machine Change 69 Increment and Decrement Operators 74 More about the Increment and Decrement Operators 75 2.2 The Class String 76 String Constants and Variables 76 Concatenation of Strings 77 Classes 78 String Methods 79 String Processing 83 Escape Characters 84 The Unicode Character Set 85 2.3 Keyboard and Screen I/O 87 Screen Output 87 Keyboard Input 90 2.4 Documentation and Style 98 Programming Tip: Use Meaningful Names for Variables 99 Documentation and Comments 99 Indenting 102 Named Constants 102 2.5 Graphics Supplement (Optional) 106 Programming Example: Style Rules Applied to a Graphics Applet 106 JOpti onPane 106 Inputting Other Numeric Types 115 Programming Example: Change-Making Program with Windowing I/O 117 Chapter Summary 120 Answers to Self-Test Questions 120 Programming Projects 125 CHAPTER 3 F1ow of Contro1 129 Objectives 130 Prerequisites 130 3.1 Branching Statements 130 The if-eIse Statement 130 Introduction to Boolean Expressions 134 Programming Tip: Alphabetical Order 139 Nested Statements and Compound Statements 142 Multibranch if-e1se Statements 145 Programming Example: Assigning Letter Grades 146 The switch Statement 149 The Conditional Operator (Optional) 153 3.2 Java Loop Statements 155 whi1e Statements 156 The do-whi1e Statement 160 Programming Example: Bug Infestation 164 The for Statement 168 The Comma in for Statements (Optional) 172 The break Statement in Loops 175 The exit Method 177 3.3 Programming with Loops 179 The Loop Body 179 Initializing Statements 181 Ending a Loop 182 Programming Example: Nested Loops 183 Programming Tip: Avoid Declaring Variables in a Loop Body 184 Loop Bugs 184 Tracing Variables 188 3.4 The Type boo] ean 189 Boolean Expressions and Boolean Variables 190 Programming Tip: Naming Boolean Variables 191 Precedence Rules 192 Input and Output of Boolean Values 195 Case Study: Using a Boolean Variable to End a Loop 195 3.5 Graphics Supplement (Optional) 199 Specifying a Drawing Color 199 Programming Example: A Multiface Applet 203 The drawString Method 208 A JOptionPane Yes/No Window 209 Chapter Summary 211 Answers to Self-Test Questions 211 Programming Projects 217 CHAPTER 4 Defining Classes and Methods 221 Objectives 222 Prerequisites 222 4.1 Class and Method Definitions 222 Class Files and Separate Compilation 224 Instance Variables 225 Using Methods 228 void Method Definitions 231 Methods That Return a Value 233 The this Parameter 238 Local Variables 240 Blocks 242 Parameters of a Primitive Type 243 Summary of Class and Method Definition Syntax 249 4.2 Information Hiding and Encapsulation 250 Information Hiding 250 Programming Tip: Parameter Names Are Local to the Method 251 Precondition and Postcondition Comments 251 The public and private Modifiers 254 Programming Tip: Instance Variables Should Be prirate 257 Programming Example: A Purchase Class 261 Encapsulation 265 Automatic Documentation with javadoc 268 UML Class Diagrams 268 4.3 Objects and Reference 270 Variables of a Class Type and Objects 270 Programming Example: A Species Class 279 Boolean-Valued Methods 282 Class Parameters 284 Comparing Class Parameters and Primitive-Type Parameters 287 4.4 Graphics Supplement (Optional) 291 The G raphi cs Class 291 Programming Example: Multiple Faces Redone with a Helping Method 294 The init Method 298 Adding Labels to an Applet 298 Chapter Summary 302 Answers to Self-Test Questions 303 Programming Projects 310 CHAPTER 5 More About Objects and Methods 315 Objectives 316 Prerequisites 316 5.1 Programming with Methods 317 Methods Calling Methods 317 Programming Tip: Make Helping Methods Private 323 5.2 Static Methods and Static Variables 326 Static Methods 326 Static Variables (Optional) 332 The Math Class 334 Integer, Double, and Other Wrapper Classes 337 5.3 Designing Methods 343 Case Study: Formatting Output 343 Top-Down Design 348 Testing Methods 350 5.4 Overloading 352 Overloading Basics 352 Programming Example: A Pet Class 355 Programming Example: A Class for Money 363 5.5 Constructors 369 Defining Constructors 370 Programming Tip: You Can Use Other Methods in a Constructor 378 5.6 Information Hiding Revisited 380 5.7 Packages 384 Packages and Importing 384 Package Names and Directories 385 Name Clashes 388 5.8 Graphics Supplement (Optional) 389 Programming Tip: Code Appearance and Actions Separately 389 Adding Buttons 390 Event-Driven Programming 391 Programming Example: A Complete Button Applet 391 Programming Buttons 392 Icons 399 Changing Visibility 401 Programming Example: An Example of Changing Visibility 4Ol What's Next 405 Chapter Summary 405 Answers to Self-Test Questions 406 Programming projects 412 CHAPTER 6 Arrays 417 Objectives 418 Prerequisites 418 6.1 Array Basics 419 Creating and Accessing Arrays 419 Array Details 420 Programming Tip: Use Singular Array Names 424 The 1ength Instance Variable 425 Programming Tip: Use a for Loop to Step Through an Array 427 Initializing Arrays 429 6.2 Arrays in Classes and Methods 430 Case Study: Sales Report 430 Indexed Variables as Method Arguments 438 Entire Arrays as Method Arguments 440 Arguments for the Method mai n 441 Methods That Return Arrays 446 6.3 Programming with Arrays and Classes 449 Programming Example: A Specialized List Class 450 Partially Filled Arrays 457 Searching an Array 457 6.4 Sorting Arrays 460 Selection Sort 460 Other Sorting Algorithms 465 6.5 Multidimensional Arrays 465 Multidimensional-Array Basics 467 Multidimensional-Array Parameters and Returned Values 470 Implementation of Multidimensional Arrays 471 Ragged Arrays (Optional) 475 Programming Example: Employee Time Records 476 6.6 Graphics Supplement (Optional) 482 Graphics Supplement Part 1: Text Areas and Text Fields 483 Programming Example: A Question-and-Answer Applet 483 JTextArea and JTextFi el d Objects 483 Graphics Supplement Part 2: Drawing Polygons 487 Drawing Polygons 487 Chapter Summary 491 Answers to Self-Test Questions 492 Programming Projects 497 CHAPTER 7 Inheritance 501 Objectives 502 Prerequisites 502 7.1 Inheritance Basics 502 Programming Example: A Person Class 503 Derived Classes 504 Overriding Method Definitions 507 Overriding Versus Overloading 508 The final Modifier 508 Programming Tip: Assume That Your Coworkers Are Malicious 510 UML Inheritance Diagrams 510 7.2 Programming with Inheritance 513 Constructors in Derived Classes 513 The this Method (Optional) 514 Call to an Overridden Method 515 Programming Example: Multilevel Derived Classes 516 A Subtle Point About Overloading and Overriding (Optional) 521 Programming Tip: An Object Can Have More than One Type 522 Programming Tip: “Is a” and “Has a” Relations 525 The Class Object 526 Case Study: Character Graphics 528 Abstract Classes 538 Interfaces 542 7.3 Dynamic Binding and Polymorphism 544 Dynamic Binding 544 Type Checking and Dynamic Binding 546 Dynamic Binding with toString 547 Polymorphism 548 7.4 Graphics Supplement (Optional) 551 The Class JApplet 551 The Class JFrame 552 Window Events and Window Listeners 555 The ActionListener Interface 556 Programming Example: Happy Face as a JFrame 557 What To Do Next 558 Chapter Summary 559 Answers to Self-Test Questions 559 Programming Projects 565