100% found this document useful (1 vote)
11 views40 pages

Download (eBook PDF) Starting Out with Java: From Control Structures through Data Structures 3rd Edition ebook All Chapters PDF

The document provides links to various eBooks on programming, specifically focusing on Java and C++. It includes titles such as 'Starting Out with Java' and 'Data Structures and Abstractions with Java' in multiple editions. Users can download these eBooks in different formats from ebookluna.com.

Uploaded by

tenchayosha77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
11 views40 pages

Download (eBook PDF) Starting Out with Java: From Control Structures through Data Structures 3rd Edition ebook All Chapters PDF

The document provides links to various eBooks on programming, specifically focusing on Java and C++. It includes titles such as 'Starting Out with Java' and 'Data Structures and Abstractions with Java' in multiple editions. Users can download these eBooks in different formats from ebookluna.com.

Uploaded by

tenchayosha77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Get the full ebook with Bonus Features for a Better Reading Experience on ebookluna.

com

(eBook PDF) Starting Out with Java: From Control


Structures through Data Structures 3rd Edition

https://ebookluna.com/product/ebook-pdf-starting-out-with-
java-from-control-structures-through-data-structures-3rd-
edition/

OR CLICK HERE

DOWLOAD NOW

Download more ebook instantly today at https://ebookluna.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

(eBook PDF) Starting Out with Java: From Control


Structures through Data Structures 4th Edition

https://ebookluna.com/product/ebook-pdf-starting-out-with-java-from-
control-structures-through-data-structures-4th-edition/

ebookluna.com

(eBook PDF) Starting Out with Java: From Control


Structures through Objects, 7th Edition

https://ebookluna.com/product/ebook-pdf-starting-out-with-java-from-
control-structures-through-objects-7th-edition/

ebookluna.com

(eBook PDF) Starting Out with C++: From Control Structures


through Objects 8th Edition

https://ebookluna.com/product/ebook-pdf-starting-out-with-c-from-
control-structures-through-objects-8th-edition/

ebookluna.com

(eBook PDF) Starting Out with C++: From Control Structures


through Objects, Brief Version 8th Edition

https://ebookluna.com/product/ebook-pdf-starting-out-with-c-from-
control-structures-through-objects-brief-version-8th-edition/

ebookluna.com
(eBook PDF) Starting Out with C++ from Control Structures
to Objects 9th Edition

https://ebookluna.com/product/ebook-pdf-starting-out-with-c-from-
control-structures-to-objects-9th-edition/

ebookluna.com

(eBook PDF) Data Structures and Abstractions with Java 4th


Edition

https://ebookluna.com/product/ebook-pdf-data-structures-and-
abstractions-with-java-4th-edition/

ebookluna.com

Data Structures and Abstractions with Java 5th Edition


(eBook PDF)

https://ebookluna.com/product/data-structures-and-abstractions-with-
java-5th-edition-ebook-pdf/

ebookluna.com

(eBook PDF) Data Structures and Abstractions with Java 4th


Global Edition

https://ebookluna.com/product/ebook-pdf-data-structures-and-
abstractions-with-java-4th-global-edition/

ebookluna.com

(eBook PDF) Starting Out with Java: Early Objects 5th


Edition

https://ebookluna.com/product/ebook-pdf-starting-out-with-java-early-
objects-5th-edition/

ebookluna.com
Contents vii

Having Multiple Conditionally Executed Statements . . . . . . . . . . . . . . . . 119


Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Comparing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
3.2 The if-else Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
3.3 Nested if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
3.4 The if-else-if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
3.5 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
The Precedence of Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Checking Numeric Ranges with Logical Operators . . . . . . . . . . . . . . . . . 144
3.6 Comparing String Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Ignoring Case in String Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
3.7 More about Variable Declaration and Scope . . . . . . . . . . . . . . . . . . . 151
3.8 The Conditional Operator (Optional) . . . . . . . . . . . . . . . . . . . . . . . . 152
3.9 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
3.10 Displaying Formatted Output with System.out.printf
and String.format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Format Specifier Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Specifying a Minimum Field Width . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Formatting String Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
The String.format Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
3.11 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Review Questions and Exercises   179
Programming Challenges   184

Chapter 4 Loops and Files   189


4.1 The Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . 189
The Difference between Postfix and Prefix Modes . . . . . . . . . . . . . . . . . . 192
4.2 The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
The while Loop Is a Pretest Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Infinite Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Don’t Forget the Braces with a Block of Statements . . . . . . . . . . . . . . . . 197
Programming Style and the while Loop . . . . . . . . . . . . . . . . . . . . . . . . 198
4.3 Using the while Loop for Input Validation . . . . . . . . . . . . . . . . . . . . 200
4.4 The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
4.5 The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
The for Loop Is a Pretest Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Avoid Modifying the Control Variable in the Body
of the for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Other Forms of the Update Expression . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Declaring a Variable in the for Loop’s Initialization Expression . . . . . . . 211
Creating a User Controlled for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Using Multiple Statements in the Initialization
and Update Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
viii Contents

4.6 Running Totals and Sentinel Values . . . . . . . . . . . . . . . . . . . . . . . . . . 216


Using a Sentinel Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
4.7 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
4.8 The break and continue Statements (Optional) . . . . . . . . . . . . . . . . 229
4.9 Deciding Which Loop to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
4.10 Introduction to File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . 230
Using the PrintWriter Class to Write Data to a File . . . . . . . . . . . . . . . 230
Appending Data to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Specifying the File Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Reading Data from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Reading Lines from a File with the nextLine Method . . . . . . . . . . . . . . . 238
Adding a throws Clause to the Method Header . . . . . . . . . . . . . . . . . . . 241
Checking for a File’s Existence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
4.11 Generating Random Numbers with the Random Class . . . . . . . . . . . . 249
4.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Review Questions and Exercises   256
Programming Challenges   262

Chapter 5 Methods   269


