|
Table of Contents
Introduction
Chapter 1 Classes and Objects
1.1 Java Program Structure 11
1.2 A First Program 12
1.3 Defining Methods 13
1.4 Data Types and Variable Declarations 15
Data Type char 16
Integer Types 16
Floating-point Types 17
Variables and Identifiers 17
1.5 Data Abstraction and Encapsulation 18
1.6 Classes and Objects 19
Information Hiding and Member Access Control 20
Creating Objects 22
Constructors 22
Member Access Notations 23
Methods 24
1.7 String Basics 24
String Concatenation 25
String Representation of Objects 25
1.8 Arrays 26
1.9 Method Invocation and Argument Passing 28
1.10 Standard I/O 29
1.11 Command-line Arguments and main 30
1.12 Problem Solving with Objects 32
A Simple Vector2D Class 33
1.13 Solution via Objects 35
1.14 Object-oriented Thinking 38
1.15 Code Organization 39
1.16 Programming Tips 39
Class Style 40
1.17 Summary 41
Exercises 42
Chapter 2 Java Features and Constructs
2.1 ASCII Character I/O 48
2.2 File I/O 49
2.3 Basic Error and Exception Handling 49
2.4 A Class of Fractions 53
2.5 Identifier Scoping 57
2.6 Instance and Classwide Members 59
2.7 Symbolic Constants 62
2.8 Using Arrays: Quicksort 63
2.9 String and StringBuffer 65
2.10 Two-dimensional Arrays 68
2.11 A Matrix Class 68
2.12 Type Conversions 70
2.13 Implicit Type Conversion 71
2.14 Explicit Type Casts 72
2.15 Programming Tips 73
2.16 Summary 73
Exercises 74
Review Questions 74
Programming Assignments 75
Chapter 3 Object-based Programming
3.1 A Term Life Insurance Account 78
3.2 A Term Life Premium Calculator 79
3.3 Java Wrapper Classes 81
3.4 Operations on Characters 82
3.5 A URL Decoder 83
3.6 A Circular Buffer 84
3.7 Pocket Calculator Simulation 88
Program Design 88
The CalcEng Class 89
Testing CalcEng 91
The CalcFace Class 92
Testing CalcFace 94
The Calculator Class 95
3.8 Linked List 96
3.9 Overloading Methods 101
3.10 Storage Allocation and Management 103
Object Storage Allocation with new 103
Garbage Collection 103
The Method finalize 104
3.11 Summary 104
Exercises 105
Review Questions 105
Programming Assignments 106
Chapter 4 Inheritance and Class Extension
4.1 Advantages of Inheritance 110
4.2 Class Extension Basics 110
4.3 Class Scope Nesting 113
4.4 Extended Object Composition 114
Protected Members 114
4.5 Free Checking Account 115
4.6 Access Control Under Class Extension 117
4.7 Class Extension Principles 119
4.8 Subclass Constructors 120
Object Initialization Sequence 121
4.9 Subclass Finalize 121
4.10 Type Relations Under Inheritance 121
4.11 Field Access Under Subclassing 123
4.12 Hiding of Fields and Static Methods 124
4.13 Method Access Under Subclassing 124
4.14 Method Overriding 125
4.15 Dynamic Invocation of Overriding Methods 126
4.16 Example: Numeric Wrapper Classes 127
4.17 Method Overloading in Subclasses 128
4.18 Calculator with SQRT 129
4.19 The Object Class and Generic Codes 130
Generic Hashtable 132
Generic Array 132
4.20 Managing Text Lines 132
4.21 Writing Generic Programs 135
4.22 Generic List 135
4.23 Generic Stack 138
4.24 Summary 139
Exercises 140
Review Questions 140
Programming Assignments 141
Chapter 5 OOP Techniques: Interfaces and Polymorphism
5.1 Programming with Plug-compatible Objects 146
Compatible Types and Polymorphism 147
Keys to Plug Compatibility 148
5.2 Using Plug-compatible Components 148
5.3 Planning Uniform Public Interfaces 150
5.4 Defining Interfaces 151
5.5 Implementing Interfaces 153
5.6 Why Interfaces? 154
5.7 Extending Interfaces 155
5.8 Abstract Superclass 155
5.9 Abstract Sequence 156
Subclass: ArraySequence 160
5.10 A Sequence of Dates 161
5.11 Ordering Text Lines 163
Comparing Keys 163
ArraySequence of Text Lines 165
5.12 Interfaces VS. Abstract Classes 166
5.13 Cloning Objects 167
Copying Using Object.clone 167
Overriding Object.clone 168
Array Copying 170
5.14 Inheritance Planning 170
5.15 Summary 172
Exercises 173
Review Questions 173
Programming Assignments 174
Chapter 6 Packages and Core Classes
6.1 Packages 176
Importing from Packages 176
6.2 Java-supplied Packages 179
6.3 Accessing Java Documentation 179
6.4 Input and Output 180
I/O Stream Hierarchies 181
The Java I/O Model 182
6.5 File I/O 183
6.6 Buffered I/O 184
6.7 Print Writers 184
6.8 Term Life Account Files 185
6.9 File Updating 187
Adding to End of File 189
6.10 Other I/O Streams 189
In-Memory I/O 189
Additional I/O 189
6.11 Textual and Unicode Character I/O 190
6.12 Non-character I/O 192
6.13 Object I/O 193
6.14 Number and Date Formatting 194
6.15 Error and Exception Handling 196
Catching Exceptions 197
Types of Exception 199
Specifying Exceptions 199
Catch or Specify Principle 200
Throwing Exceptions 200
Messages in Exceptions 200
Creating Your Own Exceptions 201
6.16 Matrix with Exceptions: An example 202
6.17 Character-based Standard I/O and Error Reporting 204
6.18 Mathematical Computations 205
6.19 Date and Calendar 206
6.20 System and Environment Properties 207
6.21 Summary 210
Exercises 211
Review Questions 211
Programming Assignments 212
Chapter 7 Applets and Event-Driven Programming
7.1 About Networking 216
Client and Server 216
7.2 The Web 217
Hypertext 218
URL 219
7.3 Web Pages and HTML 220
7.4 Applets 221
The applet Tag 222
Applet vs. Regular Application 223
7.5 A First Applet 224
7.6 Testing and Deploying Applets 226
7.7 Browser Control of Applets 227
Applet Life Cycle 228
7.8 GUI Programming Basics 230
7.9 Event Handling 232
The Delegation Model 233
Event Types and Representation 234
Event Listeners 235
Event Sources 236
Writing Listeners with Adapters 238
7.10 Tic Tac Toe 239
Example Summary 250
7.11 Sound Effects 251
7.12 Inner Classes 253
Inner Class Examples 254
Anonymous Event Handlers 255
7.13 Applet Parameters 256
User-Defined Applet Parameters 256
JAR and the archive Attribute 257
7.14 Applet Security Restrictions 258
7.15 Summary 259
Exercises 260
Review Questions 260
Programming Assignments 261
Chapter 8 GUI Programming
8.1 GUI Programming Overview 264
8.2 Widget Concepts 264
8.3 Swing Basics 265
GUI Program Outline 266
8.4 Overview of Swing Widgets 268
8.5 Swing Widgets 269
8.6 Atomic Widgets 274
Fonts for Widgets 277
8.7 Text Input Widgets 278
8.8 Drawing Custom Graphics 282
A Parameterized Pie Chart 282
8.9 Drawing Support 287
Widget Painting 287
Custom Painting 288
Displaying Images 291
8.10 Raised Pie Chart 291
8.11 Layout Management 295
8.12 Using Dialogs 298
8.13 Using Menus 302
8.14 Event Handling Notes 307
8.15 Applet-application Dual-purpose Programs 308
8.16 Summary 310
Exercises 311
Review Questions 311
Programming Assignments 312
Chapter 9 Generic Containers
9.1 Lists 316
Applying ArrayList 318
Using ArrayStack 319
9.2 Maps 321
HashMap 322
Example: URL Decoding 323
TreeMap 326
9.3 Iterators 326
The Iterator Interface 328
9.4 Sets 329
9.5 Collections Framework Interface Hierarchies 330
9.6 Framework Iterators 332
9.7 Functors 334
9.8 Generic Algorithms for Containers 334
9.9 Synchronized Containers 338
9.10 Read-Only Containers 339
9.11 Container Compatibility 340
9.12 Defining Custom Containers 340
9.13 Summary 342
Exercises 342
Review Questions 342
Programming Assignments 343
Chapter 10 Networking
10.1 Networking by URL 346
10.2 Creating and Manipulating URLs 347
10.3 Reading from a URL 348
10.4 Communicating with a URL 350
10.5 HTTP Basics 351
10.6 A URL Downloader 353
10.7 The POST Query 356
10.8 Query Body Encoding 357
10.9 A POST -Query Client 358
10.10 A Specific URL Client 360
10.11 Networking in Applets 361
10.12 CGI Scripts 363
10.13 Web Server-Side Programming 365
Servlets 367
10.14 Network Communication with Sockets 367
10.15 Stream Socket Clients 369
10.16 Datagram Sockets 372
10.17 Creating Network Servers 373
10.18 Stream Socket Server Example 374
10.19 Remote Method Invocation 378
Remote Objects 378
The Java RMI Architecture 379
A Factorial Server by RMI 380
Deploying The Client and The Server 383
Building an RMI Application 385
Argument Passing in RMI 385
10.20 Summary 386
Exercises 387
Review Questions 387
Programming Assignments 388
Chapter 11 Threads and Concurrent Programming
11.1 What Is a Thread? 389
Mutual Exclusion 391
Synchronization 391
Thread Scheduling 392
Deadlock 392
11.2 Threads in Java 392
11.3 Programming Threads 392
11.4 Launching Threads 393
11.5 Thread Control 394
11.6 Mutual Exclusion for Java Threads 396
Synchronized Methods 398
Synchronized Statements 399
11.7 Coordinating Threads 401
11.8 Consumer and Producer 402
11.9 I/O Between Two Threads 406
11.10 Thread Priorities 407
11.11 Terminating Thread Execution 408
11.12 Runnable Classes 408
11.13 Multithreaded GUI Applications 409
A 60-Minute Timer 410
11.14 Timer with Images 417
11.15 Decoupled Image Loading and Drawing 421
In-memory Image Manipulations 422
11.16 Animation 423
11.17 AnimatorPanel 424
11.18 Animator Applet-Application 427
11.19 The Image Manager 428
11.20 Summary 430
Exercises 431
Review Questions 431
Programming Assignments 432
Chapter 12 Advanced Topics
12.1 Reflection 436
Classes as Objects 436
Inspecting Methods 440
Invoking an Inspected Method 440
12.2 Dynamic Class Loading 443
12.3 The Java Run Time 446
12.4 Running Other Programs from Java 447
12.5 The Java Native Interface 448
12.6 JNI Example: Basic 449
12.7 JNI Example: Argument Passing 453
12.8 JNI Example: Strings 454
12.9 JNI Example: Arrays 456
12.10 Generating Java Documentation in HTML 457
Javadoc Generated Files 460
Doclets 460
12.11 Clipboards 461
12.12 Interapplet Communication 465
12.13 Java Security Manager 467
Security Checks 467
Writing a Security Manager 468
Security Policy Files 468
12.14 Signed Applets and Dynamic Trust Management 470
Accepting Signed Applets 471
Creating Signed Applets 471
12.15 Summary 472
Exercises 473
Review Questions 473
Programming Assignments 474
Chapter 13 Object-Oriented Design
13.1 Decomposition Approaches 478
Procedural Decomposition 478
Data Decomposition 479
Object-Oriented Decomposition 479
13.2 Object-Oriented Design Principles 480
Identifying Classes and Objects 481
External Behavior of Objects 481
Designing Objects 481
Relationships among Objects and Classes 482
Implementation 482
13.3 Design Patterns 482
13.4 Unified Modeling Language (UML) 484
13.5 The CRC Method 485
13.6 Pocket Calculator Simulation 486
CRC Design 487
Basic Calculator 487
Memory and Scientifice Engines 488
Window Calculator 488
Implementation 489
Class WinCalculator 490
Class WinFace 491
Class CalcWindow 493
Class Keypad 494
Class Lcd 496
13.7 The MVC Design Pattern 497
13.8 Swing Widget Architecture and MVC 499
MVC Structure of JList 500
Lunch Menu: An MCV Example 501
13.9 Summary 504
Exercises 505
Review Questions 505
Programming Assignments 505
Appendices
A Java Tools 507
B Java Keywords, Operators, and Packages 513
C Common Java Constructs 517
D Major Differences Between Java and ANSI C/C++ 523
E Layout Managers and Swing Widgets 527
F Colors in JFC 535
G Java I/O Classes 537
H Mathematical Computations 541
I The Java Debugger: jdb 543
J Index of Classes 549
K Basic Background 555
|
|