Chapter 1 Introduction to Programming and the Java Language 1.1 Basic Computer Concepts 5 1.1.1 Hardware 5 1.1.2 Operating Systems 8 1.1.3 Application Software 9 1.1.4 Computer Networks and the Internet 9 1.2 Practice Activity: Displaying System Configuration 11 1.2.1 Displaying Windows Configuration Information 11 1.2.2 Displaying Unix/Linux Configuration Information 12 1.3 Data Representation 14 1.3.1 Binary Numbers 14 1.3.2 Using Hexadecimal Numbers to Represent Binary Numbers 18 1.3.3 Representing Characters with the Unicode Character Set 19 1.4 Programming Languages 19 1.4.1 High- and Low-Level Languages 19 1.4.2 An Introduction to Object-Oriented Programming 21 1.4.3 The Java Language 22
xx Contents 1.5 An Introduction to Programming 23 1.5.1 Programming Basics 23 1.5.2 Program Design with Pseudocode 24 1.5.3 Developing a Java Application 28 1.5.4 Programming Activity 1: Writing a First Java Application 29 Debugging Techniques 31 Testing Techniques 34 1.6 Chapter Summary 35 1.7 Exercises, Problems, and Projects 37 Chapter 2 Programming Building Blocks Java Basics 2.1 Java Application Structure 42 2.2 Data Types, Variables, and Constants 45 2.2.1 Declaring Variables 46 2.2.2 Integer Data Types 47 2.2.3 Floating-Point Data Types 48 2.2.4 Character Data Type 48 2.2.5 Boolean Data Type 49 2.2.6 Initial Values and Literals 49 2.2.7 String Literals and Escape Sequences 54 2.2.8 Constants 57 2.3 Programming Activity 1: Exploring Data Types 60 2.4 Expressions and Arithmetic Operators 63 2.4.1 The Assignment Operator and Expressions 63 2.4.2 Arithmetic Operators 64 2.4.3 Operator Precedence 66 2.4.4 Integer Division and Modulus 67 2.4.5 Division by Zero 70 2.4.6 Mixed-Type Arithmetic and Type Casting 71 2.4.7 Shortcut Operators 75 2.5 Programming Activity 2: Exercising the Arithmetic Operators 79 2.6 Chapter Summary 81 2.7 Exercises, Problems, and Projects 83
xxi Chapter 3 Object-Oriented Programming, Part 1: Using Classes 3.1 Class Basics and Benefits 92 3.2 Creating Objects Using Constructors 95 3.3 Calling Methods 98 3.4 Using Object References 103 3.5 Programming Activity 1: Calling Methods 107 3.6 Calling Static Methods and Using Static Class Variables 109 3.7 Using Predefined Classes 111 3.7.1 Java Packages 111 3.7.2 The String Class 112 The length Method 116 The touppercase and tolowercase Methods 117 The indexof Methods 117 The charat and substring Methods 117 3.7.3 Using System.out 119 3.7.4 Formatting Output 122 The NumberFormat Class 122 The DecimalFormat Class 124 3.7.5 The Math Class and Random Numbers 128 The pow Method 131 The round Method 132 The min and max Methods 133 The random Method 134 3.7.6 The Integer, Double, and Other Wrapper Classes 138 3.7.7 Input and Output Using JOptionPane Dialog Boxes 141 3.7.8 Input from the Console Using the Scanner Class 146 3.8 Programming Activity 2: Using Predefined Classes 154 3.9 Chapter Summary 157 3.10 Exercises, Problems, and Projects 159
xxii Contents Chapter 4 Introduction to Applets and Graphics 4.1 Applet Structure 175 4.2 Executing an Applet 178 4.3 Drawing Shapes with Graphics Methods 180 4.4 Using Color 190 4.5 Programming Activity 1: Writing an Applet with Graphics 196 4.6 Chapter Summary 197 4.7 Exercises, Problems, and Projects 198 Chapter 5 Flow of Control, Part 1: Selection 5.1 Forming Conditions 206 5.1.1 Equality Operators 207 5.1.2 Relational Operators 208 5.1.3 Logical Operators 209 DeMorgan s Laws 213 5.2 Simple Selection with if 215 5.3 Selection Using if/else 218 5.4 Selection Using if/else if 221 5.5 Sequential and Nested if/else Statements 225 5.5.1 Sequential if/else Statements 225 Finding the Minimum or Maximum Values 225 5.5.2 Nested if/else Statements 228 Dangling Else 229 5.6 Testing Techniques for if/else Statements 232 5.7 Programming Activity 1: Working with if/else 233 5.8 Comparing Floating-Point Numbers 236 5.9 Comparing Objects 240 5.9.1 The equals Method 240 5.9.2 String Comparison Methods 242 5.10 The Conditional Operator (?:) 246 5.11 The switch Statement 248 5.12 Programming Activity 2: Using the switch Statement 257 5.13 Chapter Summary 260 5.14 Exercises, Problems, and Projects 261
xxiii Chapter 6 Flow of Control, Part 2: Looping 6.1 Event-Controlled Loops Using while 276 6.2 General Form for while Loops 277 6.3 Event-Controlled Looping 279 6.3.1 Reading Data from the User 280 6.3.2 Reading Data from a Text File 283 6.4 Looping Techniques 287 6.4.1 Accumulation 287 6.4.2 Counting Items 290 6.4.3 Calculating an Average 293 6.4.4 Finding Maximum or Minimum Values 296 6.4.5 Animation 299 6.5 Type-Safe Input Using Scanner 306 6.6 Constructing Loop Conditions 310 6.7 Testing Techniques for while Loops 318 6.8 Event-Controlled Loops Using do/while 320 6.9 Programming Activity 1: Using while Loops 323 Task Instructions 324 Troubleshooting 326 6.10 Count-Controlled Loops Using for 327 6.10.1 Basic Structure of for Loops 327 6.10.2 Constructing for Loops 328 6.10.3 Testing Techniques for for Loops 338 6.11 Nested Loops 339 6.12 Programming Activity 2: Using for Loops 346 Instructions 346 Troubleshooting 349 6.13 Chapter Summary 349 6.14 Exercises, Problems, and Projects 351 Chapter 7 Object-Oriented Programming, Part 2: User-Defined Classes 7.1 Defining a Class 366 7.2 Defining Instance Variables 368 7.3 Writing Class Methods 369 7.4 Writing Constructors 371 7.5 Writing Accessor Methods 378
xxiv Contents 7.6 Writing Mutator Methods 382 7.7 Writing Data Manipulation Methods 387 7.8 Programming Activity 1: Writing a Class Definition, Part 1 392 7.9 The Object Reference this 395 7.10 The tostring and equals Methods 396 7.11 Static Class Members 401 7.12 Graphical Objects 407 7.13 Enumeration Types 414 7.14 Programming Activity 2: Writing a Class Definition, Part 2 421 7.15 Creating Packages 425 7.16 Generating Web-Style Documentation with Javadoc 433 7.17 Chapter Summary 439 7.18 Exercises, Problems, and Projects 441 Chapter 8 Single-Dimensional Arrays 8.1 Declaring and Instantiating Arrays 461 8.1.1 Declaring Arrays 461 8.1.2 Instantiating Arrays 462 8.1.3 Combining the Declaration and Instantiation of Arrays 463 8.1.4 Assigning Initial Values to Arrays 464 8.2 Accessing Array Elements 464 8.3 Aggregate Array Operations 469 8.3.1 Printing Array Elements 470 8.3.2 Reading Data into an Array 471 8.3.3 Summing the Elements of an Array 473 8.3.4 Finding Maximum or Minimum Values 474 8.3.5 Copying Arrays 475 8.3.6 Changing the Size of an Array 479 8.3.7 Comparing Arrays for Equality 481 8.3.8 Displaying Array Data as a Bar Chart 484 8.4 Programming Activity 1: Working with Arrays 487 Instructions 488 Troubleshooting 491
xxv 8.5 Using Arrays in Classes 492 8.5.1 Using Arrays in User-Defined Classes 492 8.5.2 Retrieving Command Line Arguments 499 8.6 Searching and Sorting Arrays 501 8.6.1 Sequential Search of an Unsorted Array 501 8.6.2 Selection Sort 505 8.6.3 Bubble Sort 511 8.6.4 Sorting Arrays of Objects 518 8.6.5 Sequential Search of a Sorted Array 520 8.6.6 Binary Search of a Sorted Array 521 8.7 Programming Activity 2: Searching and Sorting Arrays 526 Instructions 527 Troubleshooting 528 8.8 Using Arrays as Counters 529 8.9 Chapter Summary 534 8.10 Exercises, Problems, and Projects 535 Chapter 9 Multidimensional Arrays and the ArrayList Class 9.1 Declaring and Instantiating Multidimensional Arrays 557 9.1.1 Declaring Multidimensional Arrays 557 9.1.2 Instantiating Multidimensional Arrays 559 9.1.3 Combining the Declaration and Instantiation of Multidimensional Arrays 560 9.1.4 Assigning Initial Values to Multidimensional Arrays 560 9.2 Accessing Multidimensional Array Elements 564 9.3 Aggregate Two-Dimensional Array Operations 570 9.3.1 Processing All the Elements of a Two- Dimensional Array 570 9.3.2 Processing a Given Row of a Two- Dimensional Array 573 9.3.3 Processing a Given Column of a Two- Dimensional Array 575 9.3.4 Processing a Two-Dimensional Array One Row at a Time 577
xxvi Contents 9.3.5 Processing a Two-Dimensional Array One Column at a Time 579 9.3.6 Displaying Two-Dimensional Array Data as a Bar Chart 582 9.4 Two-Dimensional Arrays Passed To and Returned From Methods 585 9.5 Programming Activity 1: Working with Two- Dimensional Arrays 590 Instructions 591 Troubleshooting 594 9.6 Other Multidimensional Arrays 595 9.7 The ArrayList Class 598 9.7.1 Declaring and Instantiating ArrayList Objects 599 9.7.2 Methods of the ArrayList Class 601 9.7.3 Looping Through an ArrayList Using an Enhanced for Loop 601 9.7.4 Using the ArrayList Class in a Program 605 9.8 Programming Activity 2: Working with the ArrayList Class 611 Instructions 611 Troubleshooting 615 9.9 Chapter Summary 617 9.10 Exercises, Problems, and Projects 618 Chapter 10 Object-Oriented Programming, Part 3: Inheritance, Polymorphism, and Interfaces 10.1 Inheritance 641 10.2 Inheritance Design 642 10.2.1 Inherited Members of a Class 643 10.2.2 Subclass Constructors 647 10.2.3 Adding Specialization to the Subclass 652 10.2.4 Overriding Inherited Methods 654 10.3 The Protected Access Modifier 659 10.4 Programming Activity 1: Using Inheritance 667 Instructions 667
xxvii 10.5 Abstract Classes and Methods 671 10.6 Polymorphism 681 10.7 Programming Activity 2: Using Polymorphism 683 Instructions 687 10.8 Interfaces 690 10.9 Chapter Summary 700 10.10 Exercises, Problems, and Projects 702 Chapter 11 Exceptions and Input/Output Operations 11.1 Simple Exception Handling 720 11.1.1 Using try and catch Blocks 721 11.1.2 Catching Multiple Exceptions 731 11.1.3 User-Defined Exceptions 735 11.2 The java.io Package 739 11.3 Reading from the Java Console 740 11.4 Reading and Writing Text Files 749 11.4.1 Reading Text Files 750 11.4.2 Writing to Text Files 754 11.4.3 Appending to Text Files 758 11.5 Reading Structured Text Files 760 11.5.1 Parsing a String Using StringTokenizer 761 11.5.2 Reading Structured Data Using StringTokenizer 763 11.6 Programming Activity 1: Reading from a Structured Text File 769 Instructions 769 If you have time... 772 Troubleshooting 772 11.7 Writing and Appending to Structured Text Files 773 11.8 Reading and Writing Objects to a File 776 11.8.1 Writing Objects to Files 776 11.8.2 Reading Objects from Files 782 11.9 Programming Activity 2: Reading Objects from a File 786 Task Instructions: Reading from the transactions.obj File 786 If you have time... 789 Troubleshooting 789
xxviii Contents 11.10 Chapter Summary 790 11.11 Exercises, Problems, and Projects 791 Chapter 12 Graphical User Interfaces 12.1 GUI Applications Using JFrame 814 12.2 GUI Components 819 12.3 A Simple Component: JLabel 823 12.4 Event Handling 827 12.5 Text Fields 832 12.6 Command Buttons 838 12.7 Radio Buttons and Checkboxes 842 12.8 Programming Activity 1: Working with Buttons 851 Instructions 852 Troubleshooting 853 12.9 Lists 854 12.10 Combo Boxes 858 12.11 Adapter Classes 866 12.12 Mouse Movements 867 12.13 Layout Managers: GridLayout 878 12.14 Layout Managers: BorderLayout 887 12.15 Using Panels to Nest Components 893 12.16 Programming Activity 2: Working with Layout Managers 898 Instructions 900 12.17 Chapter Summary 903 12.18 Exercises, Problems, and Projects 905 Chapter 13 Recursion 13.1 Simple Recursion: Identifying the General and Base Cases 926 13.2 Recursion with a Return Value 930 13.2.1 Computing the Factorial of a Number 931 13.2.2 Computing the Greatest Common Divisor 936 13.3 Recursion with Two Base Cases 941 13.4 Programming Activity 1: Checking for a Palindrome 945 Instruction 947
xxix Task Instructions 949 Troubleshooting 950 13.5 Binary Search Revisited: A Recursive Solution 950 13.6 Programming Activity 2: The Towers of Hanoi 956 Instructions 959 Task Instructions 961 Troubleshooting 962 13.7 Animation Using Recursion 962 13.8 Recursion Versus Iteration 966 13.9 Chapter Summary 968 13.10 Exercises, Problems, and Projects 969 Chapter 14 An Introduction to Data Structures 14.1 Linked List 984 14.1.1 Linked-List Concepts and Structure 984 14.1.2 A Linked-List Shell 986 14.1.3 Generating an Exception 992 14.1.4 Other Methods of a Linked List 993 14.1.5 Testing a Linked-List Class 1001 14.2 Implementing a Stack Using a Linked List 1005 14.3 Implementing a Queue Using a Linked List 1009 14.4 Array Representation of Stacks 1014 14.5 Programming Activity 1: Writing Methods for a Stack Class 1019 Instructions 1020 Troubleshooting 1021 14.6 Array Representation of Queues 1023 14.7 Sorted Linked Lists 1031 14.8 Programming Activity 2: Writing Insert and Delete Methods for a Sorted Linked List 1040 Instructions 1040 Troubleshooting 1045 14.9 Doubly Linked Lists 1045 14.10 Recursively Defined Linked Lists 1050 14.11 Chapter Summary 1059 14.12 Exercises, Problems, and Projects 1060
xxx Contents Appendix A Java Reserved Words and Keywords 1081 Appendix B Operator Precedence 1083 Appendix C The Unicode Character Set 1085 Appendix D Representing Negative Integers 1087 Appendix E Representing Floating-Point Numbers 1091 Appendix F Java Classes APIs 1095 ActionEvent 1095 ActionListener Interface 1096 ArrayList 1096 BorderLayout 1097 BufferedReader 1097 BufferedWriter 1098 ButtonGroup 1099 Color 1099 Container 1100 DecimalFormat 1100 Double 1101 Enum 1102 Exception 1102 File 1103 FileInputStream 1103 FileOutputStream 1103 FileReader 1104 FileWriter 1104 FlowLayout 1104 Graphics 1104 GridLayout 1106 InputStreamReader 1106 Integer 1106 ItemEvent 1107 ItemListener Interface 1108
xxxi JButton 1108 JCheckBox 1108 JComboBox 1109 JComponent 1110 JFrame 1111 JLabel 1112 JList 1113 JOptionPane 1114 JPasswordField 1114 JRadioButton 1115 JTextArea 1116 JTextField 1117 ListSelectionListener Interface 1118 Math 1118 MouseEvent 1119 MouseListener Interface 1119 MouseMotionListener Interface 1120 NumberFormat 1120 ObjectInputStream 1121 ObjectOutputStream 1121 PrintWriter 1122 Scanner 1123 String 1125 StringTokenizer 1126 System 1127 System.out 1127 Appendix G Solutions to Selected Exercises 1129 Index 1149