5.1 Introduction to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
void Methods and Value-Returning Methods . . . . . . . . . . . . . . . . . . . . 270
Defining a void Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Calling a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Hierarchical Method Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Using Documentation Comments with Methods . . . . . . . . . . . . . . . . . . 278
5.2 Passing Arguments to a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Argument and Parameter Data Type Compatibility . . . . . . . . . . . . . . . . 281
Parameter Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Passing Multiple Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Arguments Are Passed by Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Passing Object References to a Method . . . . . . . . . . . . . . . . . . . . . . . . . 285
Using the @param Tag in Documentation Comments . . . . . . . . . . . . . . . 288
5.3 More about Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Local Variable Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Initializing Local Variables with Parameter Values . . . . . . . . . . . . . . . . . 292
5.4 Returning a Value from a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Defining a Value-Returning Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Calling a Value-Returning Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Using the @return Tag in Documentation Comments . . . . . . . . . . . . . . 296
Returning a boolean Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Returning a Reference to an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
5.5 Problem Solving with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Calling Methods That Throw Exceptions . . . . . . . . . . . . . . . . . . . . . . . . 306
5.6 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Review Questions and Exercises   307
Programming Challenges   312
Contents ix

Chapter 6 A First Look at Classes   319


6.1 Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Classes: Where Objects Come From . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Classes in the Java API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Primitive Variables vs. Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
6.2 Writing a Simple Class, Step by Step . . . . . . . . . . . . . . . . . . . . . . . . . 326
Accessor and Mutator Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
The Importance of Data Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Avoiding Stale Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Showing Access Specification in UML Diagrams . . . . . . . . . . . . . . . . . . . 341
Data Type and Parameter Notation in UML Diagrams . . . . . . . . . . . . . .341
Layout of Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
6.3 Instance Fields and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
6.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Showing Constructors in a UML Diagram . . . . . . . . . . . . . . . . . . . . . . . 350
Uninitialized Local Reference Variables . . . . . . . . . . . . . . . . . . . . . . . . . 350
The Default Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Writing Your Own No-Arg Constructor . . . . . . . . . . . . . . . . . . . . . . . . . 351
The String Class Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
6.5 Passing Objects as Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
6.6 Overloading Methods and Constructors . . . . . . . . . . . . . . . . . . . . . . 372
The BankAccount Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Overloaded Methods Make Classes More Useful . . . . . . . . . . . . . . . . . . 380
6.7 Scope of Instance Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Shadowing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
6.8 Packages and import Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Explicit and Wildcard import Statements . . . . . . . . . . . . . . . . . . . . . . . . 382
The java.lang Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Other API Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
6.9 Focus on Object-Oriented Design: Finding the Classes
and Their Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Finding the Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Identifying a Class’s Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
This Is Only the Beginning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
6.10 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Review Questions and Exercises   391
Programming Challenges   396

Chapter 7 Arrays and the ArrayList Class   405


7.1 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Accessing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Inputting and Outputting Array Contents . . . . . . . . . . . . . . . . . . . . . . . 408
Java Performs Bounds Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Watch Out for Off-by-One Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Array Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Alternate Array Declaration Notation . . . . . . . . . . . . . . . . . . . . . . . . . . 414
x Contents

7.2 Processing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415


Array Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
The Enhanced for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Letting the User Specify an Array’s Size . . . . . . . . . . . . . . . . . . . . . . . . . 419
Reassigning Array Reference Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Copying Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
7.3 Passing Arrays as Arguments to Methods . . . . . . . . . . . . . . . . . . . . . 424
7.4 Some Useful Array Algorithms and Operations . . . . . . . . . . . . . . . . . 428
Comparing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Summing the Values in a Numeric Array . . . . . . . . . . . . . . . . . . . . . . . . 429
Getting the Average of the Values in a Numeric Array . . . . . . . . . . . . . . 430
Finding the Highest and Lowest Values in a Numeric Array . . . . . . . . . . 430
The SalesData Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Partially Filled Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Working with Arrays and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
7.5 Returning Arrays from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
7.6 String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Calling String Methods from an Array Element . . . . . . . . . . . . . . . . . . . 445
7.7 Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
7.8 The Sequential Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
7.9 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Initializing a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
The length Field in a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . . 457
Displaying All the Elements of a Two-Dimensional Array . . . . . . . . . . . . 459
Summing All the Elements of a Two-Dimensional Array . . . . . . . . . . . . . 459
Summing the Rows of a Two-Dimensional Array . . . . . . . . . . . . . . . . . . 460
Summing the Columns of a Two-Dimensional Array . . . . . . . . . . . . . . . 460
Passing Two-Dimensional Arrays to Methods . . . . . . . . . . . . . . . . . . . . .461
Ragged Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
7.10 Arrays with Three or More Dimensions . . . . . . . . . . . . . . . . . . . . . . . 464
7.11 The Selection Sort and the Binary Search Algorithms . . . . . . . . . . . . . 465
The Selection Sort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
The Binary Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
7.12 Command-Line Arguments and Variable-Length Argument Lists . . . . 470
Command-Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Variable-Length Argument Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
7.13 The ArrayList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Creating and Using an ArrayList Object . . . . . . . . . . . . . . . . . . . . . . . 475
Using the Enhanced for Loop with an ArrayList . . . . . . . . . . . . . . . . . 476
The ArrayList Class’s toString method . . . . . . . . . . . . . . . . . . . . . . . . 477
Removing an Item from an ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . .478
Inserting an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Replacing an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Capacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Using the Diamond Operator for Type Inference (Java 7) . . . . . . . . . . . . 482
7.14 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Contents xi

Review Questions and Exercises   483


Programming Challenges   488

Chapter 8 A Second Look at Classes and Objects   495


8.1 Static Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
A Quick Review of Instance Fields and Instance Methods . . . . . . . . . . . . 495
Static Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Static Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
8.2 Passing Objects as Arguments to Methods . . . . . . . . . . . . . . . . . . . . 502
8.3 Returning Objects from Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
8.4 The toString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
8.5 Writing an equals Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
8.6 Methods That Copy Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Copy Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
8.7 Aggregation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Aggregation in UML Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Security Issues with Aggregate Classes . . . . . . . . . . . . . . . . . . . . . . . . . 525
Avoid Using null References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
8.8 The this Reference Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Using this to Overcome Shadowing . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Using this to Call an Overloaded Constructor
from Another Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
8.9 Enumerated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Enumerated Types Are Specialized Classes . . . . . . . . . . . . . . . . . . . . . . . 534
Switching On an Enumerated Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
8.10 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
The finalize Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
8.11 Focus on Object-Oriented Design: Class Collaboration . . . . . . . . . . . 544
Determining Class Collaborations with CRC Cards . . . . . . . . . . . . . . . . . 547
8.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Review Questions and Exercises   549
Programming Challenges   553

Chapter 9 Text Processing and More


about Wrapper Classes   559
9.1 Introduction to Wrapper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
9.2 Character Testing and Conversion with the Character Class . . . . . . . 560
Character Case Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
9.3 More String Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Searching for Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Extracting Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Methods That Return a Modified String . . . . . . . . . . . . . . . . . . . . . . . . 579
The Static valueOf Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
xii Contents

