java for everyone table of contents
DESCRIPTION
Java For Everyone Table of ContentsTRANSCRIPT
conTEnTS
xxi
PrEfacE vii
SPEcIaL fEaTUrES xxvi
InTroDUcTIon 1
1.1 computer Programs 2
1.2 The anatomy of a computer 3
1.3 The Java Programming Language 5
1.4 Becoming familiar with your Programming Environment 8
1.5 analyzing your first Program 12
1.6 Errors 15
1.7 Problem Solving: algorithm Design 16
fUnDamEnTaL DaTa TyPES 29
2.1 variables 30
2.2 arithmetic 41
2.3 Input and output 48
2.4 Problem Solving: first Do It By Hand 57
2.5 Strings 59
DEcISIonS 81
3.1 The if Statement 82
3.2 comparing numbers and Strings 88
3.3 multiple alternatives 96
3.4 nested Branches 100
3.5 Problem Solving: flowcharts 105
3.6 Problem Solving: Test cases 108
3.7 Boolean variables and operators 111
3.8 application: Input validation 116
LooPS 139
4.1 The while Loop 140
4.2 Problem Solving: Hand-Tracing 147
4.3 The for Loop 150
4.4 The do Loop 156
cHaPter 1
cHaPter 2
cHaPter 3
cHaPter 4
jfe2_fm.indd 21 10/30/11 2:47 PM
xxii contents
4.5 application: Processing Sentinel values 158
4.6 Problem Solving: Storyboards 162
4.7 common Loop algorithms 165
4.8 nested Loops 172
4.9 application: random numbers and Simulations 176
mETHoDS 201
5.1 methods as Black Boxes 202
5.2 Implementing methods 204
5.3 Parameter Passing 207
5.4 return values 210
5.5 methods Without return values 214
5.6 Problem Solving: reusable methods 215
5.7 Problem Solving: Stepwise refinement 218
5.8 variable Scope 225
5.9 recursive methods (optional) 228
arrayS anD array LISTS 249
6.1 arrays 250
6.2 The Enhanced for Loop 257
6.3 common array algorithms 258
6.4 Using arrays with methods 268
6.5 Problem Solving: adapting algorithms 272
6.6 Problem Solving: Discovering algorithms by manipulating
Physical objects 279
6.7 Two-Dimensional arrays 282
6.8 array Lists 289
InPUT/oUTPUT anD ExcEPTIon HanDLInG 317
7.1 reading and Writing Text files 318
7.2 Text Input and output 323
7.3 command Line arguments 330
7.4 Exception Handling 337
7.5 application: Handling Input Errors 347
oBJEcTS anD cLaSSES 361
8.1 object-oriented Programming 362
8.2 Implementing a Simple class 364
cHaPter 5
cHaPter 6
cHaPter 7
cHaPter 8
jfe2_fm.indd 22 10/30/11 2:47 PM
contents xxiii
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
8.3 Specifying the Public Interface of a class 367
8.4 Designing the Data representation 371
8.5 Implementing Instance methods 372
8.6 constructors 375
8.7 Testing a class 380
8.8 Problem Solving: Tracing objects 386
8.9 Problem Solving: Patterns for object Data 388
8.10 object references 395
8.11 Static variables and methods 400
InHErITancE anD InTErfacES 415
9.1 Inheritance Hierarchies 416
9.2 Implementing Subclasses 420
9.3 overriding methods 424
9.4 Polymorphism 430
9.5 object: The cosmic Superclass 441
9.6 Interface Types 448
GraPHIcaL USEr InTErfacES 465
10.1 frame Windows 466
10.2 Events and Event Handling 470
10.3 Processing Text Input 481
10.4 creating Drawings 487
aDvancED USEr InTErfacES (WEB onLy)
11.1 Layout management
11.2 choices
11.3 menus
11.4 Exploring the Swing Documentation
11.5 Using Timer Events for animations
11.6 mouse Events
oBJEcT-orIEnTED DESIGn (WEB onLy)
12.1 classes and Their responsibilities
12.2 relationships Between classes
12.3 application: Printing an Invoice
12.4 Packages
cHaPter 9
cHaPter 10
cHaPter 11
cHaPter 12
jfe2_fm.indd 23 10/30/11 2:47 PM
xxiv contents
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
rEcUrSIon (WEB onLy)
13.1 Triangle numbers revisited
13.2 Problem Solving: Thinking recursively
13.3 recursive Helper methods
13.4 The Efficiency of recursion
13.5 Permutations
13.6 mutual recursion
13.7 Backtracking
SorTInG anD SEarcHInG (WEB onLy)
14.1 Selection Sort
14.2 Profiling the Selection Sort algorithm
14.3 analyzing the Performance of the Selection Sort algorithm
14.4 merge Sort
14.5 analyzing the merge Sort algorithm
14.6 Searching
14.7 Problem Solving: Estimating the running Time of an algorithm
14.8 Sorting and Searching in the Java Library
THE Java coLLEcTIonS framEWorK (WEB onLy)
15.1 an overview of the collections framework
15.2 Linked Lists
15.3 Sets
15.4 maps
15.5 Stacks, Queues, and Priority Queues
15.6 Stack and Queue applications
aPPEnDIx a THE BaSIc LaTIn anD LaTIn-1 SUBSETS of UnIcoDE 507
aPPEnDIx B Java oPEraTor SUmmary 511
aPPEnDIx c Java rESErvED WorD SUmmary 513
aPPEnDIx D THE Java LIBrary 515
aPPEnDIx E Java SynTax SUmmary aPPEnDIx f HTmL SUmmary aPPEnDIx G TooL SUmmary aPPEnDIx H JavaDoc SUmmary
cHaPter 13
cHaPter 14
cHaPter 15
aPPendices
jfe2_fm.indd 24 10/30/11 2:47 PM
contents xxv
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
aPPEnDIx I nUmBEr SySTEmS aPPEnDIx J BIT anD SHIfT oPEraTIonS aPPEnDIx K UmL SUmmary aPPEnDIx L Java LanGUaGE coDInG GUIDELInES
GLoSSary 547
InDEx 559
crEDITS 585
SynTax BoxES
arrays 251array Lists 290assignment 34
cast 44 catching Exceptions 341comparisons 89constant Declaration 35constructor with Superclass Initializer 430constructors 376
for Statement 152
if Statement 84Input Statement 49Instance methods 373Instance variable Declaration 365Interface Types 449
Java Program 13
Static method Declaration 205Subclass Declaration 422
The Enhanced for Loop 258The finally clause 344The instanceof operator 445The throws clause 343Throwing an Exception 338Two-Dimensional array Declaration 283
while Statement 141
variable Declaration 31
aLPHabeticaL List OF
jfe2_fm.indd 25 10/30/11 2:47 PM
xxvi Special features
cHaPTEr
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
Programming Tips Special Topics random facts
Backup copies 11
The EnIac and the Dawn of computing 5
choose Descriptive variable names 38
Do not Use magic numbers 39
Spaces in Expressions 47
Use the aPI Documentation 53
numeric Types in Java 39
Big numbers 40
combining assignment and arithmetic 47
Instance methods and Static methods 64
Using Dialog Boxes for Input and output 65
The Pentium floating-Point Bug 48
International alphabets and Unicode 66
Brace Layout 86
always Use Braces 86
Tabs 87
avoid Duplication in Branches 88
Hand-Tracing 103
make a Schedule and make Time for Unexpected Problems 109
The conditional operator 87
Lexicographic ordering of Strings 92
The switch Statement 99
Enumeration Types 105
Logging 110
Short-circuit Evaluation of Boolean operators 114
De morgan’s Law 115
The Denver airport Luggage Handling System 95
artificial Intelligence 119
Use for Loops for TheirIntended Purpose only 155
choose Loop Bounds That match your Task 155
count Iterations 156
The Loop-and-a-Half Problem and the break Statement 160
redirection of Input and output 161
Drawing Graphical Shapes 179
The first Bug 146
Software Piracy 182
common Errors
How Tos and
Worked Examples
1 Introduction omitting Semicolons 14
misspelling Words 16
Describing an algorithm with Pseudocode 20
compiling and running a Program
Writing an algorithm for Tiling a floor
Dividing Household Expenses
2 fundamental Data Types
Using Undeclared or Uninitialized variables 37
overflow 38
roundoff Errors 38
Unintended Integer Division 46
Unbalanced Parentheses 46
Using Integer Division
carrying out computations 54
computing the cost of Stamps
computing Travel Time
computing Distances on Earth
3 Decisions a Semicolon after the if condition 86
Exact comparison of floating-Point numbers 91
Using == to compare Strings 92
The Dangling else Problem 104
combining multiple relational operators 113
confusing && and || conditions 114
Implementing an if Statement 93
Extracting the middle
computing the Plural of an English Word
The Genetic code
4 Loops Don’t Think “are We There yet?” 144
Infinite Loops 145
off-by-one Errors 145
Evaluating a cell Phone Plan
Writing a Loop 169
credit card Processing
manipulating the Pixels in an Image
Drawing a Spiral
jfe2_fm.indd 26 10/30/11 2:47 PM
Special features xxvii
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
Programming Tips Special Topics random facts
Backup copies 11 The EnIac and the Dawn of computing 5
choose Descriptive variable names 38
Do not Use magic numbers 39
Spaces in Expressions 47
Use the aPI Documentation 53
numeric Types in Java 39
Big numbers 40
combining assignment and arithmetic 47
Instance methods and Static methods 64
Using Dialog Boxes for Input and output 65
The Pentium floating-Point Bug 48
International alphabets and Unicode 66
Brace Layout 86
always Use Braces 86
Tabs 87
avoid Duplication in Branches 88
Hand-Tracing 103
make a Schedule and make Time for Unexpected Problems 109
The conditional operator 87
Lexicographic ordering of Strings 92
The switch Statement 99
Enumeration Types 105
Logging 110
Short-circuit Evaluation of Boolean operators 114
De morgan’s Law 115
The Denver airport Luggage Handling System 95
artificial Intelligence 119
Use for Loops for TheirIntended Purpose only 155
choose Loop Bounds That match your Task 155
count Iterations 156
The Loop-and-a-Half Problem and the break Statement 160
redirection of Input and output 161
Drawing Graphical Shapes 179
The first Bug 146
Software Piracy 182
common Errors
How Tos and
Worked Examples
1 Introduction omitting Semicolons 14
misspelling Words 16
Describing an algorithm with Pseudocode 20
compiling and running a Program
Writing an algorithm for Tiling a floor
Dividing Household Expenses
2 fundamental Data Types
Using Undeclared or Uninitialized variables 37
overflow 38
roundoff Errors 38
Unintended Integer Division 46
Unbalanced Parentheses 46
Using Integer Division
carrying out computations 54
computing the cost of Stamps
computing Travel Time
computing Distances on Earth
3 Decisions a Semicolon after the if condition 86
Exact comparison of floating-Point numbers 91
Using == to compare Strings 92
The Dangling else Problem 104
combining multiple relational operators 113
confusing && and || conditions 114
Implementing an if Statement 93
Extracting the middle
computing the Plural of an English Word
The Genetic code
4 Loops Don’t Think “are We There yet?” 144
Infinite Loops 145
off-by-one Errors 145
Evaluating a cell Phone Plan
Writing a Loop 169
credit card Processing
manipulating the Pixels in an Image
Drawing a Spiral
jfe2_fm.indd 27 10/30/11 2:47 PM
xxviii Special features
cHaPTEr
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
Programming Tips Special Topics random facts
method comments 207
Do not modify Parameter variables 209
Keep methods Short 223
Tracing methods 223
Stubs 224
The Explosive Growth of Personal computers 232
Use arrays for Sequences of related Items 256
reading Exception reports 274
Sorting with the Java Library 267
Binary Search 267
methods with a variable number of Parameters 272
Two-Dimensional arrays with variable row Lengths 288
multidimensional arrays 289
The Diamond Syntax in Java 7 299
an Early Internet Worm 256
Throw Early, catch Late 345
Do not Squelch Exceptions 345
Do not Use catch and finally in the Same try Statement 346
reading Web Pages 321
file Dialog Boxes 321
reading and Writing Binary Data 322
regular Expressions 330
automatic resource management in Java 7 346
Encryption algorithms 336
The ariane rocket Incident 347
all Data variables Should Be Private; most methods Should Be Public 374
The javadoc Utility 370
overloading 380
calling one constructor from another 399
Electronic voting machines 394
open Source and free Software 402
common Errors
How Tos and
Worked Examples
5 methods Trying to modify arguments 209
missing return value 212
Implementing a method 212
Generating random Passwords
calculating a course Grade
Debugging
Thinking recursively 231
fully Justified Text
6 arrays and array Lists Bounds Errors 255
Uninitialized arrays 255
Underestimating the Size of a Data Set 267
Length and Size 299
Working with arrays 275
rolling the Dice
removing Duplicates from an array
a World Population Table
Game of Life
7 Input/output and Exception Handling
Backslashes in file names 321
constructing a Scanner with a String 321
computing a Document’s readability
Processing Text files 333
analyzing Baby names
Detecting accounting fraud
8 objects and classes forgetting to Initialize object references in a constructor 378
Trying to call a constructor 379
Declaring a constructor as void 379
Implementing a class 382
Implementing a Bank account class
Paying off a Loan
modeling a robot Escaping from a maze
jfe2_fm.indd 28 10/30/11 2:47 PM
Special features xxix
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
Programming Tips Special Topics random facts
method comments 207
Do not modify Parameter variables 209
Keep methods Short 223
Tracing methods 223
Stubs 224
The Explosive Growth of Personal computers 232
Use arrays for Sequences of related Items 256
reading Exception reports 274
Sorting with the Java Library 267
Binary Search 267
methods with a variable number of Parameters 272
Two-Dimensional arrays with variable row Lengths 288
multidimensional arrays 289
The Diamond Syntax in Java 7 299
an Early Internet Worm 256
Throw Early, catch Late 345
Do not Squelch Exceptions 345
Do not Use catch and finally in the Same try Statement 346
reading Web Pages 321
file Dialog Boxes 321
reading and Writing Binary Data 322
regular Expressions 330
automatic resource management in Java 7 346
Encryption algorithms 336
The ariane rocket Incident 347
all Data variables Should Be Private; most methods Should Be Public 374
The javadoc Utility 370
overloading 380
calling one constructor from another 399
Electronic voting machines 394
open Source and free Software 402
common Errors
How Tos and
Worked Examples
5 methods Trying to modify arguments 209
missing return value 212
Implementing a method 212
Generating random Passwords
calculating a course Grade
Debugging
Thinking recursively 231
fully Justified Text
6 arrays and array Lists Bounds Errors 255
Uninitialized arrays 255
Underestimating the Size of a Data Set 267
Length and Size 299
Working with arrays 275
rolling the Dice
removing Duplicates from an array
a World Population Table
Game of Life
7 Input/output and Exception Handling
Backslashes in file names 321
constructing a Scanner with a String 321
computing a Document’s readability
Processing Text files 333
analyzing Baby names
Detecting accounting fraud
8 objects and classes forgetting to Initialize object references in a constructor 378
Trying to call a constructor 379
Declaring a constructor as void 379
Implementing a class 382
Implementing a Bank account class
Paying off a Loan
modeling a robot Escaping from a maze
jfe2_fm.indd 29 10/30/11 2:47 PM
xxx Special Features
CHAPTER
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
Common Errors
How Tos and
Worked Examples
9 Inheritance and Interfaces Replicating Instance Variables from the Superclass 423
Confusing Super- and Subclasses 424
Accidental Overloading 428
Forgetting to Use super When Invoking a Superclass Method 429
Don’t Use Type Tests 446
Forgetting to Declare Implement-ing Methods as Public 453
Developing an Inheritance Hierarchy 436
Implementing an Employee Hierarchy for Payroll Processing
Building a Discussion Board
Drawing Geometric Shapes
10Graphical User Interfaces
Modifying Parameter Types in the Implementing Method 478
Forgetting to Attach a Listener 478
Forgetting to Repaint 496
By Default, Components Have Zero Width and Height 497
Drawing Graphical Shapes 497
Coding a Bar Chart Creator
Solving Crossword Puzzles
11Advanced User Interfaces (WEB ONLY)
Laying Out a User Interface
Programming a Working Calculator
Adding Mouse and Keyboard Support to the Bar Chart Creator
Designing a Baby Naming Program
12Object-Oriented Design (WEB ONLY)
Using CRC Cards and UML Diagrams in Program Design
Simulating an Automatic Teller Machine
Programming Tips Special Topics Random Facts
Use a Single Class for Variation in Values, Inheritance for Variation in Behavior 420
Calling the Superclass Constructor 429
Dynamic Method Lookup and the Implicit Parameter 433
Abstract Classes 434
Final Methods and Classes 435
Protected Access 436
Inheritance and the toString Method 446
Inheritance and the equals Method 447
Constants in Interfaces 453
Function Objects 454
Don’t Use a Frame as a Listener 478
Adding the main Method to the Frame Class 470
Local Inner Classes 479
Anonymous Inner Classes 480
Use a GUI Builder Keyboard Events
Event Adapters
Make Parallel Arrays into Arrays of Objects
Consistency
Attributes and Methods in UML Diagrams
Multiplicities
Aggregation, Association, and Composition
jfe2_fm.indd 30 11/4/11 11:56 AM
Special features xxxi
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
common Errors
How Tos and
Worked Examples
9 Inheritance and Interfaces
replicating Instance variables from the Superclass 423
confusing Super- and Subclasses 424
accidental overloading 428
forgetting to Use super When Invoking a Superclass method 429
Don’t Use Type Tests 446
forgetting to Declare Implement-ing methods as Public 453
Developing an Inheritance Hierarchy 436
Implementing an Employee Hierarchy for Payroll Processing
Building a Discussion Board
Drawing Geometric Shapes
10 Graphical User Interfaces
modifying Parameter Types in the Implementing method 478
forgetting to attach a Listener 478
forgetting to repaint 496
By Default, components Have Zero Width and Height 497
Drawing Graphical Shapes 497
coding a Bar chart creator
Solving crossword Puzzles
11 advanced User Interfaces (WEB onLy)
Laying out a User Interface
Programming a Working calculator
adding mouse and Keyboard Support to the Bar chart creator
Designing a Baby naming Program
12 object-oriented Design (WEB onLy)
Using crc cards and UmL Diagrams in Program Design
Simulating an automatic Teller machine
Programming Tips Special Topics random facts
Use a Single class for variation in values, Inheritance for variation in Behavior 420
calling the Superclass constructor 429
Dynamic method Lookup and the Implicit Parameter 433
abstract classes 434
final methods and classes 435
Protected access 436
Inheritance and the toString method 446
Inheritance and the equals method 447
constants in Interfaces 453
function objects 454
Don’t Use a frame as a Listener 478
adding the main method to the frame class 470
Local Inner classes 479
anonymous Inner classes 480
Use a GUI Builder Keyboard Events
Event adapters
make Parallel arrays into arrays of objects
consistency
attributes and methods in UmL Diagrams
multiplicities
aggregation, association, and composition
jfe2_fm.indd 31 10/30/11 2:47 PM
xxxii Special Features
CHAPTER
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
Common Errors
How Tos and
Worked Examples
13Recursion (WEB ONLY)
Infinite Recursion Finding Files
Towers of Hanoi
14Sorting and Searching (WEB ONLY)
The compareTo Method Can Return Any Integer, Not Just –1, 0, and 1
Enhancing the Insertion Sort Algorithm
15The Java Collections Framework (WEB ONLY)
Choosing a Collection
Word Frequency
Simulating a Queue of Waiting Customers
Building a Table of Contents
Programming Tips Special Topics Random Facts
The Limits of Computation
Oh, Omega, and Theta
Insertion Sort
The Quicksort Algorithm
The Parameterized Comparable Interface
The Comparator Interface
The First Programmer
Use Interface References to Manipulate Data Structures
Standardization
Reverse Polish Notation
jfe2_fm.indd 32 11/4/11 11:57 AM
Special features xxxiii
Available online in WileyPLUS and at www.wiley.com/college/horstmann.
common Errors
How Tos and
Worked Examples
13 recursion (WEB onLy)
Infinite recursion finding files
Towers of Hanoi
14 Sorting and Searching (WEB onLy)
The compareTo method can return any Integer, not Just –1, 0, and 1
Enhancing the Insertion Sort algorithm
15 The Java collections framework (WEB onLy)
choosing a collection
Word frequency
Simulating a Queue of Waiting customers
Building a Table of contents
Programming Tips Special Topics random facts
The Limits of computation
oh, omega, and Theta
Insertion Sort
The Quicksort algorithm
The Parameterized Comparable Interface
The Comparator Interface
The first Programmer
Use Interface references to manipulate Data Structures
Standardization
reverse Polish notation
jfe2_fm.indd 33 10/30/11 2:47 PM
jfe2_fm.indd 34 10/30/11 2:47 PM