|
Introduction to Programming and the Java Language |
|
|
|
|
|
3 | (6) |
|
|
|
3 | (4) |
|
|
|
7 | (1) |
|
|
|
8 | (1) |
|
Computer Networks and the Internet |
|
|
8 | (1) |
|
Practice Activity: Displaying System Configuration |
|
|
9 | (3) |
|
Displaying Windows Configuration Information |
|
|
10 | (1) |
|
Displaying Unix/Linux Configuration Information |
|
|
10 | (2) |
|
|
|
12 | (6) |
|
|
|
12 | (3) |
|
Using Hexadecimal Numbers to Represent Binary Numbers |
|
|
15 | (2) |
|
Representing Characters with the Unicode Character Set |
|
|
17 | (1) |
|
|
|
18 | (4) |
|
High- and Low-Level Languages |
|
|
18 | (2) |
|
An Introduction to Object-Oriented Programming |
|
|
20 | (1) |
|
|
|
20 | (2) |
|
An Introduction to Programming |
|
|
22 | (11) |
|
|
|
22 | (1) |
|
Program Design with Pseudocode |
|
|
23 | (3) |
|
Developing a Java Application |
|
|
26 | (1) |
|
Programming Activity 1: Writing a First Java Application |
|
|
27 | (6) |
|
|
|
33 | (2) |
|
Exercises, Problems, and Projects |
|
|
35 | (7) |
|
Programming Building Blocks---Java Basics |
|
|
|
Java Application Structure |
|
|
42 | (3) |
|
Data Types, Variables, and Constants |
|
|
45 | (15) |
|
|
|
46 | (1) |
|
|
|
47 | (1) |
|
Floating-Point Data Types |
|
|
48 | (1) |
|
|
|
48 | (1) |
|
|
|
49 | (1) |
|
Initial Values and Literals |
|
|
49 | (5) |
|
String Literals and Escape Sequences |
|
|
54 | (3) |
|
|
|
57 | (3) |
|
Programming Activity 1: Exploring Data Types |
|
|
60 | (3) |
|
Expressions and Arithmetic Operators |
|
|
63 | (16) |
|
The Assignment Operator and Expressions |
|
|
63 | (1) |
|
|
|
64 | (2) |
|
|
|
66 | (1) |
|
Integer Division and Modulus |
|
|
67 | (3) |
|
|
|
70 | (1) |
|
Mixed-Type Arithmetic and Type Casting |
|
|
71 | (4) |
|
|
|
75 | (4) |
|
Programming Activity 2: Exercising the Arithmetic Operators |
|
|
79 | (2) |
|
|
|
81 | (2) |
|
Exercises, Problems, and Projects |
|
|
83 | (9) |
|
Object-Oriented Programming, Part 1: Using Classes |
|
|
|
Class Basics and Benefits |
|
|
92 | (3) |
|
Creating Objects Using Constructors |
|
|
95 | (3) |
|
|
|
98 | (5) |
|
|
|
103 | (4) |
|
Programming Activity 1: Calling Methods |
|
|
107 | (2) |
|
Calling Static Methods and Using Static Class Variables |
|
|
109 | (2) |
|
|
|
111 | (43) |
|
|
|
111 | (1) |
|
|
|
112 | (4) |
|
|
|
116 | (1) |
|
The to UpperCase and toLowerCase Methods |
|
|
117 | (1) |
|
|
|
117 | (1) |
|
The charAt and substring Methods |
|
|
117 | (2) |
|
|
|
119 | (3) |
|
|
|
122 | (1) |
|
|
|
122 | (2) |
|
|
|
124 | (4) |
|
The Math Class and Random Numbers |
|
|
128 | (3) |
|
|
|
131 | (1) |
|
|
|
132 | (1) |
|
|
|
133 | (1) |
|
|
|
134 | (4) |
|
The Integer, Double, and Other Wrapper Classes |
|
|
138 | (3) |
|
Input and Output Using JOptionPane Dialog Boxes |
|
|
141 | (5) |
|
Input from the Console Using the Scanner Class |
|
|
146 | (8) |
|
Programming Activity 2: Using Predefined Classes |
|
|
154 | (3) |
|
|
|
157 | (2) |
|
Exercises, Problems, and Projects |
|
|
159 | (16) |
|
Introduction to Applets and Graphics |
|
|
|
|
|
175 | (3) |
|
|
|
178 | (2) |
|
Drawing Shapes with Graphics Methods |
|
|
180 | (10) |
|
|
|
190 | (6) |
|
Programming Activity 1: Writing an Applet with Graphics |
|
|
196 | (1) |
|
|
|
197 | (1) |
|
Exercises, Problems, and Projects |
|
|
198 | (8) |
|
Flow of Control, Part 1: Selection |
|
|
|
|
|
206 | (9) |
|
|
|
207 | (1) |
|
|
|
208 | (1) |
|
|
|
209 | (4) |
|
|
|
213 | (2) |
|
|
|
215 | (3) |
|
|
|
218 | (3) |
|
Selection Using if/else if |
|
|
221 | (4) |
|
Sequential and Nested if/else Statements |
|
|
225 | (7) |
|
Sequential if/else Statements |
|
|
225 | (1) |
|
Finding the Minimum or Maximum Values |
|
|
225 | (3) |
|
Nested if/else Statements |
|
|
228 | (1) |
|
|
|
229 | (3) |
|
Testing Techniques for if/else Statements |
|
|
232 | (1) |
|
Programming Activity 1: Working with if/else |
|
|
233 | (3) |
|
Comparing Floating-Point Numbers |
|
|
236 | (4) |
|
|
|
240 | (6) |
|
|
|
240 | (2) |
|
String Comparison Methods |
|
|
242 | (4) |
|
The Conditional Operator (?:) |
|
|
246 | (2) |
|
|
|
248 | (9) |
|
Programming Activity 2: Using the switch Statement |
|
|
257 | (3) |
|
|
|
260 | (1) |
|
Exercises, Problems, and Projects |
|
|
261 | (15) |
|
Flow of Control, Part 2: Looping |
|
|
|
Event-Controlled Loops Using while |
|
|
276 | (1) |
|
General Form for while Loops |
|
|
277 | (2) |
|
|
|
279 | (8) |
|
Reading Data from the User |
|
|
280 | (3) |
|
Reading Data from a Text File |
|
|
283 | (4) |
|
|
|
287 | (19) |
|
|
|
287 | (3) |
|
|
|
290 | (3) |
|
|
|
293 | (3) |
|
Finding Maximum or Minimum Values |
|
|
296 | (3) |
|
|
|
299 | (7) |
|
Type-Safe Input Using Scanner |
|
|
306 | (4) |
|
Constructing Loop Conditions |
|
|
310 | (8) |
|
Testing Techniques for while Loops |
|
|
318 | (2) |
|
Event-Controlled Loops Using do/while |
|
|
320 | (3) |
|
Programming Activity 1: Using while Loops |
|
|
323 | (4) |
|
Count-Controlled Loops Using for |
|
|
327 | (12) |
|
Basic Structure of for Loops |
|
|
327 | (1) |
|
|
|
328 | (10) |
|
Testing Techniques for for Loops |
|
|
338 | (1) |
|
|
|
339 | (7) |
|
Programming Activity 2: Using for Loops |
|
|
346 | (3) |
|
|
|
349 | (2) |
|
Exercises, Problems, and Projects |
|
|
351 | (15) |
|
Object-Oriented Programming, Part 2: User-Defined Classes |
|
|
|
|
|
366 | (2) |
|
Defining Instance Variables |
|
|
368 | (1) |
|
|
|
369 | (2) |
|
|
|
371 | (7) |
|
|
|
378 | (4) |
|
|
|
382 | (5) |
|
Writing Data Manipulation Methods |
|
|
387 | (5) |
|
Programming Activity 1: Writing a Class Definition, Part 1 |
|
|
392 | (3) |
|
The Object Reference this |
|
|
395 | (1) |
|
The toString and equals Methods |
|
|
396 | (5) |
|
|
|
401 | (6) |
|
|
|
407 | (7) |
|
|
|
414 | (7) |
|
Programming Activity 2: Writing a Class Definition, Part 2 |
|
|
421 | (4) |
|
|
|
425 | (8) |
|
Generating Web-Style Documentation with Javadoc |
|
|
433 | (6) |
|
|
|
439 | (2) |
|
Exercises, Problems, and Projects |
|
|
441 | (20) |
|
Single-Dimensional Arrays |
|
|
|
Declaring and Instantiating Arrays |
|
|
461 | (3) |
|
|
|
461 | (1) |
|
|
|
462 | (1) |
|
Combining the Declaration and Instantiation of Arrays |
|
|
463 | (1) |
|
Assigning Initial Values to Arrays |
|
|
464 | (1) |
|
|
|
464 | (5) |
|
Aggregate Array Operations |
|
|
469 | (18) |
|
|
|
470 | (1) |
|
Reading Data into an Array |
|
|
471 | (2) |
|
Summing the Elements of an Array |
|
|
473 | (1) |
|
Finding Maximum or Minimum Values |
|
|
474 | (1) |
|
|
|
475 | (4) |
|
Changing the Size of an Array |
|
|
479 | (2) |
|
Comparing Arrays for Equality |
|
|
481 | (3) |
|
Displaying Array Data as a Bar Chart |
|
|
484 | (3) |
|
Programming Activity 1: Working with Arrays |
|
|
487 | (5) |
|
|
|
492 | (9) |
|
Using Arrays in User-Defined Classes |
|
|
492 | (7) |
|
Retrieving Command Line Arguments |
|
|
499 | (2) |
|
Searching and Sorting Arrays |
|
|
501 | (25) |
|
Sequential Search of an Unsorted Array |
|
|
501 | (4) |
|
|
|
505 | (6) |
|
|
|
511 | (7) |
|
Sorting Arrays of Objects |
|
|
518 | (2) |
|
Sequential Search of a Sorted Array |
|
|
520 | (1) |
|
Binary Search of a Sorted Array |
|
|
521 | (5) |
|
Programming Activity 2: Searching and Sorting Arrays |
|
|
526 | (3) |
|
|
|
529 | (5) |
|
|
|
534 | (1) |
|
Exercises, Problems, and Projects |
|
|
535 | (22) |
|
Multidimensional Arrays and the ArrayList Class |
|
|
|
Declaring and Instantiating Multidimensional Arrays |
|
|
557 | (7) |
|
Declaring Multidimensional Arrays |
|
|
557 | (2) |
|
Instantiating Multidimensional Arrays |
|
|
559 | (1) |
|
Combining the Declaration and Instantiation of Multidimensional Arrays |
|
|
560 | (1) |
|
Assigning Initial Values to Multidimensional Arrays |
|
|
560 | (4) |
|
Accessing Multidimensional Array Elements |
|
|
564 | (6) |
|
Aggregate Two-Dimensional Array Operations |
|
|
570 | (15) |
|
Processing All the Elements of a Two-Dimensional Array |
|
|
570 | (3) |
|
Processing a Given Row of a Two-Dimensional Array |
|
|
573 | (2) |
|
Processing a Given Column of a Two-Dimensional Array |
|
|
575 | (2) |
|
Processing a Two-Dimensional Array One Row at a Time |
|
|
577 | (2) |
|
Processing a Two-Dimensional Array One Column at a Time |
|
|
579 | (3) |
|
Displaying Two-Dimensional Array Data as a Bar Chart |
|
|
582 | (3) |
|
Two-Dimensional Arrays Passed To and Returned From Methods |
|
|
585 | (5) |
|
Programming Activity 1: Working with Two-Dimensional Arrays |
|
|
590 | (5) |
|
Other Multidimensional Arrays |
|
|
595 | (3) |
|
|
|
598 | (13) |
|
Declaring and Instantiating ArrayList Objects |
|
|
599 | (2) |
|
Methods of the ArrayList Class |
|
|
601 | (1) |
|
Looping Through an ArrayList Using an Enhanced for Loop |
|
|
601 | (4) |
|
Using the ArrayList Class in a Program |
|
|
605 | (6) |
|
Programming Activity 2: Working with the ArrayList Class |
|
|
611 | (6) |
|
|
|
617 | (1) |
|
Exercises, Problems, and Projects |
|
|
618 | (23) |
|
Object-Oriented Programming, Part 3: Inheritance, Polymorphism, and Interfaces |
|
|
|
|
|
641 | (1) |
|
|
|
642 | (17) |
|
Inherited Members of a Class |
|
|
643 | (4) |
|
|
|
647 | (5) |
|
Adding Specialization to the Subclass |
|
|
652 | (2) |
|
Overriding Inherited Methods |
|
|
654 | (5) |
|
The Protected Access Modifier |
|
|
659 | (8) |
|
Programming Activity 1: Using Inheritance |
|
|
667 | (4) |
|
Abstract Classes and Methods |
|
|
671 | (10) |
|
|
|
681 | (2) |
|
Programming Activity 2: Using Polymorphism |
|
|
683 | (7) |
|
|
|
690 | (10) |
|
|
|
700 | (2) |
|
Exercises, Problems, and Projects |
|
|
702 | (18) |
|
Exceptions and Input/Output Operations |
|
|
|
Simple Exception Handling |
|
|
720 | (19) |
|
Using try and catch Blocks |
|
|
721 | (10) |
|
Catching Multiple Exceptions |
|
|
731 | (4) |
|
|
|
735 | (4) |
|
|
|
739 | (1) |
|
Reading from the Java Console |
|
|
740 | (9) |
|
Reading and Writing Text Files |
|
|
749 | (11) |
|
|
|
750 | (4) |
|
|
|
754 | (4) |
|
|
|
758 | (2) |
|
Reading Structured Text Files |
|
|
760 | (9) |
|
Parsing a String Using StringTokenizer |
|
|
761 | (2) |
|
Reading Structured Data Using StringTokenizer |
|
|
763 | (6) |
|
Programming Activity 1: Reading from a Structured Text File |
|
|
769 | (4) |
|
Writing and Appending to Structured Text Files |
|
|
773 | (3) |
|
Reading and Writing Objects to a File |
|
|
776 | (10) |
|
|
|
776 | (6) |
|
Reading Objects from Files |
|
|
782 | (4) |
|
Programming Activity 2: Reading Objects from a File |
|
|
786 | (4) |
|
|
|
790 | (1) |
|
Exercises, Problems, and Projects |
|
|
791 | (23) |
|
Graphical User Interfaces |
|
|
|
GUI Applications Using JFrame |
|
|
814 | (5) |
|
|
|
819 | (4) |
|
A Simple Component: JLabel |
|
|
823 | (4) |
|
|
|
827 | (5) |
|
|
|
832 | (6) |
|
|
|
838 | (4) |
|
Radio Buttons and Checkboxes |
|
|
842 | (9) |
|
Programming Activity 1: Working with Buttons |
|
|
851 | (3) |
|
|
|
854 | (4) |
|
|
|
858 | (8) |
|
|
|
866 | (1) |
|
|
|
867 | (11) |
|
Layout Managers: GridLayout |
|
|
878 | (9) |
|
Layout Managers: BorderLayout |
|
|
887 | (6) |
|
Using Panels to Nest Components |
|
|
893 | (5) |
|
Programming Activity 2: Working with Layout Managers |
|
|
898 | (5) |
|
|
|
903 | (2) |
|
Exercises, Problems, and Projects |
|
|
905 | (21) |
|
|
|
|
Simple Recursion: Identifying the General and Base Cases |
|
|
926 | (4) |
|
Recursion with a Return Value |
|
|
930 | (11) |
|
Computing the Factorial of a Number |
|
|
931 | (5) |
|
Computing the Greatest Common Divisor |
|
|
936 | (5) |
|
Recursion with Two Base Cases |
|
|
941 | (4) |
|
Programming Activity 1: Checking for a Palindrome |
|
|
945 | (5) |
|
Binary Search Revisited: A Recursive Solution |
|
|
950 | (6) |
|
Programming Activity 2: The Towers of Hanoi |
|
|
956 | (6) |
|
Animation Using Recursion |
|
|
962 | (4) |
|
Recursion Versus Iteration |
|
|
966 | (2) |
|
|
|
968 | (1) |
|
Exercises, Problems, and Projects |
|
|
969 | (15) |
|
An Introduction to Data Structures |
|
|
|
|
|
984 | (21) |
|
Linked-List Concepts and Structure |
|
|
984 | (2) |
|
|
|
986 | (6) |
|
|
|
992 | (1) |
|
Other Methods of a Linked List |
|
|
993 | (8) |
|
Testing a Linked-List Class |
|
|
1001 | (4) |
|
Implementing a Stack Using a Linked List |
|
|
1005 | (4) |
|
Implementing a Queue Using a Linked List |
|
|
1009 | (5) |
|
Array Representation of Stacks |
|
|
1014 | (5) |
|
Programming Activity 1: Writing Methods for a Stack Class |
|
|
1019 | (4) |
|
Array Representation of Queues |
|
|
1023 | (8) |
|
|
|
1031 | (9) |
|
Programming Activity 2: Writing Insert and Delete Methods for a Sorted Linked List |
|
|
1040 | (5) |
|
|
|
1045 | (5) |
|
Recursively Defined Linked Lists |
|
|
1050 | (9) |
|
|
|
1059 | (1) |
|
Exercises, Problems, and Projects |
|
|
1060 | (21) |
|
Appendix A Java Reserved Words and Keywords |
|
|
1081 | (2) |
|
Appendix B Operator Precedence |
|
|
1083 | (2) |
|
Appendix C The Unicode Character Set |
|
|
1085 | (2) |
|
Appendix D Representing Negative Integers |
|
|
1087 | (4) |
|
Appendix E Representing Floating-Point Numbers |
|
|
1091 | (4) |
|
Appendix F Java Classes APIs |
|
|
1095 | (34) |
|
|
|
1095 | (1) |
|
|
|
1096 | (1) |
|
|
|
1096 | (1) |
|
|
|
1097 | (1) |
|
|
|
1097 | (1) |
|
|
|
1098 | (1) |
|
|
|
1099 | (1) |
|
|
|
1099 | (1) |
|
|
|
1100 | (1) |
|
|
|
1100 | (1) |
|
|
|
1101 | (1) |
|
|
|
1102 | (1) |
|
|
|
1102 | (1) |
|
|
|
1103 | (1) |
|
|
|
1103 | (1) |
|
|
|
1103 | (1) |
|
|
|
1104 | (1) |
|
|
|
1104 | (1) |
|
|
|
1104 | (1) |
|
|
|
1104 | (2) |
|
|
|
1106 | (1) |
|
|
|
1106 | (1) |
|
|
|
1106 | (1) |
|
|
|
1107 | (1) |
|
|
|
1108 | (1) |
|
|
|
1108 | (1) |
|
|
|
1108 | (1) |
|
|
|
1109 | (1) |
|
|
|
1110 | (1) |
|
|
|
1111 | (1) |
|
|
|
1112 | (1) |
|
|
|
1113 | (1) |
|
|
|
1114 | (1) |
|
|
|
1114 | (1) |
|
|
|
1115 | (1) |
|
|
|
1116 | (1) |
|
|
|
1117 | (1) |
|
ListSelectionListener Interface |
|
|
1118 | (1) |
|
|
|
1118 | (1) |
|
|
|
1119 | (1) |
|
|
|
1119 | (1) |
|
MouseMotionListener Interface |
|
|
1120 | (1) |
|
|
|
1120 | (1) |
|
|
|
1121 | (1) |
|
|
|
1121 | (1) |
|
|
|
1122 | (1) |
|
|
|
1123 | (2) |
|
|
|
1125 | (1) |
|
|
|
1126 | (1) |
|
|
|
1127 | (2) |
|
Appendix G Solutions to Selected Exercises |
|
|
1129 | (20) |
| Index |
|
1149 | |