9.4 The StringBuilder Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582


The StringBuilder Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Other StringBuilder Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
The toString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
9.5 Tokenizing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
9.6 Wrapper Classes for the Numeric Data Types . . . . . . . . . . . . . . . . . . 597
The Static toString Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
The toBinaryString, toHexString, and toOctalString Methods . . . . . 598
The MIN_VALUE and MAX_VALUE Constants . . . . . . . . . . . . . . . . . . . . . . . 598
Autoboxing and Unboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
9.7 Focus on Problem Solving: The TestScoreReader Class . . . . . . . . . . . 600
9.8 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604
Review Questions and Exercises   605
Programming Challenges   608

Chapter 10 Inheritance   613


10.1 What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Generalization and Specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Inheritance and the “Is a” Relationship . . . . . . . . . . . . . . . . . . . . . . . . . 614
Inheritance in UML Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
The Superclass’s Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Inheritance Does Not Work in Reverse . . . . . . . . . . . . . . . . . . . . . . . . . . 625
10.2 Calling the Superclass Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . 626
When the Superclass Has No Default
or No-Arg Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Summary of Constructor Issues in Inheritance . . . . . . . . . . . . . . . . . . . . 633
10.3 Overriding Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Overloading versus Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Preventing a Method from Being Overridden . . . . . . . . . . . . . . . . . . . . . 642
10.4 Protected Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Package Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
10.5 Chains of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
Class Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
10.6 The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
10.7 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
Polymorphism and Dynamic Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
The “Is-a” Relationship Does Not Work in Reverse . . . . . . . . . . . . . . . . . 660
The instanceof Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661
10.8 Abstract Classes and Abstract Methods . . . . . . . . . . . . . . . . . . . . . . . 662
Abstract Classes in UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
10.9 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
An Interface is a Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
Fields in Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Implementing Multiple Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Interfaces in UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Contents xiii

Default Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676


Polymorphism and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
10.10 Anonymous Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
10.11 Functional Interfaces and Lambda Expressions . . . . . . . . . . . . . . . . . 686
10.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Review Questions and Exercises   692
Programming Challenges   698

Chapter 11 Exceptions and Advanced File I/O   703


11.1 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Handling an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
Retrieving the Default Error Message . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
Polymorphic References to Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . 712
Using Multiple catch Clauses to Handle Multiple Exceptions . . . . . . . . . 712
The finally Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
The Stack Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
Handling Multiple Exceptions with One catch Clause (Java 7) . . . . . . . . 723
When an Exception Is Not Caught . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
Checked and Unchecked Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
11.2 Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727
Creating Your Own Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 730
Using the @exception Tag in Documentation Comments . . . . . . . . . . . . 733
11.3 Advanced Topics: Binary Files, Random Access Files,
and Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
Random Access Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746
Serializing Aggregate Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 750
11.4 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Review Questions and Exercises   751
Programming Challenges   757

Chapter 12 A First Look at GUI Applications   761


12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761
The JFC, AWT, and Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
Event-Driven Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
The javax.swing and java.awt Packages . . . . . . . . . . . . . . . . . . . . . . . 764
12.2 Creating Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
Using Inheritance to Extend the JFrame Class . . . . . . . . . . . . . . . . . . . . 767
Equipping GUI Classes with a main Method . . . . . . . . . . . . . . . . . . . . . . 769
Adding Components to a Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
Handling Events with Action Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . 777
xiv Contents

Writing an Event Listener for the KiloConverter Class . . . . . . . . . . . . . . 779


Background and Foreground Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
The ActionEvent Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788
12.3 Layout Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
Adding a Layout Manager to a Container . . . . . . . . . . . . . . . . . . . . . . . 794
The FlowLayout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
The BorderLayout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
The GridLayout Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
12.4 Radio Buttons and Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
12.5 Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821
12.6 Focus on Problem Solving: Extending Classes from JPanel . . . . . . . . 824
The Brandi’s Bagel House Application . . . . . . . . . . . . . . . . . . . . . . . . . . 824
The GreetingPanel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
The BagelPanel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826
The ToppingPanel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828
The CoffeePanel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832
12.7 Splash Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
12.8 Using Console Output to Debug a GUI Application . . . . . . . . . . . . . . 837
12.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842
Review Questions and Exercises   842
Programming Challenges   845

Chapter 13 Advanced GUI Applications   849


13.1 The Swing and AWT Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . 849
13.2 Read-Only Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850
13.3 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852
Selection Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852
Responding to List Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
Retrieving the Selected Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854
Placing a Border around a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
Adding a Scroll Bar to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
Adding Items to an Existing JList Component . . . . . . . . . . . . . . . . . . . 863
Multiple Selection Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
13.4 Combo Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868
Retrieving the Selected Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869
13.5 Displaying Images in Labels and Buttons . . . . . . . . . . . . . . . . . . . . . . 874
13.6 Mnemonics and Tool Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
Mnemonics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
Tool Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
13.7 File Choosers and Color Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
File Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883
Color Choosers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
Contents xv

13.8 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886


13.9 More about Text Components: Text Areas and Fonts . . . . . . . . . . . . . 895
Text Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895
Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
13.10 Sliders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
13.11 Look and Feel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904
13.12 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906
Review Questions and Exercises   907
Programming Challenges   912

Chapter 14 Applets and More   917


14.1 Introduction to Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917
14.2 A Brief Introduction to HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
Hypertext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
Markup Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
Document Structure Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
Text Formatting Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 922
Creating Breaks in Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
Inserting Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
14.3 Creating Applets with Swing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
Running an Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
Handling Events in an Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932
14.4 Using AWT for Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
14.5 Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
The XY Coordinate System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
Graphics Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
The repaint Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956
Drawing on Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
14.6 Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
Handling Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
14.7 Timer Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973
14.8 Playing Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
Using an AudioClip Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
Playing Audio in an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981
14.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 982
Review Questions and Exercises   982
Programming Challenges   988

Chapter 15 Creating GUI Applications with JavaFX   991


15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991
Event-Driven Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
15.2 Stages and Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
15.3 Scene Graphs and Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 995
Creating a Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
xvi Contents

15.4 Panes and Component Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 999


