Part 1 Using Objects
Programming and the Java Language
1.1 Programs and Programming
1.2 Algorithms
1.3 High-Level Languages and Programs
1.4 A Simple Computer
1.5 Machine Code
1.6 Files and the Filing System
1.7 The World Wide Web
1.8 The Java Programming Language
1.9 Getting Started with Java
1.10 A First Java Program
1.11 The pti nt and println Methods
1.12 How This Book Is Arranged
1.13 Key Points in Chapter 1
1.14 Exercises
2 Object Orientation
2.1 Objects and Classes
2.2 Software Objects
2.3 More about Single Objects
2.4 An Object-Oriented Program
2.5 Types
2.6 Classes and Instances Revisited
2.7 Key Points in Chapter 2
2.8 Exercises
3 Declaring Objects and Calling Methods
3.1 Program Structure
3.2 The class Person
3.3 A Program to Manipulate a Person
3.4 The i mp0rt Statement
3.5 Declaring Objects
3.6 Using Methods to Set the Attributes of Objects
3.7 Using Methods to Extract Object Attributes
3.8 Using Constants in Java
3.9 Using Objects and Methods
3.10 A Variety of Methods
3.11 Constructors Revisited
3.12 Input to a Program (Optional)
3.13 Key Points in Chapter 3
3.14 Exercises
4 Selecting Among Alternatives
4.1 Reading Values from the Keyboard
4.2 More on Integer Variables
4.3 Type Checking
4.4 Making Decisions
4.5 Selection Statements in Java
4.5.1 Statements
4.5.2 Relational Operators and Boolean Expressions
4.6 The Boolean Type
4.7 The switch Statement
4.8 Testing a New Class
4.9 Key Points in Chapter 4
4.10 Exercises
5 Repetition
5.1 Repetition as a Basic Control Structure
5.2 Looping a Predetermined Number of Times: The for Statement
5.3 Looping an Indeterminate Number of Times: The whi 1 e Statement
5.4 Stopping in the Middle of an Iteration
5.5 for Loops and while Loops
5.6 Nested Loops
5.7 Boolean Expressions for Loops
5.8 Testing at the End of the Loop
5.9 Other Java Loop Features
5.10 Key Points in Chapter 5
5.11 Exercises
6 Basic Java Data Types
6.1 Objects and Basic Data Types
6.1.1 Declaration
6.1.2 Setting a Value
6.1.3 Obtaining a Value
6.1.4 Arguments to Methods
6.2 The int Data Type
6.3 Other Whole Number Data Types in Java (Optional)
6.4 Floating-Point Data Types
6.5 The b001 ean Data Type
6.6 The char Data Type
6.7 The String Class
6.8 Methods for the String Class
6.9 Wrapper Classes (Optional)
6.10 Key Points in Chapter 6
6.11 Exercises
Part 2 Writing Objects
7 A Simple Class
7.1 What We Are Trying to Achieve
7.2 Providing the Person Class
7.3 Methods for the Person Class
7.4 Actual and Formal Arguments
7.5 Modes of Argument Passing
7.6 Return Values
7.7 Lexical Conventions within a Class
7.8 Key Points in Chapter 7
7.9 Exercises
8 More on the Simple Class
8.1 Constructor Methods
8.2 Overloading
8.3 Class Constants
8.4 Class Variables
8.5 Private Methods
8.6 Class or Static Methods
8.7 Revisiting the Main Class
8.8 Packages and Directories
8.8.1 The i mp0rt Statement
8.9 Scope and Visibility
8.9.1 Intraclass Visibility
8.9.2 Use of this
8.9.3 Interclass Visibility
8.10 Key Points in Chapter 8
8.11 Exercises
9 Arrays
9.1 Collections of Elements
9.2 Arrays of Objects
9.3 Searching an Array
9.4 Binary Search
9.5 Sorting an Array
9.6 Arrays as Arguments
9.7 Multidimensional Arrays
9.8 Nonrectangular Arrays (Optional)
9.9 Key Points in Chapter 9
9.10 Exercises
10 Objects within Objects
10.1 What We Are Trying to Achieve
10.2 Writing the 0urDate Class
10.3 Using the OurDate Class
10.4 Objects as Arguments
10.5 Multiple References to the Same Object
10.6 Objects as Arguments and Return Values: Call by Reference
10.6.1 Changing the Contents of the Formal and Actual Arguments
10.7 Hiding References to Other Objects
10.8 Key Points in Chapter 10
10.9 Exercises
11 Putting Objects to Work
11.1 A Task Organizer Program
11.2 A Priority Queue Class
11.3 Implementing a Priority Queue with an Array
11.4 Alternative Implementations of PriorityQueue (Optional)
11.5 Testing the PriorityQueue Class
11.6 Using the PriorityQueue Class
11.7 Outstanding Issues
11.8 Key Points in Chapter 11
11.9 Exercises
Part 3 Advanced Objects
12 Introduction to Inheritance
12.1 Motivation
12.1.1 Data Modeling
12.1.2 Programming
12.2 What's the Difference?
12.3 Overriding Inherited Methods
12.4 Access Rights and Subclasses
12.5 Airplane Reservations: An Example
12.6 Key Points in Chapter 12
12.7 Exercises
13 Class and Method Polymorphism
13.1 Person and Student: An Example
13.2 Constructor Methods and Inheritance
13.2.1 Constructor Chaining
13.3 Multiple Levels of Inheritance: The Inheritance Hierarchy
13.4 The Class Object
13.5 Polymorphism
13.6 Polymorphism and Heterogeneous Collections
13.6.1 Dynamic Method Binding (Late Binding)
13.7 Calling Overridden Methods
13.8 Methods in Derived Classes
13.9 Key Points in Chapter 13
13.10 Exercises
14 Abstract Classes and Interfaces
14.1 Abstract Classes
14.2 Polymorphism
14.3 Interfaces
14.4 Key Points in Chapter 14
14.5 Exercises
15 Throwing and Catching Exceptions
15.1 Motivation: Robust Programs
15.2 Defining a New Exception
15.3 Throwing an Exception
15.4 Catching an Exception
15.4.1 The finally Clause (Optional)
15.5 Key Points in Chapter 15
15.6 Exercises
16 Graphics and the Abstract Windowing Toolkit
16.1 Graphical User Interfaces
16.2 A Simple Program with a Graphical Interface
16.3 Writing the Chapterl6n0 Class
16.3.1 The Constructor for the Chapterl6n0 Class
16.3.2 Other Layout Managers
16.3.3 The main Method for the Chapterl6n0 Class
16.3.4 The actionPerformed Method of the Chapterl6nO Class
16.3.5 The windowClosing Method of the Chapterl6n0 Class
16.4 Writing the CanvasO Class
16.5 Writing Text on the Canvas
16.6 Animating the Simple Graphics Program
16.7 Input of Character Strings in a Graphical Interface
16.7.1 Setting Up the Picture
16.7.2 Getting a String from a WextField
16.7.3 Drawing the Thermometer
16.8 Menus, Files, and Images (Optional)
16.8.1 Setting Up Menus
16.8.2 Selecting a File
16.8.3 Displaying an Image
16.8.4 Tracking the Mouse
16.9 Key Points in Chapter 16
16.10 Exercises
Part 4 Advanced Java
17 Linked Data Structures
17.1 Linear and Linked Data Structures
17.2 Implementing a Priority Queue Using a Linked Data Structure
17.3 Methods for the PriorityOueue Class
17.3.1 The length Method
17.3.2 The first Method
17.3.3 The remove Method
17.4 The insert Method
17.5 Deletion from a Linked Data Structure (Optional)
17.6 Doubly Linked Lists (Optional)
17.7 Using Linked Data Structures
17.8 Key Points in Chapter 17
17.9 Exercises
18 Recursion and Binary Trees
18.1 Recursion
18.2 Solving the Towers of Hanoi Problem
18.2.1 A Recursive Solution to the Towers of Hanoi Problem
18.2.2 An Iterative Solution to the Towers of Hanoi Problem
18.3 Binary Trees
18.3.1 Searching and Updating a Binary Tree
18.3.2 Writing the Code for the Binary Tree
18.3.3 Adding a Word Occurrence to the Lexicon
18.3.4 Outputting the Lexicon Information
18.4 Key Points in Chapter 18
18.5 Exercises
19 Input and Output in Java
19.1 Input and Output Systems
19.2 The Java Classes for Input and Output
19.3 The PrintStream Class and System.out
19.3.1 Output Redirection
19.4 The BufferedReader Class and System. in
19.4.1 Tokenizing an Input Line
19.4.2 Converting Strings to Numeric Values
19.4.3 Redirecting Input
19.5 Files and File Handling
19.6 Reading and Writing Files
19.6.1 Writing to a File
19.6.2 Reading from a File
19.7 Binary Files (Optional)
19.8 Random Access Files (Optional)
19.9 Accessing Other Computers (Optional)
19.10 Key Points in Chapter 19
19.11 Exercises
20 Creating and Using Applets
20.1 Creating Applets
20.2 Using Applets
20.3 More about Applets
20.4 A Useful Applet
20.4.1 ThereadIndex Method
20.4.2 The act i0nPerf0rmed Method
20.5 Security Aspects of the Use of Applets
20.6 Key Points in Chapter 20
20.7 Exercises
21 Other Features of Java
21.1 Vectors and Other Java Data Structures
21.1.1 The Vector Class
21.1.2 The Hashtable Class
21.2 Strings and StringBuffers
21.3 Run-Time Type Information (Optional)
21.4 Threads (Optional)
21.4.1 Synchronizing Threads
21.5 Key Points in Chapter 21
21.6 Exercises
Part 5 Object-Oriented Design
22 Object-Oriented Design
22.1 Software Engineering
22.2 The Software Life Cycle
22.2.1 Requirements
22.2.2 Design
22.2.3 Coding
22.2.4 Testing
22.2.5 Maintenance
22.3 Design
22.3.1 The Design Process
22.3.2 Functional Design
22.4 Object-Oriented Design (OOD)
22.4.1 Capturing Our Design: A Design Notation
22.4.2 Object Identification
22.5 Key Points in Chapter 22
22.6 Exercises
22.7 References
23 Case Study: Implementing the Personal Organizer 1
23.1 First Steps in the Design
23.2 File Organization
23.2.1 Index Sequential Access
23.2.2 The Main File
23.2.3 The Rand0mAccessFile Class
23.2.4 The Index
23.2.5 Suitability of the Vector Class for Internal Representation of the Index
23.2.6 Suitability of the Hashtabl e Class for Internal Representation of the Index
23.2.7 Using the Vector Class Indirectly
23.3 The Classes in Detail
23.3.1 Filing System Considerations
23.3.2 Clientship
23.4 Moving toward Implementation
23.4.1 The DirBase Class
23.4.2 The DirEntry Class
23.4.3 The IndexElem Class
23.4.4 The Index Class
23.5 Key Points in Chapter 23
23.6 Exercise
23.7 Reference
24 Case Study: Implementing the Personal Organizer 2
24.1 Completing the Implementation
24.2 Implementation of DirBase, Index, IndexElem, and DirEntry
24.2.1 D irEntry Class Source and Commentary
24.2.2 IndexElem Class Source and Commentary
24.2.3 Index Class Source and Commentary
24.2.4 DirBase Class Source and Commentary
24.3 Testing What We Have Done So Far
24.3.1 Using a StreamTokenizer
24.3.2 Test-Based Interface: Intermediate Application and Testing
24.3.3 What Are We Testing?
24.4 Graphical User Interface: The Final Prototype Application
24.4.1 Testing the Graphical Interface
24.5 Using Inheritance
24.6 Key Points in Chapter 24
24.7 Exercises
25 Criteria for a Good Object-Oriented Design
25.1 Introduction
25.2 Cohesion
25.3 Coupling
25.3.1 The Law of Demeter
25.4 Clarity
25.5 Extensibility of Our Design
25.5.1 Adding an Email Attribute to a Directory Entry
25.5.2 Adding a Diary Feature to the Personal Organizer
25.6 Key Points in Chapter 25
25.7 Exercises
25.8 References
Part 6 Appendixes
A Getting Started with Java
B Keywords in Java
C ASCII and Unicode Characters
D Program Listings
D.1 Person. Java
D.2 Chapter20n2. java
D.3 The GUI Source Code for the Java Persona] Organizer
D.3.1 The Gui Class
D.3.2 The DirGui Class
D.3.3 The BrowseRecGui Class
D.3.4 The NewRecGui Class
D.3.5 The AlertDialog Class
Index