Preface ...................................................... xvii
Chapter 1 Introduction ......................................... 1
1.1 A Brief History of Java .................................... 2
1.2 What Is a Program? ......................................... 2
1.3 How Computers Work ......................................... 3
1.4 Compiling and Running a Java Program ....................... 4
1.5 The Software Development Method ............................ 6
1.6 A Java Program: HelloWorld ................................. 6
1.6.1 Compiling the HelloWorld Program .................... 7
1.6.2 Running the HelloWorld Program ...................... 9
1.7 The Parts of a Java Program ............................... 10
1.8 Errors in Programs ........................................ 12
1.8.1 Compiler Errors .................................... 12
1.8.2 Run-Time Errors .................................... 15
1.8.3 Logic Errors ....................................... 15
1.9 Summary ................................................... 16
Exercises ...................................................... 16
Chapter 2 Introduction to Classes and Objects ................. 17
2.1 Thinking Objectively ...................................... 18
2.2 Creating Objects .......................................... 18
2.3 How Methods Work .......................................... 21
2.4 The Computer Screen ....................................... 21
2.4.1 Window Coordinates ................................. 22
2.5 Creating a Rectangle Object ............................... 24
2.6 The DrawingKit Class ...................................... 25
2.6.1 Compiling and Running Your Program with Class
DrawingKit ......................................... 26
2.6.2 Nuts and Bolts of the Program ...................... 28
2.6.3 Coloring a Rectangle ............................... 30
2.7 Creating and Displaying Graphics Objects .................. 30
2.7.1 Drawing and Coloring an Ellipse .................... 31
2.7.2 Drawing a Line ..................................... 32
2.7.3 Changing the Line Thickness ........................ 32
2.7.4 Writing Text ....................................... 34
2.7.5 Drawing a Curve .................................... 35
2.7.6 Drawing an Image Stored in a File .................. 36
2.7.7 Changing the Window Size ........................... 36
2.8 Writing to the Console .................................... 38
2.9 Summary ................................................... 39
Exercises ................................................. 41
Further Reading ........................................... 43
References ................................................ 43
Chapter 3 Programming Basics .................................. 45
3.1 Primitive Data Types ...................................... 46
3.1.1 Declaring and Initializing Primitive Variables
3.2 Identifiers ............................................... 48
3.3 Literals .................................................. 49
3.4 Strings ................................................... 50
3.5 Constants ................................................. 51
3.6 Assignment Statements ..................................... 52
3.7 Conversion and Casting .................................... 53
3.8 Using Constructors and Methods ............................ 54
3.8.1 Using Variables as Arguments ....................... 56
3.8.2 Methods That Return Values ......................... 59
3.9 The Scanner Class ......................................... 59
3.10 Operators ................................................. 60
3.10.1 Arithmetic Operators ............................... 60
3.10.2 Unary Operators .................................... 62
3.10.3 Assignment Operators ............................... 64
3.10.4 Operator Precedence ................................ 64
3.11 Classes and Packages ...................................... 66
3.11.1 Using Packages ..................................... 67
3.12 Introduction to Digital Images ............................ 68
3.12.1 File Formats ....................................... 72
3.12.2 Java 2D Classes for Handling Images ................ 72
3.12.3 Displaying an Image with DrawingKit ................ 73
3.12.4 Retrieving Samples from an Image File .............. 74
3.13 Computers in Engineering: GPS Receivers ................... 77
3.13.1 2D Trilateration ................................... 77
3.13.2 2D Trilateration Program ........................... 79
3.14 Summary ................................................... 81
Exercises ................................................. 83
Further Reading ........................................... 88
References ................................................ 88
Chapter 4 Control Flow Statements ............................. 91
4.1 Selection Statements and Loops ............................ 92
4.2 Flowcharts ................................................ 92
4.3 Conditions ................................................ 93
4.4 Selection Statements ...................................... 95
4.4.1 The if Statement ................................... 96
4.4.2 The if-else Statement .............................. 96
4.4.3 The switch Statement .............................. 100
4.5 Scope of Variables ....................................... 104
4.6 Loops .................................................... 105
4.6.1 The while Loop .................................... 105
4.6.2 The do-while Loop ................................. 110
4.6.3 The for Loop ...................................... 111
4.7 Conditions Using Logical Operators ....................... 114
4.7.1 Operator Precedence ............................... 116
4.8 break and continue Statements ............................ 117
4.9 Nested for Loops ......................................... 119
4.10 Modifying Pictures ....................................... 120
4.11 Summary .................................................. 127
Exercises ................................................ 127
Further Reading .......................................... 133
References ............................................... 133
Chapter 5 User-Defined Classes ............................... 135
5.1 Parts of a Class ......................................... 136
5.2 Fields ................................................... 136
5.3 Methods .................................................. 137
5.3.1 The Method Body ................................... 138
5.4 Methods with Parameters .................................. 141
5.4.1 Primitive Data Type Arguments ..................... 143
5.4.2 Reference Data Type Arguments ..................... 144
5.4.3 Returning a Reference Type from a Method .......... 149
5.5 Constructors ............................................. 153
5.5.1 Constructors without Parameters ................... 153
5.5.2 Constructors with Parameters ...................... 155
5.5.3 Default Constructors .............................. 156
5.5.4 Overloaded Constructors ........................... 156
5.6 The this Keyword ......................................... 158
5.7 Organizing. Java Files in Packages ....................... 160
5.7.1 Creating Packages ................................. 161
5.7.2 Naming Packages ................................... 162
5.7.3 Compiling. Java Files in Packages ................. 163
5.8 Access Modifiers ......................................... 164
5.8.1 pri vate Access Modifier .......................... 164
5.8.2 publ i с Access Modifier .......................... 165
5.8.3 Package-private Access Modifier (Default) ......... 167
5.8.4 protected Access Modifier ......................... 167
5.8.5 Access Modifier Symbols ........................... 168
5.9 Static Fields and Methods ................................ 169
5.9.1 A Useful Static Method: String. format ............ 171
5.9.2 The Math Class .................................... 174
5.10 The DrawingKit Class ..................................... 176
5.10.1 The Graphics2D Class .............................. 176
5.10.2 The JFrame Class .................................. 178
5.10.3 The JPanel Class .................................. 178
5.11 Documenting Programs ..................................... 182
5.11.1 Class Comments .................................... 183
5.11.2 Constructor Comments .............................. 183
5.11.3 Method Comments ................................... 184
5.11.4 Field Comments .................................... 184
5.11.5 The javadoc Command ............................... 185
5.12 The Java API ............................................. 186
5.13 Summary .................................................. 187
Exercises ................................................ 187
Further Reading .......................................... 193
References ............................................... 193
Chapter 6 Inheritance ........................................ 195
6.1 What Is Inheritance? ..................................... 196
6.2 When Is Inheritance Used? ................................ 196
6.3 The extends Keyword ...................................... 197
6.4 Types of Inheritance ..................................... 199
6.5 Access Modifiers ......................................... 200
6.6 Overriding Methods ....................................... 203
6.6.1 Polymorphism ...................................... 204
6.6.2 Access Modifiers for Overriding Methods ........... 206
6.6.3 Covariant Return Types ............................ 207
6.6.4 The Advantage of Overriding Methods ............... 211
6.6.5 The super Keyword ................................. 213
6.7 Overloaded Methods ....................................... 214
6.8 Constructor Chaining ..................................... 217
6.8.1 Java Automatically Calls the Superclass
Constructor ....................................... 217
6.8.2 Object: The Granddaddy of All Classes ............. 219
6.8.3 A Program with an Error ........................... 220
6.8.4 The Programmer Explicitly Calls a Constructor ..... 221
6.9 Abstract Classes ......................................... 224
6.9.1 The Calendar Class ................................ 231
6.10 The final Keyword ........................................ 238
6.10.1 Final Methods ..................................... 238
6.10.2 Final Classes ..................................... 239
6.10.3 Final Variables ................................... 239
6.11 Animation ................................................ 240
6.12 Advanced Graphics (Optional) ............................. 247
6.12.1 Graphics2D Transformations ........................ 249
6.13 Computers in Business: Credit Card Finance Charge
Calculator ............................................... 252
6.13.1 The BigDecimal Class .............................. 254
6.13.2 Program to Calculate Time to Pay Off Balance ...... 256
6.14 Summary .................................................. 262
Exercises ................................................ 263
Further Reading .......................................... 270
References ............................................... 270
Chapter 7 Arrays and Strings ................................. 273
7.1 What Is an Array? ........................................ 274
7.2 Creating Arrays .......................................... 275
7.3 Initializing Arrays ...................................... 276
7.4 Using Arrays ............................................. 279
7.4.1 The length Field .................................. 279
7.4.2 Enhanced for Statement ............................ 279
7.4.3 Arrays of Objects ................................. 280
7.5 Multidimensional Arrays .................................. 282
7.6 The Crystals Game ........................................ 285
7.6.1 The Crystals Game: Checking for Symmetric
Crystals .......................................... 286
7.7 Passing Arrays as Arguments to Methods ................... 288
7.8 The Arrays Class ......................................... 289
7.9 Enums .................................................... 290
7.9.1 The Enum Class .................................... 291
7.9.2 The values() Method ............................... 292
7.9.3 Adding Methods to an Enum ......................... 293
7.9.4 Adding Fields and Constructors to an Enum ......... 294
7.9.5 The Crystals Game: The Enum Di rection ............ 297
7.10 The ArrayLi st Class ..................................... 299
7.10.1 The Crystals Game: Checking for Holes ............. 300
7.10.2 The Crystals Game: Finding a Perfect Crystal ...... 304
7.11 Strings .................................................. 305
7.12 Methods in the String Class .............................. 307
7.12.1 Length of a String ................................ 307
7.12.2 Comparing Strings ................................. 308
7.12.3 Methods That Use/Retrieve an Index ................ 308
7.12.4 Methods to Retrieve a Substring or Its Index ...... 310
7.12.5 Concatenating Strings ............................. 311
7.12.6 Replacing/Removing Characters ..................... 312
7.12.7 Converting to Uppercase/Lowercase ................. 312
7.13 The Stri ngBuil der Class ................................ 312
7.14 Primitive Wrapper Classes ................................ 315
7.14.1 The BigDeci mal and Big Integer Classes ........... 317
7.15 Command-Line Arguments ................................... 318
7.16 The Crystals Game Revisited .............................. 320
7.17 Computers in Science: Human DNA .......................... 322
7.18 Summary .................................................. 329
Exercises ................................................ 329
Further Reading .......................................... 336
References ............................................... 336
Chapter 8 Interfaces and Nested Classes ...................... 339
8.1 What Are Interfaces? ..................................... 340
8.2 Defining an Interface .................................... 341
8.2.1 Palette: The Col orMi xabl e Interface ............ 342
8.3 Using an Interface ....................................... 343
8.3.1 Palette: The ColoredEllipse Class ................. 344
8.3.2 Palette: The Col oredRectangl e Class ............. 347
8.4 Differences Between Interfaces and Abstract Classes ...... 349
8.5 Implementing Multiple Interfaces ......................... 349
8.6 An Interface as a Data Type .............................. 350
8.6.1 The instance of Keyword ........................... 353
8.7 Extending an Interface ................................... 355
8.8 Predefined Interfaces .................................... 356
8.8.1 The Shape Interface ............................... 356
8.8.2 The Collection Interface .......................... 356
8.8.3 The EventListener Interface ....................... 357
8.8.4 The Runnable Interface ............................ 358
8.9 Nested Interfaces and Nested Classes ...................... 359
8.10 Static Nested Classes .................................... 360
8.11 Inner Classes ............................................ 363
8.11.1 Palette: The Palette Class ........................ 365
8.12 Anonymous Classes ........................................ 368
8.12.1 Extending an Existing Class ....................... 368
8.12.2 Implementing an Existing Interface ................ 370
8.13 Palette: A Preview of GUI Programming .................... 371
8.14 Summary .................................................. 376
Exercises ................................................ 376
Further Reading .......................................... 381
References ............................................... 381
Chapter 9 GUI Programming .................................... 383
9.1 Creating a Window ........................................ 384
9.2 The Swing Inheritance Tree ............................... 387
9.3 The JPanel Class ......................................... 388
9.3.1 Adding Panels to Another Panel .................... 389
9.3.2 Setting the Content Pane of a Window .............. 389
9.3.3 Adding Borders to the Panels ...................... 391
9.3.4 Adding Components to a Panel ...................... 392
9.3.5 Managing the Layout of Components in a Panel ...... 394
9.3.6 Changing Panel Size ............................... 394
9.3.7 Drawing on a Panel ................................ 394
9.3.8 Creating Custom Panels ............................ 398
9.3.9 Painting a Panel's Background ..................... 400
9.4 Layout Managers .......................................... 401
9.4.1 The Fl owLayout Class ............................. 401
9.4.2 The BorderLayout Class ............................ 403
9.4.3 The GridLayout Class .............................. 408
9.4.4 The BoxLayout Class ............................... 410
9.4.5 Insets ............................................ 412
9.5 Borders .................................................. 415
9.6 The Event Model .......................................... 418
9.6.1 Creating Event Sources ............................ 419
9.6.2 Event Objects ..................................... 419
9.6.3 Creating Event Handlers ........................... 420
9.6.4 Registering Event Handlers ........................ 423
9.6.5 Creating Event Handlers Using Anonymous Classes ... 427
9.7 GUI Components ........................................... 429
9.7.1 The JLabel Class .................................. 432
9.7.2 The JButton Class ................................. 432
9.7.3 The JTextField Class .............................. 436
9.7.4 The JFormattedTextField Class ..................... 439
9.7.5 The JPasswordField Class .......................... 441
9.7.6 The JTextArea Class ............................... 443
9.7.7 The JCheckBox Class ............................... 444
9.7.8 The JRadioButton and ButtonGroup Classes .......... 450
9.7.9 The JScrollPane Class ............................. 451
9.7.10 The JSlider Class ................................. 454
9.7.11 The JOption Pane Class ............................ 456
9.7.12 The JMenuBar, JMenu, and JMenuItem Classes ........ 459
9.7.13 The JFileChooser and File Classes ................. 461
9.8 Look and Feel Architecture ............................... 464
9.9 Summary .................................................. 465
Exercises ................................................ 465
Further Reading .......................................... 468
References ............................................... 468
Chapter 10 Exception Handling ................................. 471
10.1 The try and catch Blocks ................................. 472
10.2 The Exception Class ...................................... 475
10.3 Methods That Throw Exceptions ............................ 477
10.3.1 The Call Stack .................................... 480
10.4 A try Block with Multiple catch Blocks ................... 480
10.5 Run-Time Exceptions ...................................... 484
10.6 The finally Block ........................................ 486
10.7 Summary .................................................. 487
Exercises ................................................ 487
References ............................................... 491
Chapter 11 File I/O ........................................... 493
11.1 Overview of I/O Classes .................................. 494
11.2 Reading and Writing Text Files ........................... 494
11.2.1 Improving Read/Write Efficiency with Buffered
Reader/BufferedWriter ............................. 498
11.2.2 Using Class Scanner to Read from a File ........... 502
11.3 Reading and Writing Binary Files ......................... 505
11.3.1 Improving Read/Write Efficiency with
BufferedlnputStream/BufferedOutputStream .......... 505
11.4 Reading and Manipulating WAV Files ....................... 506
11.4.1 Soundwaves ........................................ 506
11.4.2 Digitizing Sound .................................. 508
11.4.3 Audio File Formats ................................ 510
11.4.4 The WAV File Format ............................... 511
11.4.5 The AudioMixer Class: Displaying a WAV File
Header ............................................ 512
11.4.6 The Audi oMi xer Class: Getting the Decimal
Value of a Byte Array ............................. 514
11.4.7 The AudioMixer Class: Printing a WAV Header
Summary ........................................... 517
11.4.8 The AudioMixer Class: Mixing Two WAV Files ........ 520
11.5 Accessing Files Randomly ................................. 524
11.5.1 The Audi oMi xer Class: Playing Audio in Reverse .. 529
11.6 Serialization ............................................ 533
11.6.1 Object Serialization .............................. 535
11.6.2 Object Deserialization ............................ 535
11.7 Summary .................................................. 542
Exercises ................................................ 542
Further Reading .......................................... 545
References ............................................... 545
Chapter 12 Generics and Collections ........................... 547
12.1 The Collections Framework ................................ 548
12.2 Generics ................................................. 549
12.3 The Set Interface and Its Implementations ................ 552
12.4 The Map Interface and Its Implementations ................ 556
12.4.1 Using Hash Maps ................................... 559
12.4.2 Multimap .......................................... 562
12.5 The Li st Interface and Its Implementations .............. 564
12.6 Iterators ................................................ 565
12.7 More on Generics ......................................... 567
12.7.1 Understanding Generic Classes ..................... 567
12.7.2 Using Wildcards ................................... 569
12.8 Performing Set Operations ................................ 571
12.9 Comparing Objects ........................................ 575
12.9.1 The Comparabl e Interface ......................... 578
12.9.2 The Comparator Interface .......................... 581
12.10 The Complete PhotoFinder Class .......................... 584
12.10.1 The PhotoFinder Class: Adding a GUI .............. 584
12.10.2 The PhotoFinder Class: Serialization ............. 586
12.11 Summary ................................................. 587
Exercises ................................................ 588
References ............................................... 591
Chapter 13 More on GUI Programming ............................ 593
13.1 Handling Mouse Events .................................... 594
13.1.1 The MouseAdapter and MouseMotionAdapter Classes ... 597
13.2 Handling Key Events ...................................... 599
13.2.1 The KeyListener Interface and KeyAdapter Class .... 600
13.3 The Swing Timer .......................................... 602
13.4 The JDialog Class ........................................ 604
13.5 A 2D Animation Game - Ball Game .......................... 608
13.5.1 Ball Game - Class Bat ............................. 609
13.5.2 Ball Game - Class Ball ............................ 609
13.5.3 Ball Game - Class BallGameRules ................... 610
13.5.4 Ball Game - Class BallGameDrawingPanel ............ 614
13.5.5 Ball Game - Class BallGameView .................... 615
13.5.6 Ball Game - Class BallGameController .............. 619
13.5.7 Compiling the Source Code and Running the Ball
Game .............................................. 621
13.6 Summary .................................................. 623
Exercises ................................................ 623
References ............................................... 625
Chapter 14 Multithreaded Programming .......................... 627
14.1 What Is Multithreading? .................................. 628
14.2 Playing Movies on the QuickTime Player ................... 629
14.2.1 Installing QuickTime and QTJ ...................... 629
14.2.2 The MoviePlayer Class ............................. 630
14.3 The Thread Class ......................................... 631
14.4 Simplified Model of a Computer ........................... 632
14.5 The sleep Method ......................................... 633
14.6 Creating a New Thread .................................... 636
14.7 Thread Names ............................................. 639
14.8 More on the Thread Class ................................. 641
14.9 Interrupting a Thread .................................... 642
14.10 The join Method ......................................... 645
14.11 Creating a New Thread Using the Runnable Interface ...... 647
14.12 Local Versus Instance Variables ......................... 649
14.13 Synchronizing Threads ................................... 651
14.14 Problems with Threads ................................... 654
14.15 Summary ................................................. 657
Exercises ................................................ 657
Further Reading .......................................... 658
References ............................................... 659
Index ......................................................... 661
|