VBox and HBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 999
Margin and Padding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1001
Nested Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003
15.5 Events and Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1004
Passing Information to Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . 1006
Using Inner Classes for Event Handling . . . . . . . . . . . . . . . . . . . . . . . . 1009
Using Anonymous Local Inner Classes for Event Handling . . . . . . . . . . 1010
Using Lambda Expressions for Event Handling . . . . . . . . . . . . . . . . . . . 1011
15.6 Determining the Target of an Event . . . . . . . . . . . . . . . . . . . . . . . . . 1012
15.7 Radio Buttons and Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . 1015
Programming with Check Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019
Responding to Radio Button and Check Box events . . . . . . . . . . . . . . . 1020
15.8 Displaying Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1020
15.9 Timeline Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1022
15.10 Text Input Controls, Panes, and CSS Styling . . . . . . . . . . . . . . . . . . 1026
Text Input Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026
Using CSS to Style Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028
Preferred Width and Height of Components . . . . . . . . . . . . . . . . . . . . . 1029
TilePane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1029
BorderPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031
GridPane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033
Additional Panes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1034
15.11 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1035
Review Questions and Exercises   1035
Programming Challenges   1038

Chapter 16 Recursion   1041


16.1 Introduction to Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1041
16.2 Solving Problems with Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 1044
Direct and Indirect Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048
16.3 Examples of Recursive Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049
Summing a Range of Array Elements with Recursion . . . . . . . . . . . . . . 1049
Drawing Concentric Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1050
The Fibonacci Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052
Finding the Greatest Common Divisor . . . . . . . . . . . . . . . . . . . . . . . . . 1054
16.4 A Recursive Binary Search Method . . . . . . . . . . . . . . . . . . . . . . . . . . 1055
16.5 The Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058
16.6 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063
Review Questions and Exercises   1063
Programming Challenges   1066

Chapter 17 Sorting, Searching, and Algorithm Analysis   1069


17.1 Introduction to Sorting Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
The Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
Contents xvii

Using the Bubble Sort to Sort Objects . . . . . . . . . . . . . . . . . . . . . . . . . 1075


The Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1078
Using the Selection Sort to Sort Objects . . . . . . . . . . . . . . . . . . . . . . . . 1082
The Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1084
Using the Insertion Sort to Sort Objects . . . . . . . . . . . . . . . . . . . . . . . . 1089
The Quicksort Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1090
Using Quicksort to Sort Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1094
17.2 Introduction to Search Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . 1094
The Sequential Search Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1094
Average Case Analysis of the Sequential Search . . . . . . . . . . . . . . . . . . 1097
The Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1097
Efficiency of the Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101
A Recursive Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101
17.3  Analysis of Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1105
Computational Problems and Basic Steps . . . . . . . . . . . . . . . . . . . . . . 1106
Complexity of Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1107
Worst Case Complexity of Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 1108
Average Case Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1109
Asymptotic Complexity and the Big O Notation . . . . . . . . . . . . . . . . . . 1110
17.4 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1114
Review Questions and Exercises   1115
Programming Challenges   1118

Chapter 18 Generics   1121


18.1 Introduction to Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1121
Using the Diamond Operator for Type Inference . . . . . . . . . . . . . . . . . 1124
18.2 Writing a Generic Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1124
Only Reference Types Can Be Passed to Type Parameters . . . . . . . . . . . 1129
Instantiating a Generic Class without Specifying a Type Argument . . . . 1130
Commonly Used Type Parameter Names . . . . . . . . . . . . . . . . . . . . . . . 1132
18.3 Passing Objects of a Generic Class to a Method . . . . . . . . . . . . . . . 1132
Constraining a Type Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1134
Defining a Type Parameter in a Method Header . . . . . . . . . . . . . . . . . 1136
The extends Key Word Constrains a Type to an Upper Bound . . . . . . . 1137
The super Key Word Constrains a Type to a Lower Bound . . . . . . . . . . 1137
18.4 Writing Generic Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1138
18.5 Constraining a Type Parameter in a Generic Class . . . . . . . . . . . . . . 1139
18.6 Inheritance and Generic Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 1142
18.7 Defining Multiple Type Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 1146
18.8 Generics and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1149
Constraining a Type Parameter to a Type That Implements
an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1150
18.9 Erasure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1155
18.10 Restrictions on the Use of Generic Types . . . . . . . . . . . . . . . . . . . . . 1158
xviii Contents

18.11 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1160


Review Questions and Exercises   1161
Programming Challenges   1164

Chapter 19 Collections and the Stream API   1167


19.1 Introduction to the Java Collections Framework . . . . . . . . . . . . . . . 1167
Lists, Sets, and Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1167
Java Functional Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1168
JCF Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1168
Iterating a Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1169
The Iterable Interface and the forEach method . . . . . . . . . . . . . . . . 1169
Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1170
The Enhanced For Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173
The Collection Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173
19.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175
The List Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1176
The ArrayList and LinkedList Classes . . . . . . . . . . . . . . . . . . . . . . . 1177
Creating and Initializing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1178
List Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1179
Methods of the LinkedList Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1182
Using an Interface Variable to Reference a Collection Object . . . . . . . 1183
19.3 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
The Set Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
The HashSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185
Implementing Your Own hashCode Method . . . . . . . . . . . . . . . . . . . . . 1190
The LinkedHashSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1194
The SortedSet Interface and the TreeSet Class . . . . . . . . . . . . . . . . . 1195
Using a Comparator to Compare Objects . . . . . . . . . . . . . . . . . . . . . . 1198
19.4 Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1200
The HashMap Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1202
The LinkedHashMap Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1208
The SortedMap Interface and the TreeMap Class . . . . . . . . . . . . . . . . . 1210
19.5 The Collections Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1211
19.6 The Stream API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1213
The Stream Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1214
Creating a Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1214
Terminal Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1215
Intermediate Operations on Streams . . . . . . . . . . . . . . . . . . . . . . . . . . 1219
Intermediate Stream Operations are Lazy . . . . . . . . . . . . . . . . . . . . . . 1221
The Stream map Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1223
The Stream reduce Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1224
Stream Collectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1226
19.7 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1231
Review Questions and Exercises   1232
Programming Challenges   1235
Contents xix

Chapter 20 Linked Lists   1237


20.1 Introduction to Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1237
Creating Lists and Adding Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1238
Removing a Node . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1240
Traversing Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1241
20.2 Operations on Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1244
The isEmpty Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1244
The size Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1245
The add Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1245
The remove Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1246
The toString Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1246
Graphical Interfaces for the Linked List Program . . . . . . . . . . . . . . . . . 1252
20.3 Doubly-Linked and Circularly-Linked Lists . . . . . . . . . . . . . . . . . . . . 1259
Adding a Node to a Doubly-Linked List . . . . . . . . . . . . . . . . . . . . . . . . 1260
Removing a Node from a Doubly-Linked List . . . . . . . . . . . . . . . . . . . . 1261
Circularly-Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1268
20.4 Recursion on Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1268
A Recursive Implementation of size . . . . . . . . . . . . . . . . . . . . . . . . . . 1269
A Recursive Implementation of the add(String e,
Node list) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1270
A Recursive Implementation of add(int index, String e,
Node list) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1270
A Recursive Implementation of the remove Methods . . . . . . . . . . . . . . .1271
20.5 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1283
Review Questions and Exercises   1283
Programming Challenges   1286

Chapter 21 Stacks and Queues   1289


21.1 Stacks and Their Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1289
Examples and Applications of Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . 1289
Stack Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1290
Stacks of Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1291
21.2 Array Implementation of Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1292
The Stack Push Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1293
The Stack empty Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1293
The Stack peek and pop Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1293
Stacks of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1297
21.3 Linked Implementation of Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . 1298
Implementation of Stack Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1299
21.4 Queues and Their Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1303
21.5 Array Implementation of Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . 1303
GUI Front Ends for the Queue Demo Program . . . . . . . . . . . . . . . . . . . 1310
Swing Version of the GUI Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . 1311
JavaFX Version of the GUI Front End . . . . . . . . . . . . . . . . . . . . . . . . . . 1314
xx Contents

21.6 Linked List Implementation of Queues . . . . . . . . . . . . . . . . . . . . . . . 1317


Queue Initialization and Enqueuing of Items . . . . . . . . . . . . . . . . . . . . 1317
Dequeuing Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1318
21.7 Generic Implementation of Stacks and Queues . . . . . . . . . . . . . . . . 1322
21.8 Queues and Breadth-First Search . . . . . . . . . . . . . . . . . . . . . . . . . . . 1325
21.9 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1328
Review Questions and Exercises   1329
Programming Challenges   1331

Chapter 22 Binary Trees, AVL Trees, and Priority Queues   1335


22.1 Binary Trees and Their Applications . . . . . . . . . . . . . . . . . . . . . . . . . 1335
Binary Tree Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1336
Applications of Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1337
Representing Nodes of Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . 1337
Traversing a Binary Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1338
Graphical Display of Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1341
Graphical Display of Binary Trees Using Swing . . . . . . . . . . . . . . . . . . 1342
Graphical Display of Binary Trees Using JavaFX . . . . . . . . . . . . . . . . . 1344
Class Implementation of Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . 1347
22.2 Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1347
Adding a Value to a Binary Search Tree . . . . . . . . . . . . . . . . . . . . . . . . 1349
Removing a Value from a Binary Search Tree . . . . . . . . . . . . . . . . . . . . 1349
Implementation of Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . . 1353
Graphical Display of Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . 1358
A Program for Testing the Binary Search Tree Class . . . . . . . . . . . . . . . 1360
22.3 AVL Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1368
Adding New Elements to AVL Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . 1369
Implementation of AVL Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1372
22.4 Priority Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1380
Applications of Priority Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1382
Using Comparators with Priority Queues . . . . . . . . . . . . . . . . . . . . . . . 1383
Analysis of Heapsort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1385
Implementation of Priority Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . 1385
Binary Trees with the Heap Order Property . . . . . . . . . . . . . . . . . . . . . 1385
Complete Binary Trees and Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1386
The Depth of Complete Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . 1387
Storing a Complete Binary Tree in an Array . . . . . . . . . . . . . . . . . . . . . 1387
Adding an Item to a Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1388
Removing the Minimum Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1389
22.5 Common Errors to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1397
Review Questions and Exercises   1397
Programming Challenges   1401

Index    1501
Exploring the Variety of Random
Documents with Different Content
[264]

Op. cit. 1895, p. 399.

[265]

P. ent. Soc. London, 1880, p. iii.

[266]

Ent. Monthly Mag. xiii. 1877, p. 231.

[267]

Entomologist, xxiii. 1890, p. 92.

[268]

Mem. Ac. Washington, vii. 1895, 290 pp., 49 plates.

[269]

Tr. ent. Soc. London, 1878, p. 121, Pl. v.

[270]

Op. cit. 1889, pp. 1-40, 6 plates.

[271]

Walsingham, Op. cit., 1889. c. p. 21.

[272]

Ent. Zeit. Stettin, lvi. 1895, p. 233.

[273]

Op. cit. li. 1890, p. 261.

[274]

Ent. Zeit. Stettin, li. 1890, p. 263.


[275]

For details as to habits, etc., see Rambur, Ann. Soc. ent. France,
v. 1836, p. 577; and Graslin, op. cit. xix. 1850, p. 396.

[276]

Monograph of European Psychidae, Ann. Soc. ent. Belgique, xxv.


1881, p. 29, etc.

[277]

Heylaerts, op. cit. p. 55.

[278]

Zool. Anz. xx. 1897, p. 473. This is probably Apterona crenulella,


or one of its varieties.

[279]

Bull. U.S. Dep. Agric. Ent. x. 1887, p. 22.

[280]

Ann. New York Ac. viii. 1893, p. 54.

[281]

Kalender, Ent. Zeit. Stettin, xxxv. 1874, p. 203.

[282]

Ent. Tidskr. xvi. 1895, p. 116.

[283]

On larvae of Hepialidae, J. New York ent. Soc. iii. 1895, p. 69,


Plates III. IV.

[284]

Olliff, Australian Hepialidae, Entomologist, xxviii. 1895, p. 114.


[285]

Ent. Mag. xiii. 1876, p. 63; and xxiii. 1886, p. 164.

[286]

Weir, Entomologist, xiii. 1880, p. 249, plate; King, Ent. Record, vii.
1895, p. 111.

[287]

Bertkau, SB. Ver. Rheinland, xxxvi. 1879, p. 288; and Arch.


Naturg. xlviii. i. 1882, p. 362.

[288]

Zool. Anz. iii. 1880, p. 186.

[289]

It is much to be regretted that, as in so many other Lepidoptera, no


satisfactory agreement as to names has been attained; our British
A. testudo is variously styled Limacodes testudo (by Chapman and
most naturalists), Apoda limacodes (by Meyrick), or Apoda
avellana (Kirby, Catalogue of Moths). The family is called either
Limacodidae, Apodidae, Cochliopodidae, or Heterogeneidae.

[290]

See Chapman, Tr. ent. Soc. London, 1894, p. 345, Plate VII., for
our British species; for North American forms, Dyar, Life-histories
of the New York Slug-caterpillars (in progress, with numerous
plates), J. New York ent. Soc. iii. etc., 1895.

[291]

See Packard, P. Amer. Phil. Soc. xxxi. 1893, pp. 83, 108, Plates.
(He uses the term Cochliopodidae instead of Limacodidae); also
Dyar, as above.

[292]

Insects affecting the Orange, Washington, 1885, p. 143.


[293]

Tr. ent. Soc. London, 1894, p. 348.

[294]

Op. cit. 1876, p. 522; and 1877, p. 433.

[295]

P. Amer. Phil. Soc. xxxii. 1894, p. 275.

[296]

Revision of the Thyrididae; Hampson, P. Zool. Soc. London, 1897,


p. 603.

[297]

P. ent. Soc. London, 1891, p. xv.

[298]

This moth is known under several generic names—Psilura, Liparis,


Ocneria, Lymantria; there is now a very extensive literature
connected with it. A good general account by Wachtl may be found
in Wien. ent. Zeit. x. 1891, pp. 149-180, 2 Plates.

[299]

Wachtl and Kornauth, Mitt. forst. Versuchswesen Österreichs, Heft


xvi. 1893.

[300]

Crahay, Ann. Soc. ent. Belgique, xxxvii. 1893, p. 282.

[301]

Amer. Natural. xxix. 1895, p. 801.

[302]

Catalogue of Lepidoptera Heterocera, i. 1892.


[303]

Ann. Soc. ent. France (4), iv. 1864, p. 689.

[304]

P. Zool. Soc. London, 1892, p. 188.

[305]

Tr. ent. Soc. London, 1892, pp. 53-140; for criticism on the
nomenclature, see Rebel, Ent. Zeit. Stettin, liii. 1892, p. 247.

[306]

See Poulton, Tr. ent. Soc. London, 1884, p. 51; op. cit. 1892, p.
293; and Bateson, p. 213; Gould, p. 215.

[307]

Giraud, Ann. Soc. ent. France (4), v. 1865, p. 105; Fauvel, l.c. Bull.
p. liii.

[308]

For a table, see Meyrick, l.c.

[309]

Barrett, "Increasing Melanism in British Geometridae," Ent.


Monthly Mag. 1895, p. 198.

[310]

P. Zool. Soc. London, 1892, p. 192.

[311]

Although this term is widely used in North America, it is not in use


in England, though it may possibly have originated in Scotland.
See Slingerland, Bull. Cornell University Exp. Stat. 104, 1895, p,
555.
[312]

Fourth Rep. U.S. Ent. Commission, 1885, p. 3.

[313]

Insect Life, vi. 1894 p. 6.

[314]

See Chapman, The Genus Acronycta and its Allies, London, 1893.

[315]

Insects Injurious, etc., Ed. 1862, Boston, p. 437.

[316]

See Westwood, Tr. Zool. Soc. London, x. pp. 507, etc., for
discussion of this question and for figures; also E. Reuter, Act.
Soc. Sci. Fenn. xxii. 1896, p. 202.

[317]

Congr. Internat. Zool. ii. 1892, pt. 2, p. 180.

[318]

Ragonot, Ann. Soc. ent. France, 1890 and 1891; and Meyrick, Tr.
ent. Soc. London, 1890, p. 429.

[319]

Ent. Mag. xii. 1876, p. 210, and xvii. 1881, p. 249.

[320]

Zool. Jahrb. Syst. vi. 1892, p. 617.

[321]

Nat. Hist. Aquatic Insects, London, 1895.

[322]
For Bibliographic references connected with the divisions of
Pyralidae see Ragonot, Ann. Soc. ent. France (6), x. 1890, pp.
458, etc.

[323]

Monograph, by Ragonot, in Romanoff, Mem. Lep. vii. 1893.

[324]

Ent. Zeit. Stettin, 1878, p. 230.

[325]

Howard, Insect Life, vii. 1895, p. 402.

[326]

Monograph by Hampson, P. Zool. Soc. London, 1895, p. 897-974.

[327]

Disqué, Ent. Zeit. Stettin, li. 1890, p. 59. Cf. also Rebel, Zool.
Jahrb. Syst. xii. 1898, p. 3.

[328]

Classification; Meyrick, Tr. ent. Soc. London, 1886, p. 1.

[329]

P. Linn. Soc. N. S. Wales (2), vi. 1881, p. 410.

[330]

Handbook Brit. Lep. 1895, p. 493.

[331]

Tr. ent. Soc. London, 1895, p. 495.

[332]

Zool. Anz. v. 1882. p. 262.


[333]

Ann. Soc. ent. France (4), x. 1870, p. 1, pl. vii.

[334]

For table of the larvae, according to number of feet and other


characters, see Sorhagen, Berlin. ent. Zeit. xxvii. 1883, pp. 1-8.

[335]

P. Linn. Soc. N.S. Wales (2) vii. 1892, p. 593.

[336]

Durrant, Ent. Mag., xxxi. 1895, p. 107.

[337]

"The Yucca moth and Yucca Pollination," Rep. Missouri Botanical


Garden, 1892, pp. 99-158.

[338]

The maxillary tentacle is considered by Prof. J. B. Smith to be a


prolongation of the stipes, cf. antea, p. 309; also Insect Life, v.
1893, p. 161.

[339]

Chapman, Tr. ent. Soc. London, 1894, p. 366.

[340]

Walter, Jena. Zeitschr. Naturw. xviii. 1885. He did not distinguish


Eriocephala as a genus, as we have explained on p. 308.

[341]

Amer. Natural. xxix. 1895, pp. 636 and 803.

[342]

Wood, Ent. Mag. xxvi. 1890, p. 148.


[343]

See Chapman, Tr. ent. Soc. London, 1893, p. 255.

[344]

Osten Sacken, Tr. ent. Soc. London, 1884, p. 501, and Berlin. ent.
Zeitschr. xxxvii. 1892, p. 423, etc.

[345]

Osten Sacken has recently discussed the intermediate conditions,


and proposed the name "pseudholoptic" for some of them, Berlin.
ent. Zeitschr. xli. 1896, p. 367.

[346]

Girschner, Berlin. ent. Zeitschr. xxxi. 1887, p. 155.

[347]

It may be well to remark that this name was formerly applied to all
Diptera except Nemocera.

[348]

Zool. Anz. xvii. 1894, p. 35, and Ann. Nat. Hist. (6) xiii. 1894, p.
372; Zeitschr. wiss. Zool. lviii. 1895, p. 475.

[349]

Cf. Osten Sacken, Berlin. ent. Zeitschr. xxxviii. 1893; and Becher,
Wien. ent. Zeit. i. 1882, p. 49. For an account of the condition, with
diagrammatic figures, of the fly emerging from the pupa, cf.
Sasatti, J. Coll. Japan, i. 1887. p. 34, pl. vi.

[350]

It is frequently said that one sex of a single species may be


dimorphic in this respect, but we shall subsequently mention (in
Blepharoceridae) that this is not yet sufficiently established.
[351]

Fluernes Munddele, Copenhagen, 1881, 91 pp. 6 plates; Ent.


Tidskr. i. 1879, p. 150; Becher having given (Denk. Ak. Wien. xlv.
1882, p. 123) an interpretation different from that of Meinert, this
author set forth his general views in Zool. Anz. v. 1882, pp. 570
and 599.

[352]

The reader should not suppose that there are only two views as to
the Dipterous mouth, for actually there are several; our object is
here only to give a general idea of the subject.

[353]

Tr. Linn. Soc. London (2) v. 1892, p. 271.

[354]

Tr. ent. Soc. London, 1884, p. 497.

[355]

Osten Sacken, although making use of the terms tegula and


antitegula, suggested the propriety of using squama and
antisquama, as we have done.

[356]

Zeitschr. wiss. Zool. li. 1891, p. 55.

[357]

Brandt, Horae Soc. ent. Ross. xiv. 1878, p. vii.; xv. 1879, p. 20.
Brauer, Denk. Ak. Wien, xlvii. 1883, pp. 12-16. Künckel, C.R. Ac.
Paris, lxxxix. 1879, p. 491.

[358]

Blow-fly, 1895: in two vols. For Anatomy of Volucella, see Künckel


d'Herculais, Recherches sur l'org. des Volucelles, Paris, 1875 and
1881.
[359]

Tijdschr. Ent. xxxviii. 1895, pp. 65-100.

[360]

Denk. Ak. Wien, xlvii. 1883, pp. 1-100, pls. i.-v.

[361]

Since our brief and imperfect sketch of metamorphosis appeared


in Vol. V. of this series, Packard has treated the subject more fully
in his Text-book of Entomology, New York, 1898; and Pratt has
summarised the state of knowledge as to imaginal discs in
Psyche, viii. 1897, p. 15, etc.

[362]

Monograph of Oestridae, Verh. Ges. Wien, 1863, and other papers


op. cit. 1864, 1867, 1869; also Denk. Ak. Wien, xlii. 1880, xlvii.
1883.

[363]

Becher, Wien. Ent. Zeit. i. 1882, p. 49; for observation on


connecting forms see Brauer, Verh. Ges. Wien, xl. 1890, p. 272.

[364]

The palpi are said to be of only one segment in some genera of


Cecidomyiidae. The Cecidomyiidae are easily distinguished by the
minute size—body not more than a line long—and by there not
being more than six nervules at the periphery of the wing. Aëdes
(Culicidae) has also short palpi.

[365]

It is said by Schiner that in the anomalous genus Nemestrina the


palpi are of three segments.

[366]
For tables of the families of flies the student may refer to Loew,
Smithson-Misc. Coll. vi. Art. i. 1862; to Brauer, Denk. Ak. Wien, xlii.
1880, p. 110 (Orthorrhapha only); to Williston, Manual of N.
American Diptera, 1896; to Schiner, Fauna austriaca, Diptera,
Vienna, 1860, etc.

[367]

Berlin. ent. Zeitschr. xxxvii. 1892, p. 365, and xli. 1897, p. 365.

[368]

Tr. Amer. ent. Soc. iii. 1871, p. 345.

[369]

Bull. Soc. ent. France, 1893, p. lxxx.

[370]

Naturhist. Tidskr. (3) viii. 1874, p. 34, pl. xii.

[371]

Ann. Soc. ent. France (2) vii. 1849, p. 346.

[372]

Trans. New Zealand Inst. xxiii. 1890, p. 48.

[373]

Osten Sacken, Berlin. ent. Zeitschr. xxxvii. 1892, p. 442; and


Perris, Ann. Soc. ent. France (2) vii. 1849, p. 202.

[374]

See Guérin-Méneville, Ann. Soc. ent. France (2) iv. 1846; Bull. p.
8; and Nowicki, Verh. Ges. Wien, xvii. 1867, SB. p. 23.

[375]
For details as to the family cf. Osten Sacken, Berlin. ent. Zeitschr.
xl. 1895, p. 148; and for the larvae F. Müller, Arch. Mus. Rio-Jan.
iv. 1881, p. 47. The name "Liponeuridae" was formerly applied by
some authorities to this family, but it is now generally recognised
that Blepharoceridae is more legitimate.

[376]

Berlin. ent. Zeit. xxv. 1881, p. 61; and cf. Brauer, Wien. ent. Zeit. i.
1882, p. 1.

[377]

Natural History of Aquatic Insects, London, 1895, chap. ii.

[378]

Tr. Linn. Soc. Lond. (2) ii. 1884, p. 367.

[379]

For an extremely interesting account of Chironomus refer to Miall's


book, already cited, and, for the larva, to the valuable work of
Meinert on Eucephalous larvae of Diptera, Danske Selsk. Skr. (6)
iii. 1886, p. 436.

[380]

Ann. Nat. Hist. (4) viii. 1871, p. 31.

[381]

Ibid. (6) xv. 1895, p. 133.

[382]

For metamorphoses of aquatic species of Ceratopogon, see Miall


and Meinert, already quoted: for examples of the terrestrial
species, and their illustrations, refer to Mik, Wien. ent. Zeit. vii.
1888, p. 183.
[383]

Monograph, Eaton, Ent. Mag. xxix. and xxx. 1893, 1894:


supplement op. cit. 1896, etc.

[384]

Tr. ent. Soc. London, 1895, p. 141.

[385]

Tr. ent. Soc. London, 1895, p. 479.

[386]

A Naturalist's Sojourn in Jamaica, London, 1853, p. 284.

[387]

Bull. Illinois Lab., iv. 1895, p. 193.

[388]

Miall's Aquatic Insects, 1895, p. 174.

[389]

"Studies," etc., Berlin. ent. Zeitschr. xxxi. 1887.

[390]

Tr. ent. Soc. London, 1897, p. 362.

[391]

Tr. ent. Soc. London, 1897, pp. 343-361.

[392]

Acta Univ. Lund. xxxiii. (2) No. 7, 1897.

[393]

"Studies," etc., Berlin. ent. Zeitschr. xxx. 1886, p. 153.


[394]

Osten Sacken, Berlin. ent. Zeitschr. xxxvii. 1892, p. 450.

[395]

Entomologist, xiv. 1881, p. 287. This observation has never, we


believe, been confirmed.

[396]

Ann. Soc. ent. France (2) v. 1847, p. 46.

[397]

Perris, in Ann. Soc. ent. France (2) v. 1847, p. 37, pl. i.

[398]

Ann. Soc. ent. France (5) i. 1871, Bull. p. lxvii.

[399]

Rep. Dep. Agric. Ent. Washington, 1886, p. 492.

[400]

Cf. Réaumur, Mem. v. 1740, p. 21; and Perris, Ann. Soc. ent.
France (4) x. 1870, p. 190.

[401]

Verh. Ges. Wien, xxx. 1880, p. 343.

[402]

Arch. Naturges. xli. i. 1875, p. 48.

[403]

Bull. Illinois Lab. iv. 1895.

[404]

Ent. Mag. xxiii. 1886, p. 51.


[405]

Ann. Soc. ent. France, ii. 1833, p. 492.

[406]

Wien. ent. Zeit. ii. 1883, pp. 11 and 24, pl. i.

[407]

Ent. Mag. xiv. 1878, p. 196.

[408]

For figures, etc., cf. Westwood, Tr. ent. Soc. London, 1876, p. 507,
pls. v. vi.

[409]

Verh. Ges. Wien, xix. 1869, p. 737, pl. xiii.

[410]

Tr. ent. Soc. London (3) i. 1862, p. 338, pl. xi.

[411]

Verh. Ges. Wien, xix. 1869, p. 941.

[412]

Ann. Soc. ent. France (4) x. 1870, p. 221.

[413]

SB. Ak. Wien, xci. 1885, p. 392.

[414]

Ent. Mag. xiv. 1877, p. 226; for a discussion of the subject see Mik,
Wien. ent. Zeit. xiii. 1894, p. 273.

[415]

Amer. Natural. xxviii. 1894, p. 35.


[416]

Perris, Ann. Soc. ent. France (4) x. 1870, p. 321, pl. 4; and
Laboulbène, op. cit. (5) iii. 1873, p. 50, pl. v.

[417]

Perris, Ann. Soc. ent. France (4) x. 1870, p. 354.

[418]

Ent. Meddelelser, ii. 1890, p. 213.

[419]

Frauenfeld, Verh. Ges. Wien, xx. p. 37, pl. iii.

[420]

For monograph of Pipunculidae, see Becker, Berlin. ent. Zeitschr.


xlii. 1897, pp. 25-100.

[421]

Ofv. Ak. Forh. xi. 1854, p. 302, pl. v., since confirmed by others,
see Giard, C.R. Ac. Sci. cix. 1889, pp. 79 and 708.

[422]

Natural History of Aquatic Insects, 1895, p. 198.

[423]

Ent. Zeit. Stettin, vi. 1845, p. 384, pl. i.

[424]

Ann. Soc. ent. France (6) iii. 1883, p. 23, pl. i.

[425]

Ent. Nachr. xviii. 1892, p. 13.

[426]
Ann. Soc. ent. France (4) x. 1870, p. 330.

[427]

See on this difficult subject, Becher, Wien. ent. Zeit. i. 1882, p. 49.

[428]

Loudon's Magazine, v. 1832, p. 302; P. ent. Soc. London, 1871, p.


x.

[429]

Baron von Osten Sacken informs the writer that this statement has
since been withdrawn by Portschinsky as being erroneous.

[430]

Ent. Amer. iii. 1887, p. 126.

[431]

J. Coll. Japan, i. 1886, pp. 1-46, plates i.-vi.

[432]

Souvenirs entomologiques, 1879, pp. 246-254.

[433]

A list of the Insects known to be attacked by Dipterous parasites


has been given by Brauer and Bergenstamm, Denk. Ak. Wien, lxi.
1895.

[434]

Berlin. ent. Zeit. xxx. 1886, p. 135.

[435]

Berlin. ent. Zeitschr. xxxi. 1887, p. 17.

[436]
Biol. Centralbl. vii. 1887, p. 521.

[437]

For an account of the habits of this fly, see Kirk, J. Linn. Soc. viii.
1865, pp. 149-156; and for a bibliographic list, Wulp, Tijdschr. Ent.
xxvii. 1884, p. xci. and pp. 143-150.

[438]

Preliminary Report on the Tse-tse Fly Disease, 1895.

[439]

P. Liverpool Soc. xxxiii. 1878, p. 13, note.

[440]

We may specially mention the monograph of Oestridae, published


in 1863 by the K. k. Zool.-Bot. Ges. Wien, and supplements in
Wien. ent. Zeit. v. vi. 1886, 1887; these include copious
bibliographic lists.

[441]

Riley, Insect Life, iv. 1892, p. 302.

[442]

See Blanchard, Ann. Soc. ent. France (7) ii. 1892, pp. 109, 154.

[443]

See Bigot, Ann. Soc. ent. France (6) ii. 1882, p. 21, Brauer,
Monograph, 1863, p. 51, and Wien. ent. Zeit. vi. 1887, p. 75.

[444]

Arch. Naturgesch. lviii. i. 1892, pp. 287-322, pls. xv. xvi.

[445]

Stein, Deutsche ent. Zeit. xxi. 1877, p. 297.


[446]

Abh. Ges. Halle, iv. 1858, p. 145.

[447]

Arch. Naturgesch. lix. i. 1893, p. 151.

[448]

SB. Ak. Wien. cv. 1896, Abtheil. i. p. 400.

[449]

Arch. Naturges. lviii. i. 1892, p. 287.

[450]

Horae Soc. ent. Ross. ii. 1863, p. 90.

[451]

Tr. ent. Soc. London, 1881, p. 360.

[452]

The best general description of the external anatomy of the flea is


to be found in Taschenberg, Die Flöhe, 1880. The morphology is
better elucidated, though still incompletely, in Wagner's valuable
"Aphanipterologische Studien," Horae Soc. ent. Ross. xxiii. 1889,
pp. 199-260, 5 plates, and op. cit. xxxi. 1897, pp. 555-594, 3
plates. Cf. also N. C. Rothschild, Nov. Zool. v. 1898, pp. 533-544,
3 plates.

You might also like