100% found this document useful (3 votes)
7 views

(eBook PDF) Starting Out with Java: From Control Structures through Data Structures 3rd Editioninstant download

The document provides links to various eBook PDFs related to Java and C++ programming, including editions focused on control structures, data structures, and object-oriented programming. It highlights the availability of multiple editions for each title, suggesting they are suitable for different learning needs. Additionally, the document contains a detailed table of contents outlining topics covered in the books.

Uploaded by

oldaniifiemi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
7 views

(eBook PDF) Starting Out with Java: From Control Structures through Data Structures 3rd Editioninstant download

The document provides links to various eBook PDFs related to Java and C++ programming, including editions focused on control structures, data structures, and object-oriented programming. It highlights the availability of multiple editions for each title, suggesting they are suitable for different learning needs. Additionally, the document contains a detailed table of contents outlining topics covered in the books.

Uploaded by

oldaniifiemi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

(eBook PDF) Starting Out with Java: From Control

Structures through Data Structures 3rd Edition


pdf download

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

Download more ebook from https://ebooksecure.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebooksecure.com
to discover even more!

(eBook PDF) Starting Out with Java: From Control


Structures through Data Structures 4th Edition

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

(eBook PDF) Starting Out with Java: From Control


Structures through Objects, 7th Edition

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

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


Structures through Objects 8th Edition

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

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


Structures through Objects, Brief Version 8th Edition

http://ebooksecure.com/product/ebook-pdf-starting-out-with-c-
from-control-structures-through-objects-brief-version-8th-
edition/
(eBook PDF) Starting Out with C++ from Control
Structures to Objects 9th Edition

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

(eBook PDF) Data Structures and Abstractions with Java


4th Edition

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

Data Structures and Abstractions with Java 5th Edition


(eBook PDF)

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

(eBook PDF) Data Structures and Abstractions with Java


4th Global Edition

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

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


Edition

http://ebooksecure.com/product/ebook-pdf-starting-out-with-java-
early-objects-5th-edition/
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
Contents xxi

Companion Website:
Chapter 23 Databases
Appendix A Working with Records and Random Access Files
Appendix B The ASCII/Unicode Characters
Appendix C Operator Precedence and Associativity
Appendix D Java Key Words
Appendix E Installing the JDK and JDK Documentation
Appendix F Using the javadoc Utility
Appendix G More about the Math Class
Appendix H Packages
Appendix I More about JOptionPane Dialog Boxes
Appendix J Answers to Checkpoints
Appendix K Answers to Odd-Numbered Review Questions
Appendix L Getting Started with Alice
Appendix M Configuring JavaDB
Case Study 1 Calculating Sales Commission
Case Study 2 The Amortization Class
Case Study 3 The PinTester Class
Case Study 4 Parallel Arrays
Case Study 5 The FeetInches Class
Case Study 6 The SerialNumber Class
Case Study 7 A Simple Text Editor Application
This page intentionally left blank
Location of Videonotes in the Text
VideoNote

Chapter 1 Compiling and Running a Java Program, p. 14


Using an IDE, p. 15
Your First Java Program, p. 25

Chapter 2 Displaying Console Output, p. 33


Declaring Variables, p. 39
Simple Math Expressions, p. 55
The Miles-per-Gallon Problem, p. 106

Chapter 3 The if Statement, p. 111


The if-else Statement, p. 121
The if-else-if Statement, p. 132
The Time Calculator Problem, p. 185

Chapter 4 The while Loop, p. 193


The Pennies for Pay Problem, p. 263

Chapter 5 Passing Arguments to a Method, p. 279


Returning a Value from a Method, p. 293
The Retail Price Calculator Problem, p. 312

Chapter 6 Writing Classes and Creating Objects, p. 327


Initializing an Object with a Constructor, p. 348
The Personal Information Class Problem, p. 397

Chapter 7 Accessing Array Elements in a Loop, p. 409


Passing an Array to a Method, p. 424
The Charge Account Validation Problem, p. 489

Chapter 8 Returning Objects from Methods, p. 505


Aggregation, p. 517
The BankAccount, Class Copy Constructor Problem, p. 554

Chapter 9 The Sentence Capitalizer Problem, p. 608

Chapter 10 Inheritance, p. 613


Polymorphism, p. 657
The Employee and Productionworker Classes Problem, p. 698

Chapter 11 Handling Exceptions, p.703


The Exception Project Problem, p. 759

(continued on the next page)


LOCATION OF VIDEONOTES IN THE TEXT (continued)
VideoNote
Chapter 12 Creating a Simple GUI Application, p. 764
Handling Events, p. 777
The Monthly Sales Tax Problem, p. 846

Chapter 13 The JList Component, p. 852


The JComboBox Component, p. 868
The Image Viewer Problem, p. 912

Chapter 14 Creating an Applet, p. 929


The House Applet Problem, p. 988

Chapter 15 Nested Layouts 2, p. 1038

Chapter 16 Reducing a Problem with Recursion, p. 1045


The Recursive Power Problem, p. 1067

Chapter 17 The Bubble Sort Algorithm, p. 1069


Searching for Objects with the Binary Search Algorithm, p. 1118

Chapter 18 Inheritance and Generic classes, p. 1142


Highest and Lowest Elements, p. 1165

Chapter 19 The HashMap Class, p. 1202

Chapter 20 Adding a Node to a Doubly-Linked List, p. 1260


Recursive Linked List Operations, p. 1287

Chapter 21 Array Implementation of Queues, p. 1303


Array-Based Deque, p. 1332

Chapter 22 Removing a Value from a Binary Search Tree, p. 1349


Prefix Calculator, p. 1402
Another Random Document on
Scribd Without Any Related Topics
sometimes “he would wake and find his leg in a tub of water.” But he
was not an easy boy to bully, for he was ready to fight on small
provocation; and he won all his fights except one. He did credit to
his religious training by punching Lord Calthorpe’s head for calling
him an atheist, though it is possible that his objections to the
obnoxious epithet were as much social as theological, for an atheist,
among schoolboys is, by implication, an “outsider.”
“I was a most unpopular boy,” he told Moore, “but led latterly.” The
latter statement has been generally accepted by his biographers; but
not all the stories told in support of it stand the test of inquiry. There
is the story, for instance, accepted even by Cordy Jeaffreson, that he
led the revolt against Butler’s appointment to the headmastership,
but prevented his followers from burning down one of the class-
rooms by reminding them that the names of their ancestors were
carved upon the desks. “I can certify,” wrote the late Dean Merivale
of Ely, “that just such a story was told in my early days of Sir John
Richardson;” so that Byron seems here to have got the credit for
another hero’s exploits.
There are the stories, too, of his connection with the first Eton and
Harrow cricket match. Cordy Jeaffreson goes so far as to express
doubt whether he took part in the match at all; but that is
exaggerated scepticism, which research would have confuted. The
score is printed in Lillywhite’s “Cricket Scores and Biographies of
celebrated Cricketers;” and it appears therefrom that Byron scored
seven runs in the first innings and two in the second, and also
bowled one wicket; but even on that subject the Dean of Ely, who
went to Harrow in 1818, has something to say.
“It is clear,” the Dean writes, “that he was never a leader.... On the
contrary, awkward, sentimental, and addicted to dreaming and
tombstones, he seems to have been held in little estimation among
our spirited athletes. The remark was once made to me by Mr. John
Arthur Lloyd (of Salop), a well-known Harrovian, who had been
captain of the school in the year of the first match with Eton (1805):
‘Yes,’ he said, ‘Byron played in the match, and very badly too. He
should never have been in the eleven if my counsel had been
taken.’”
And the Dean goes on, picturing Byron’s awkwardness:
“Mrs. Drury was once heard to say of him: ‘There goes Byron’
(Birron she called him) ‘straggling up the hill, like a ship in a storm
without rudder or compass.’”
Byron’s influence at Harrow, in short, was exercised over his juniors
rather than his contemporaries. It pleased him, when he was big
enough, to protect small boys from school tyrants. One catches his
feudal spirit again in his appeal to a bully not to lick Lord Delawarr
“because he is a fellow peer”; but he was also ready to intervene in
other cases in which that plea could not be urged; and he had the
reward that might be expected. He once offered to take a licking for
one of the Peels; and he became a hero with hero-worshippers—
titled hero-worshippers for the most part—sitting at his feet. Lord
Delawarr, Lord Clare, the Duke of Dorset, the Honorable John
Wingfield, were the most conspicuous among them. It was from
their adulation that he got his first taste of the incense which was, in
later years, to be burnt to him so lavishly.
He described his school friendships, when he looked back on them,
as “passions”; and there is no denying that the language of the
letters which he wrote to his friends was inordinately passionate for
a schoolboy addressing schoolfellows. “Dearest” is a more frequent
introduction to them than “dear,” and the word “sweet” also occurs.
It is not the happiest of signs to find a schoolboy writing such
letters; and it is not altogether impossible that unfounded
apprehensions caused by them account for the suggestion made by
Drury—though the fact is not mentioned in the biographies—that
Byron should be quietly removed from the school on the ground that
his conduct was causing “much trouble and uneasiness.”
That, however, is uncertain, and one must not insist. All that the so-
called “passions”—occasionally detrimental though they may have
been to school discipline—demonstrate is Byron’s enjoyment of
flattery, and his proneness to sentiment and gush. He liked, as he
grew older, to accept flattery, while professing to be superior to it; to
enjoy sentiment, and then to laugh at it; to gush with the most
gushing, and then suddenly to turn round and “say ‘damn’ instead.”
But the cynicism which was afterwards to alternate with the
sentimentalism had not developed yet. He did not yet say “damn”—
at all events in that connection.
One must think of him as a boy with a great capacity for passionate
affection, and a precocious tendency to gush, deprived of the most
natural outlets for his emotions. He could not love his mother
because she was a virago; he hardly ever saw his sister; his
guardian kept him coldly at a distance. Consequently his feelings,
dammed in one direction, broke out with almost ludicrous intensity
in another; and his friendships were sentimental to a degree
unusual, though not, of course, unknown or unprecedented, among
schoolboys. He wrote sentimental verses to his friends.
But not to them alone. “Hours of Idleness,” first published when he
was a Cambridge undergraduate, is the idealised record of his school
friendships; but it is also the idealised record of other, and very
different, excursions into sentiment. It introduces us to Mary Duff, to
Margaret Parker, to Mary Chaworth,—and also to some other Maries
of less importance; and we will turn back and glance, in quick
succession at their stories before following Byron to Cambridge.
CHAPTER III
A SCHOOLBOY’S LOVE AFFAIRS—MARY DUFF,
MARGARET PARKER, AND MARY CHAWORTH
First on the list of early loves comes little Mary Duff of Aberdeen.
She was one of Byron’s Scotch cousins, though a very distant one;
and there is hardly anything else to be said, except that he was a
child and she was a child in their kingdom by the sea. Only no wind
blew out of a cloud chilling her. Her mother made a second marriage
—described by Byron as a “faux pas” because it was socially
disadvantageous—and left the city; and the two children never met
again.
It was of no importance, of course. They were only a little more than
seven when they were separated. But Byron was proud of his
precocity, and liked to recall it, and to wonder if any other lover had
ever been equally precocious. “I have been thinking lately a good
deal of Mary Duff,” he wrote in a fragment of a diary at the age of
twenty-five; and he reminded himself how he used to lie awake,
picturing her, and how he urged his nurse to write her a love letter
on his behalf, and how they sat together—“gravely making love in
our way”—while Mary expressed pity for her younger sister Helen,
for not having an admirer too. Above all, he reminded himself of the
shock which he felt, years afterwards, when the sudden
communication of a piece of news revived the recollection of the
idyll.
“My mother,” he proceeded, “used always to rally me about this
childish amour; and, at last, many years after, when I was sixteen,
she told me, one day, ‘Oh, Byron, I have had a letter from
Edinburgh, from Miss Abercromby, and your old sweetheart Mary
Duff is married to a Mr. C——.’ And what was my answer? I really
cannot explain or account for my feelings at that moment; but they
nearly threw me into convulsions, and alarmed my mother so much
that, after I grew better, she generally avoided the subject—to me—
and contented herself with telling it to all her acquaintance.”
And then again:
“My misery, my love for that girl were so violent that I sometimes
doubt if I have ever been really attached since. Be that as it may,
hearing of her marriage several years after was like a thunder stroke
—it nearly choked me—to the horror of my mother and the
astonishment and almost incredulity of nearly everybody.”
It is a well-known story, and one can add nothing to it beyond the
fact that Mary Duff’s husband was Mr. Cockburn, the wine merchant,
and that she lived quite happily with him, and that we are entitled to
think of her whenever we drink a glass of Cockburn’s port. But we
may also doubt, perhaps, whether Byron is, in this case, quite a
faithful reporter of his own emotions, and whether his grief was not
artistically blended with other and later regrets, and other and later
perceptions of the fickleness of the female heart and the mutability
of human things. For when we come to look at the dates, we find
that the date of Mary Duff’s marriage was also the date of Byron’s
desperate passion for Mary Chaworth.
Between Mary Duff and Mary Chaworth, however, Margaret Parker
had intervened. She was another cousin, descended from Admiral
Byron’s daughter Augusta. The first letter that Byron ever wrote was
addressed to her mother. “Dear Madam,” it began, “My Mamma
being unable to write herself desires I will let you know that the
potatoes are now ready and you are welcome to them whenever you
please.” For the rest, one can only quote Byron’s brief reminiscence:
“My first dash into poetry was as early as 1800. It was the ebullition
of a passion for my first cousin Margaret Parker, one of the most
beautiful of evanescent beings. I have long forgotten the verses, but
it would be difficult for me to forget her—her dark eyes—her long
eyelashes—her completely Greek cast of face and figure! I was then
about twelve—she rather older, perhaps a year. She died about a
year or two afterwards in consequence of a fall which injured her
spine and induced consumption.... My sister told me that, when she
went to see her, shortly before her death, upon accidentally
mentioning my name, Margaret coloured through the paleness of
mortality to the eyes.... I knew nothing of her illness, being at
Harrow and in the country, till she was gone. Some years after I
made an attempt at an elegy—a very dull one.”
And then Byron speaks of his cousin’s “transparent” beauty—“she
looked as if she had been made out of a rainbow”—and concludes:
“My passion had its usual effect upon me—I could not eat—I could
not sleep—I could not rest; and although I had reason to know that
she loved me, it was the texture of my life to think of the time that
must elapse before we could meet again, being usually about twelve
hours of separation! But I was a fool then, and am not much wiser
now.”
The elegy is included in the collected works. Special indulgence is
asked for it on the ground that it was “composed at the age of
fourteen.” It is very youthful in tone—quite on the conventional lines
—as one would expect. A single quatrain may be given—not to be
criticised, but merely to show that Byron, as a boy, was still looking
at life pretty much as his pastors and masters told him to look at it:
“And shall presumptuous mortals Heaven arraign!
And, madly, Godlike Providence accuse!
Ah! no, far fly from me attempts so vain;—
I’ll ne’er submission to my God refuse.”
We are still a long way here from the intense, the cynical, the
defiant, or even the posturing Byron of later years. The gift of
personal expression has not yet come to him; and he is still in
literary fetters, weeping, on paper, according to the rules. Intensity
and the personal note only begin with his sudden love for Mary
Chaworth; cynicism and defiance only begin after that love affair has
ended in failure.
Mary Chaworth was the heir of the Annesley property, adjoining
Newstead, and she was the grand-niece of the Chaworth whom the
wicked Lord Byron ran through the body in the upper chamber of
the Pall Mall tavern; so that their marriage, if they could have been
married, would, as Byron says, “have healed feuds in which blood
had been shed by our fathers.” But Byron was not yet the Byron who
had only to come, and to be seen, in order to conquer. He was a
schoolboy of fifteen, which is an awkward age. He had achieved no
triumphs in any field which could give him self-assurance. He was
not yet a leader, even among his schoolfellows; and he was not only
lame, but also fat. How shall a fat boy hope, whatever fires of genius
burn within him, to enter the lists against his elders and bear away
the belle from county balls? Byron, at any rate, failed signally in the
attempt to do so.
Newstead having been let to Lord Grey de Ruthen, Mrs. Byron was,
at the time, lodging at Nottingham; and Byron had various reasons
for preferring to see as little of her as possible. She was never
sympathetic; she was often quarrelsome; it was her pleasant habit,
when annoyed, to rattle the fire-irons and throw the tongs at him.
So he often availed himself of his tenant’s invitation to visit
Newstead, whenever he liked; and from Newstead it was the most
natural thing in the world that he should go over to Annesley, where
Miss Chaworth, with whom he already had a slight acquaintance,
was living with her mother, Mrs. Clarke.
He was always welcome there. There was as little desire on his
cousin’s side as on his to revive the recollection of the feud. When
he came to call, he was pressed to stay and sleep. At first he
refused, most probably from shyness, though he professed a
superstitious fear of the family portraits. They had “taken a grudge
to him,” he said, on account of the duel; they would “come down
from their frames at night to haunt him.” But presently his fears, or
his shyness, were conquered. He had seen a ghost, he said, in the
park; and if he must see ghosts he might just as well see them in
the house; so, if it was all the same to his hosts, he would like to
stay.
He stayed, and was entranced with Mary Chaworth’s singing. He
rode with her, and practiced pistol shooting on the terrace—more
than a little pleased, one conjectures, to show off his marksmanship.
He went with her—and with others, including a chaperon—on an
excursion to Matlock and Castleton. A note, written long afterwards,
preserves a memory of the trip:
“It happened that, in a cavern in Derbyshire, I had to cross in a boat
(in which two people only could lie down) a stream which flows
under a rock, with the rock so close upon the water as to admit the
boat only to be pushed on by a ferryman (a sort of Charon) who
wades at the stern, stooping all the time. The companion of my
transit was M.A.C., with whom I had long been in love, and never
told it, though she had discovered it without. I recollect my
sensations, but cannot describe them, and it is as well.”
And no doubt Mary Chaworth encouraged the boy, amused at his
raptures, enjoying the visible proof of her power, prepared to snub
him, in the end, if necessary, but scarcely expecting that there would
be any need for her to do so. She was seventeen, and a girl of
seventeen always feels capable of reminding a boy of fifteen that the
prayer book forbids him to marry his grandmother. Moreover, she
was engaged, though the engagement had not yet been announced,
to Mr. John Musters—a grown man and a Philistine—a handsome,
rather dissipated, hard-riding and hard-drinking country squire. The
dreamy, limping, fat boy from Harrow had no shadow of a chance
against his athletic rival. It was impossible for Mary Chaworth to
divine the genius that lurked beneath the fat. One has no right to
expect such powers of divination from girls of seventeen.
No doubt she thought the fat boy, as she would have said, “good
fun.” No doubt she was amused when, as a demonstration that he
was not too young to be loved, he showed her the locket which
Margaret Parker had given him, three years before, when he was
twelve. Unquestionably she flirted with him—or, at least, let him flirt
with her. She even gave him a ring, and the gift must have raised
high hopes, though it was the cause of the discovery which brought
the flirtation to an end.
Squire Musters discovered the ring among Byron’s clothes one day
when he and the boy were bathing together in the Trent. He
recognised it, picked it up, and put it in his pocket. Byron claimed it,
and Musters declined to give it up; and then, to quote the Countess
Guiccioli, who is the authority for the story:
“High words were exchanged. On returning to the house, Musters
jumped on a horse and galloped off to ask an explanation from Miss
Chaworth, who, being forced to confess that Lord Byron wore the
ring with her consent, felt obliged to make amends to Musters by
promising to declare immediately her engagement with him.”
Such is the story, as one gets it, through the Countess and through
Moore, from Byron himself; but we also get a side glimpse at it in a
letter, recently published,[2] from Mrs. Byron to Hanson, the family
solicitor. From this we gather that Byron, in order to make love, had
absented himself from school; that Drury had inquired the reason of
his absence; and that his mother was making strenuous, but
unavailing, efforts to induce him to return. Nothing was the matter
with him but love—“desperate love, the worst of all maladies in my
opinion.” He had hardly been to see his mother at all, but had been
spending all his time at Annesley. “It is the last of all connexions,”
she added, “that I should wish to take place”; and she begged Mr.
Hanson to make arrangements for her son to spend his next holidays
elsewhere. Expense was no object; and it would suit her very well if
Dr. Drury could be induced to detain him at Harrow.
And Byron himself, meanwhile, was writing to his mother, alternately
using lofty language about his right to choose his own friends, and
pleading for one more day in order that he might take leave.
He took it; but there is more than one version of the story.
“Do you think,” he overheard Mary Chaworth say to her maid, “that I
could care anything for that lame boy?” And, having heard that, “he
instantly darted out of the house, and, scarcely knowing whither he
ran, never stopped till he found himself at Newstead.” That is what
Moore tells us; but the picture drawn in “The Dream,”—the most
obviously and deliberately autobiographical of Byron’s poems—is
different.
“She loved,” he writes:
“Another: even now she loved another,
And on the summit of that hill she stood
Looking afar as if her lover’s steed
Kept pace with her expectancy, and flew.”
She was waiting, that is to say, for Squire Musters to ride up the
lane, while listening to Byron’s declaration. That is the first picture;
and then there follows the picture of the boy who “within an antique
oratory stood,” and to whom, presently, “the lady of his love re-
entered”:
“She was serene and smiling then, and yet
She knew she was by him beloved—she knew,
For quickly comes such knowledge, that his heart
Was darkened with her shadow, and she saw
That he was wretched, but she saw not all.
He rose, and with a cold and gentle grasp
He took her hand; a moment o’er his face
A tablet of unutterable thoughts
Was traced, and then it faded, as it came;
He dropped the hand he held, and with slow steps
Retired, but not as bidding her adieu,
For they did part with mutual smiles; he passed
From out the massy gate of that old Hall,
And mounting on his steed he went his way;
And ne’er repassed that hoary threshold more.”
There we have the Mary Chaworth legend as it has been handed
down from one generation of biographers to another. Byron,
according to that legend, saw Mary once after her marriage, but
once only. He was on the point of visiting her at a later date, but
was dissuaded by his sister. “If you go,” Augusta said, “you will fall in
love again, and then there will be a scene; one step will lead to
another, et cela fera un éclat.” He agreed that the reasoning was
sound, and did as he was advised. He tells that story himself, and
adds: “Shortly after, I married.”
And yet—the legend continues—this hopeless love, which touched
his heart at the age of fifteen, was the dominating influence of his
life. Mary Chaworth, though always absent, was yet always present.
He never loved any other woman, though he tried to love, and
indeed seemed to love, several. The vision of her face always came
between him and them. His later love affairs were only concessions,
or attempts to escape from himself and his memories—unavailing
attempts, for this memory continued to haunt him until the end.
It sounds incredible. The thoughts of youth may be long, long
thoughts; but the memories of youth are short, and the dreams of
youth are dreams from which we never fail to wake. And yet Byron
insists, quite as much as biographers have insisted. He insists in
“The Dream,” which was written more than a decade after the
parting. He insists in later poems, the inner meaning of which is
hardly to be questioned. So that speculation is challenged, and,
when pursued, leads us inevitably to a dilemma.
For of two things, one: Either Byron was posing—posing not only to
the world but to himself; or else the story, as all the biographers
from Moore to Cordy Jeaffreson have told it, is incomplete, and after
an interlude, had a sequel.
To search for such a sequel will be our task presently. Unless we can
find one, the development of the personal note in Byron’s work will
have to be left unexplained. The impression which we get, if we read
the more personal poems in quick succession, is of a man who first
awakes from the dream of love—and remains very wide awake for a
season—and then relapses and dreams it all over again. Unless the
story which first set him dreaming had had a sequel, that would
hardly be. So we will seek for the sequel in due course, though we
must first gather up the incidents of the interlude.
CHAPTER IV
LIFE AT CAMBRIDGE AND FLIRTATIONS AT
SOUTHWELL
Baffled in love, Byron returned to Harrow, after a term’s absence, in
January 1804, and remained there for another eighteen months.
This eighteen months is the period during which he describes
himself as having been happy at school. It is also the period during
which he haunted the Harrow churchyard, indulging his day dreams
as he looked down from the hillside on the wide, green valley of the
Thames. Those dreams, it is hardly to be doubted, were chiefly of
Mary Chaworth; and we may picture the poet’s secret sorrow as
giving him, fat though he was, a sense of superiority over other boys
who had no secret sorrows. Apparently, too, casting about for an
explanation of his failure, he realised that, in the rivalries of love, the
victory is far less likely to rest with the fat than with the lame; and
so, presently,—though not until after an interval of reflection—he set
himself the task of compelling his too solid flesh to melt.
He has been laughed at, and charged with vanity for doing so; but
he was right. He would also have been ridiculed, and with more
justice, if he had resigned himself to be overwhelmed by the rising
tide of superabundant tissue. Fatness is not merely a grotesque
condition. It is a condition incompatible with fitness; and it is far
nobler to resist it with systematic heroism than to cultivate it and call
heaven and earth to witness that one is the fattest person going;
and the fact that Byron, by dint of exercises which made him
perspire, a careful diet, and a persistent use of Epsom salts, reduced
his weight from fourteen stone six to twelve stone seven, is no small
achievement to be passed over lightly. It is, on the contrary, one of
the most memorable incidents in his development—the greatest of
all the feats performed by him at Trinity College, Cambridge,[3]
where he began to reside in October 1805.
He did not read for honours. At Oxford he might have done so, and
might have figured in the same class list as his Harrow friend, Sir
Robert Peel, who took a double-first, and Archbishop Whately, who
took a double-second. At Cambridge, however, the pernicious rule
prevailed that honours were only for mathematicians. The Classical
Tripos was not originated until a good many years afterwards, and
Byron had neither talent nor taste for figures. The most notable,
though not the highest, wranglers of his year were Adam Sedgwick,
the geologist, and Blomfield, Bishop of London. Byron would have
had to work very hard to make any show against them. He did not
enter the competition, but let his mind exercise itself on more
congenial themes, cherishing the belief—so erroneous and yet so
common—that Senior Wranglers never come to any good in after
life.
His allowance was £500 a year; and he kept a servant and a horse.
His general proceedings, except when he was writing verses were
pretty similar to those of the average young nobleman who attends
a University, not to instruct but to amuse himself. He rode, and
fenced, and boxed, and swam, and dived; he gambled and backed
horses; he was alternately guest and host at rather uproarious wine-
parties, and was spoken of as a young man “of very tumultuous
passions.” The statement has been made—he has made it himself
and his biographers have repeated it—that he lived quietly at first,
and only latterly got into a dissipated set; but as we find him, in his
second term, entreating his sister to back a bill for £800, the
statement probably needs to be modified in order to square with the
facts.
Apparently Augusta did not comply with his request; but the proofs
that he lived beyond his means are ample. Mrs. Byron was as loud in
her wail on the subject as the widows of Asher. She complains—this
also in the second term—of bills “coming in thick upon me to double
the amount I expected”; and she protests, in Byron’s first Easter
vacation, against his wanton extravagance in subscribing thirty
guineas to Pitt’s statue; while, in the course of the next Easter
vacation we find her consulting the family solicitor as to the
propriety of borrowing £1000 to get her son out of the hands of the
Jews, and declaring that, during the whole of his Cambridge career
he has done “nothing but drink, gamble, and spend money.”
Very similar is the testimony of his own and his sister’s letters. “I
was much surprised,” Augusta writes, in the second term, to the
solicitor, “to see my brother a week ago at the Play, as I think he
ought to be employing his time more profitably at Cambridge.” Byron
himself, writing to his intimates, confesses to several departures
from sobriety. The first was in celebration of the Eton and Harrow
match, which was followed by a convivial scene, foreshadowing
those at the Empire on boat-race night, at some place of public
entertainment. “How I got home after the play,” Byron says, “God
knows. I hardly recollect, as my brain was so much confused by the
heat, the row, and the wine I drank, that I could not remember in
the morning how I found my way to bed.” Later, in a letter to Miss
Elizabeth Bridget Pigot of Southwell, he speaks of his life as “one
continual routine of dissipation,” talks of “a bottle of claret in my
head,” and concludes with the specific admission: “Sorry to say been
drunk every day, and not quite sober yet.”
Possibly he exaggerates a little; but those who know the Universities
best will be least likely to suspect him of exaggerating very much.
There is always a set which lives in that style at any college
frequented by young men of ample means. Their ways, mutatis
mutandis, are faithfully described in the pages of “Verdant Green.”
Byron’s career, once more mutatis mutandis, was not unlike the
career of Charles Larkyns and Little Mr. Bouncer in Cuthbert Bede’s
picture of life at the sister University. He had, at any rate, one foot in
such a set as that, though he was in a better set as well, and formed
serious friendships with such men as Hobhouse, afterwards Lord
Broughton, Charles Skinner Matthews, afterwards Fellow of
Downing, Scrope Davies, afterwards Fellow of King’s, and Francis
Hodgson, ultimately Provost of Eton. It is not quite clear whether he
was, or was not, one of the rowdy spirits who “ragged” Lort Mansell,
the Master of Trinity.[4] He certainly annoyed the dons by keeping a
bear as a pet, and asserting that he intended the animal to “sit for a
fellowship.” But the most characteristic picture, after all, is that
which he draws (selecting his solicitor, of all persons in the world, for
his confidant) of his mode of reducing his flesh.
“I wear seven waistcoats, and a great Coat, run and play cricket in
this Dress, till quite exhausted by excessive perspiration, use the
bath daily, eat only a quarter of a pound of Butcher’s Meat in 24
hours.... By these means my ribs display Skin of no great Thickness,
and my clothes have been taken in nearly half a yard.”
That is the closing passage of a letter which begins with the
confession that “Wine and women have dished your humble
servant.” The two statements, taken in conjunction, furnish two-
thirds of the picture. The remaining third of it may be deduced and
constructed from the verses which Byron had then written or was
then writing.
It might be tempting to see in the period of dissipation a
disappointed lover’s desperate attempt to escape from an
ineffaceable recollection; and the view might be supported by
Byron’s own subsequent declaration that “a violent, though pure,
love and passion,” was “the then romance of the most romantic
period of my life.” Undergraduate excesses, however, rarely require
such recondite explanations; and Byron’s reminiscences had, as we
shall see, been coloured by intervening events. All the contemporary
evidence that one can gather goes to show that they were inexact;
that, though he had been hard hit by Mary Chaworth’s disdainful
reception of his suit, he did not mope, but, holding up his head, was
in a fair way to live his trouble down; and that his theory of himself,
put forward in the well-known lines in “Childe Harold”:
“And I must from this land begone
Because I cannot love but one”
is an after thought entirely inconsistent with his practices as a
Cambridge undergraduate.
One would be constrained to suspect that, even if the early poems
addressed to Mary Chaworth stood alone. There are not many of
them, and they lack the intensity of passion—the impression of all
possible hopes irremediably blighted—which “The Dream” reveals.
They strike one as a little stiff and artificial, as though the poet had
tried to express, not so much what he actually felt, as what he
considered that a man in his position ought to feel. That is
particularly the case with the poems of the first period. There are
boasts in them which we know to have been quite unwarranted by
the circumstances of the case. The poet pictures himself as one who
might disturb domestic peace if he chose, but refrains, being
merciful as he is strong:
“Perhaps his peace I could destroy,
And spoil the blisses that await him;
Yet let my rival smile in joy,
For thy dear sake, I cannot hate him.”
The boasts there, we see, are the prelude of resignation; and, a line
or two further on, resignation is followed by the resolution to forget:
“Then, fare thee well, deceitful Maid,
’Twere vain and fruitless to regret thee;
Nor Hope nor Memory yield their aid,
But Pride may teach me to forget thee.”
That is very conventional—hardly less conventional than the Elegy
on Margaret Parker—a sentimental “prelude to life,” one would
judge, of quite an ordinary kind. And, as has been said, the
sentimental utterance does not stand alone. Other verses, hardly
less sentimental, addressed to several other ladies, were, at the
same time, pouring from Byron’s pen.
Burgage Manor, a house which his mother had taken at Southwell,
near Nottingham, was his vacation home. He fled from his home,
from time to time, because of Mrs. Byron’s incurable habit of rattling
the fire-irons in order to draw attention to his faults; but he returned
at intervals, and stayed long enough to form a considerable circle of
friends—friends, be it noted, who belonged not to “the county” but
to the professional society of the town.
The county did not “call” to any appreciable extent. A few of the
men called on Byron himself; but none of the women called on Mrs.
Byron—whether because her reputation for rattling the fire-irons and
hurling the tongs had reached them, or because, on general
principles, they did not think her good enough to mix with them.
Byron, as was natural, resented their attitude and refused to return
visits which implied a slight upon his mother. Whatever his own
disputes with her, he would not have her snubbed by the local
magnates, or himself enter their doors on sufferance while she was
excluded from them. He mixed instead with the clergy, the doctors,
the lawyers, the retired colonels, and flirted with their sisters and
daughters. In that set he moved as a triton among the minnows,
fluttering the dovecotes of Southwell pretty much as, at a later date,
Praed, fresh from Eton, fluttered the dovecotes of Teignmouth. He
could not dance, of course, owing to his lameness; but he could
distinguish himself in amateur theatricals, and he could write verses.
His success in the Southwell drawing-rooms and boudoirs was the
first reward of his success in resisting and repelling the
encroachments of the flesh. The struggle was one which he had to
renew at intervals throughout his life; but his “crowning mercy” was
the victory of this date. He emerged from it slim, elegant, and
strikingly handsome. He rejoiced, and the girls of Southwell rejoiced
with him. They understood, as well as he did, that it is difficult for a
man to be fat and sentimental at one and the same time; that there
is something ludicrously incongruous in the picture of a fat boy
writing sentimental verses and professing to pine away for love. And
they liked him to write sentimental verses to them, and he was quite
willing to do so. He was, at this time, the sort of young man who will
write verses to any girl who will give him a keepsake—the sort of
young man to whom almost every girl will give a keepsake on
condition that he will write verses to her.
He wrote lines, for instance, “to a lady who presented to the author
a lock of hair braided with his own and appointed a night in
December to meet him in the garden.” Nothing is known of her
except that her name was Mary, and that she was neither Mary Duff
nor Mary Chaworth, but a third Mary “of humble station.” Southwell,
when it saw those verses, was shocked. It seemed highly improper
to Southwell that maidens of humble station should be encouraged
to presume by such attentions on the part of noblemen. Probably it
was on this occasion that the Reverend John Becher, Vicar of
Rumpton, Notts, expostulated with the poet for
“Deigning to varnish scenes that shun the day
With guilty lustre and with amorous lay.”
But Byron kept Mary’s lock of hair, and showed it, together with her
portrait, to his friends and wrote:
“Thro’ hours, thro’ years, thro’ time ’twill cheer—
My hope in gloomy moments raise;
In life’s last conflict ’twill appear,
And meet my fond, expiring gaze.”
To Mary Chaworth herself Byron could hardly have said more, but he
was, in fact, at this time, saying the same sort of thing to all and
sundry. Just the same sentiment recurs in the lines addressed “To a
lady who presented the author with the velvet band which bound
her tresses”:
“Oh! I will wear it next my heart;
’Twill bind my soul in bonds to thee:
From me again ’twill ne’er depart,
But mingle in the grave with me.”
Yet if Byron proposes to be faithful for ever to this un-named lady,
he proposes, at the same time, to be equally faithful to a lady who
can be identified as Miss Anne Houson:
“With beauty like yours, oh, how vain the contention!
Thus lowly I sue for forgiveness before you;—
At once to conclude such a fruitless dissension,
Be false, my sweet Anne, when I cease to adore you!”
And then there are other lines—innumerable other lines which would
also have to be quoted if the treatment of the subject were to be
encyclopædic—lines to Marion, lines to Caroline, lines to a beautiful
Quaker, lines to Miss Julia Leacroft, whose brother, the fire-eating
Captain John Leacroft remonstrated with Byron, and, according to
Moore, even went so far as to challenge him, on account of his
pointed attentions to his sister: lines, finally, to M.S.G. who would
appear, if verse could be accepted as autobiography, to have offered
to yield to Byron, but to have been spared because of his tender
regard for her fair fame:
“I will not ease my tortured heart,
By driving dove-ey’d peace from thine;
Rather than such a sting impart,
Each thought presumptuous I resign.

“At least from guilt shalt thou be free,


No matron shall thy shame reprove;
Though cureless pangs may prey on me,
No martyr shalt thou be to love.”
With that citation we may quit the subject. Not one of the sets of
verses—with the single exception of the set addressed to Miss
Leacroft—has any discoverable story attached to it. All of them—or
nearly all of them—have the air of celebrating some profound
attachment from which no escape is to be looked for on this side of
the grave. Byron’s later conception of himself as a man who had
loved but one had not crept into his poetry yet. He had not even
begun to strike the pose of the Childe impelled to “visit scorching
climes beyond the sea” because the one he loved “could ne’er be
his.”
The idea, indeed, of a man fleeing the country in 1809 because he
had loved in vain in 1804 would not, in any case, carry conviction.
Even to a poet the idea could hardly have presented itself without
some definite renewal of the memories. They were revived, in fact,
at a dinner party, in 1808, of which we find an account in one of
Byron’s letters to Hodgson:
“I was seated near a woman to whom, when a boy, I was as much
attached as boys generally are, and more than a man should be. I
knew this before I went, and was determined to be valiant and
converse with sang froid; but instead I forgot my valour and my
nonchalance, and never opened my lips even to laugh, far less to
speak, and the lady was almost as absurd as myself, which made
both the object of more observation than if we had conducted
ourselves with easy indifference. You will think all this great
nonsense; if you had seen it, you would have thought it still more
ridiculous. What fools we are! We cry for a plaything which, like
children, we are never satisfied with till we break open, though, like
them, we cannot get rid of it by putting it on the fire.”
That is the prose record of the meeting, and there is also a record in
verse. There are lines “to a lady on being asked my reason for
quitting England in the Spring”; there is the piece beginning, “Well!
thou art happy”:
“Mary, adieu! I must away:
While thou art blest I’ll not repine;
But near thee I can never stay;
My heart would soon again be thine.”
And also:
“In flight I shall be surely wise,
Escaping from temptation’s snare;
I cannot view my Paradise
Without the wish of dwelling there.”
Poor stuff, as poetry, it will be agreed. Any one who wrote poetry at
all might have written it. The sentiment rendered in it is just the
sentiment which any sentimental youth would have felt to be proper
to the occasion. We can find in it, at most, only the faint fore-
running shadow of the Byronic pose. It rings very insincerely if we
set it beside the lines in which Walter Savage Landor, at about the
same period, commemorated a similar moment of emotion:
“Rose Aylmer, whom these waking eyes
May weep but never see;
A night of memories and of sighs
I consecrate to thee.”
In that comparison, most decidedly, all the advantage is with Landor
—inevitably, because his were the feelings of a man, whereas
Byron’s were the feelings of a boy. He was only twenty, and his age
is the explanation of a good deal. It explains his startled timidity,
described in the letter to Hodgson, in a novel, romantic situation. It
explains his hugging his grief as a precious possession on no
account to be let go. It also explains the zest with which, when grief
had had its sacred hour, he could turn from it and throw himself into
other activities.
He rejoiced in the pose, only outlined as yet, which was presently to
make him the most interesting man (to women at all events) in
Europe; but he also rejoiced in his youth. He flirted, as we have
seen; he took part in amateur theatrical performances; he engaged
energetically in most of the sports of the day, fencing with Angelo,
boxing with Gentleman Jackson, swimming the Thames from
Lambeth to the Tower; he accumulated debts with the fine air of a
man heaping Pelion on Ossa; he flung down his defiant challenge to
the literary bigwigs in “English Bards and Scotch Reviewers”; he
drew his plans for the grand tour. The world, in short, was just then
“so full of a number of things” that Mary Chaworth’s importance in it
can easily be, as it has often been, exaggerated.
Presently we shall see Byron exaggerating it; and we shall also see
how he came to do so—how the boy’s occasional pose became the
determining reality of the man’s life. But before we come to that, we
must turn back.
CHAPTER V
REVELRY AT NEWSTEAD—“ENGLISH BARDS AND
SCOTCH REVIEWERS”
One watches the swelling of Byron’s indebtedness with morbid
interest. It is like the rapid rising of a Spring tide which threatens to
submerge a city. Already, in his second term at Cambridge, as we
have seen, he besought his sister to pledge her credit for his loans.
At the beginning of his third year, we find him making a confession
to his solicitor:
“My debts amount to three thousand, three hundred to Jews, eight
hundred to Mrs. B. of Nottingham, to coachmaker and other
tradesmen a thousand more, and these must be much increased
before they are lessened.”
They were increased before they were lessened—unless the
explanation be that Byron only told the truth about them in
instalments. Three months later this is his confession to the
Reverend John Becher:
“Entre nous, I am cursedly dipped; my debts, everything inclusive,
will be nine or ten thousand before I am twenty-one.”
But, even so, the high-water mark is not yet reached. Towards the
end of the same year, when Byron is contemplating his “grand tour,”
he once more calls his solicitor into council:
“You honour my debts; they amount to perhaps twelve thousand
pounds, and I shall require perhaps three or four thousand at setting
out, with credit on a Bengal agent. This you must manage for me.”
A pleasant commission, which seems to have led to a reference to
Mrs. Byron, who made a luminous suggestion:
“I wish to God he would exert himself and retrieve his affairs. He
must marry a woman of fortune this Spring; love matches is all
nonsense. Let him make use of the Talents God has given him. He is
an English Peer, and has all the privileges of that situation.”
It was a matter-of-fact proposal, worthy of the canny Scotswoman
who made it—a proof that, even when she threw the tongs at her
son, she still had his interests at heart; but nothing came of it. Very
likely Byron, at this date knew no heiresses; and even his mother
was not matter-of-fact enough to expect him to advertise for one,
even for the purpose of avoiding the necessity of selling Newstead.
There was still the resource of borrowing a little more, and of
making the loans go as far as possible by retaining the money for
personal expenses, instead of applying it to the payment of debt;
and something of that sort seems to have been done. Scrope Davies
lent Byron £4800; and yet Mrs. Byron had occasion to write:
“There is some Trades People at Nottingham that will be completely
ruined if he does not pay them, which I would not have happen for a
whole world.”
Moreover, though Byron himself talked vaguely to Hanson of the
possibility of his marriage with “a golden Dolly,” he was at an age at
which a young man does not readily marry any woman with whom
he is not in love. Whether he was or was not, at that time, in love
with Mrs. Chaworth,[5] he certainly was not in love with any one
else; and he was enjoying himself and “having his fling,” after the
manner of gilded youth. His “domestic female companion,” to use
Gibbon’s charming phrase, was a professional daughter of joy who
travelled about with him in male attire. He even brought her to
Newstead, when he took possession of the Abbey on the expiration
of Lord Grey de Ruthen’s tenancy. That may have been one reason—
though it need not necessarily have been the only one—for his
refusal to let his mother join him there. It would certainly have been
a valid reason for postponing matrimony.
Around those Newstead revels a good deal of fantastic legend
circles; and the facts concerning them are hardly to be disentangled
from the myths. “Childe Harold” starts with them:—
Ah! me! in sooth he was a shameless wight,
Sore given to revel and ungodly glee;
Few earthly things found favour in his sight
Save concubines and carnal companie,
And flaunting wassailers of high and low degree.
“Childe Harold,” however, in spite of the fact that it was first called
“Childe Buron,” is a poem, not a deposition. The picture, with its
“Paphian girls” and the rest of it—
Where superstition once had made her den,
Now Paphian girls were wont to sing and smile,
And monks might deem their time was come agen,
If ancient tales say true, nor wrong these holy men,
is not necessarily faithful because the note of contrast which it
sounds is of the essence of the poem. But, on the other hand, the
excuses and explanations by means of which Moore and Cordy
Jeaffreson attempt to palliate and minimise the supposed assertions
of the poem are somewhat less than convincing.
The revels, say these apologists, cannot have been so very dreadful
because the Newstead guests sometimes included some of the local
clergy, and because some of the young men who engaged in them
afterwards took orders. The obvious answer to that is that the
revellers may very well have moderated their revelry on the
occasions on which clergymen were present—and that those of them
who afterwards became pillars of the Church may not, at that date,
have got the old Adam into complete subjection. Nor is a great deal
gained by the contention that the part of the supposed “Paphian
girls” was, in fact, sustained by Byron’s “domestic female
companion,” and by the Newstead cook and the Newstead
housemaid. To say this is merely to protest that the alleged Paphians
did not really come from Paphos, but from some other island in the
same neighbourhood.
A letter written by Charles Skinner Matthews to his sister is the only
contemporary chronicle of the proceedings. There is a confirmation
of his account, together with some supplementary details, in a letter
written, long afterwards, by Byron to John Murray. Remembering the
ages and circumstances of the revellers—and remembering also that
Moore’s information was derived from some of them—we will try to
get as near to the truth as the procurable evidence allows.
Byron, one must always bear in mind, had not yet conquered his
place in county society, or in what is now called “smart” society. His
mother’s eccentricities and his guardian’s chilly attitude had, as we
have seen, kept him out of it. He actually knew no peer who could
or would introduce him when he took his seat in the House of Lords.
The people whom he knew at home were chiefly provincial people of
the professional classes. At Cambridge he had got into a fast, though
not an unintellectual, set. He was very young, and he had plenty of
credit, if not much ready money; and here was the “venerable pile”
of Newstead—not the less venerable because it was dilapidated—at
his disposal as a playground, and a place in which to dispense
hospitality.
Naturally he wanted to show Newstead to his friends, whom he had
never been able to entertain at home before. Naturally, having
credit, he used it to fit up and furnish as much of Newstead as was
necessary for their comfortable accommodation, not troubling to
foresee the day—though he would not have had to look very far
ahead in order to foresee it—when the bailiffs would be put in to
seize the goods in default of payment. Naturally, as Mrs. Byron was
so addicted to rattling the tongs and throwing the fire-irons at him,
he did not want her there. Naturally, his college friends having fast
tastes and habits, and no ladies of their own station being of the
party, the method of their life did not follow the conventional round
of the ordinary house-party. The pet bear, and the pet wolf, which
guarded the entrances, were only symbols of the unusual and
extravagant state of things within.
Breakfast, in theory, could be served at any hour. The hour actually
preferred by the majority of the party was one p.m. Matthews, who
generally came down between eleven and twelve, “was esteemed a
prodigy of early rising.” Any one, he says, who had wanted to
breakfast as early as ten “would have been rather lucky to find any
of the servants up.” Not until two p.m., as a rule, was the breakfast
cleared away. The amusements of the afternoon—which Matthews
euphemistically calls the morning—were “reading, fencing, single-
stick, or shuttle-cock, in the great room, practising with pistols in the
hall, walking, riding, cricket, sailing on the lake, playing with the
bear, or teasing the wolf.” Dinner was between seven and eight, and
then—another euphemism most proper in a letter to a sister—“the
evening diversions may be easily conceived.”
Those evening diversions consisted, in the first instance of dressing
up and drinking. The beverages, according to Byron himself, were
“burgundy, claret, champagne, and what not,” quaffed not only out
of ordinary glasses, but also out of a loving-cup fashioned from a
skull which had been dug up in the Newstead grounds. As for the
dressing-up; “A set of monkish dresses,” says Matthews, “which had
been provided, with all the proper apparatus of crosses, beads,
tonsures, &c., often gave a variety to our appearance and to our
pursuits,” which pursuits consisted, in Byron’s words, of “buffooning
all round the house in our conventual garments.”
That Matthews speaks of tonsures as if they were articles of dress is
neither here nor there; and there is no importance to be attached to
his omission of all reference to the “buffooning.” We know from
Hobhouse that he played his part in it, and that one of the
amusements of this brilliant young Fellow of Downing was to hide
himself in a stone coffin in the Long Gallery and groan, by way of
alarming his brother revellers. Evidently the Monks of Newstead,
while taking some hints from the profane members of the
Medmenham Hell Fire Club, carried out, to the best of their ability,
the traditions of the Monks of Thelema. “Fays ce que voudras” might
have been their motto; and the doing of what they wished appears
to have involved and included the extension of invitations to the
cook and the housemaid to participate in their pleasures. Moore says
so, not as one who makes a charge, but as one who makes an
admission to rebut a graver charge, and is full of sympathy for the
exuberance of lusty youth. Moralists must make what they can of
the story, and apportion censure and indulgence as they think just.
The excesses, at any rate, whatever their degree and nature, did not
fill Byron’s life. He was getting on with his poetry in spite of them,
though it would be too much to say that he had yet proved his title
to be called a poet.
“Hours of Idleness” had appeared while he was at Cambridge. The
interest of that volume, nowadays, is far more biographical than
poetical. When one has inferred from it that Byron did not pass
through the University with a heart bowed down by the loss of Mary
Chaworth, but flirted with a long series of the belles of Southwell,
one has said nearly all that there is to say. The poems themselves,
as the quotations given amply demonstrate, are no better than the
general run of undergraduate verse composition. They are purely
imitative; no new note rings in them. One is not surprised that Lord
Carlisle, on receiving a presentation copy, was in a greater hurry to
acknowledge than to read it, and merely remarked, in his
acknowledgment that young men were better occupied in writing
poetry than in devoting their valuable time to women and horses.
“Tolerably handsome,” was Byron’s first verdict on that letter; but he
seems to have felt snubbed when he read it over a second time.
Lord Carlisle’s opinions, he wrote to Miss Pigot, were nothing to him,
but his guardian must not be “insolent.” If he were insolent, he
should be gibbeted, just as Butler of Harrow had been gibbeted. In
fact, and to sum up:
“Perhaps the Earl ‘bears no brother near the throne’—if so, I will
make his sceptre totter in his hands.”
Which shows that Byron’s back was up, and that he was already in a
fighting mood when the famous review in the Edinburgh introduced
a jarring note into the chorus of approbation.
The author of the attack was not Jeffrey, as Byron thought, but
Brougham. He had the excuse, for what it may be worth, that the
poems had indubitably been over-praised because they had
appeared under the signature of a nobleman. He, therefore, set out
on the war path with the truculent air of a man whose conscience
requires him to bludgeon a butterfly. The punishment, we cannot
doubt, was very painful to the poet whom Cambridge
undergraduates and Southwell belles had flattered; and the instant
question for him was: Would he take his punishment lying down, or
would he take it fighting?
That question, however, was not long in doubt. The Byrons were a
fighting race; and the poet had inherited their love of fighting. Just
as he had fought Lord Calthorpe at Harrow for calling him an atheist,
so now he would fight the Edinburgh critic for calling him a fool. And
he would fight him with his own weapons. Let him have three
bottles of claret to prime him, and then he would strip for the fray,
and would “take on,” not the reviewer only, but every one whom the
reviewer had praised, and every one whom he himself disliked, or
thought he might dislike if he knew him better. So he emptied his
three bottles, and set to work on “English Bards and Scotch
Reviewers,” and having written twenty lines of it, “felt better.”
It is the poem in which his genius first begins to be apparent. Most
of the judgments expressed in it were unjust—most of them were
afterwards retracted by their author; but that does not matter. One
does not expect sound criticism from poets—least of all does one
expect it from poets of one-and-twenty. The essence of the thing is
that now, in “English Bards and Scotch Reviewers” a new personality
spoke—and spoke loud enough to be heard.
The note of Byron—the note which gained him his large and
attentive audience—was his reckless audacity. He was not afraid of
saying things; he did not wrap them up, like Wordsworth and
Coleridge, but said them in plain language which all the world could
understand—said them, moreover, in a manner which made them
appear true even to those who thought, or wished to think, them
false. His readers never knew what he would be saying next. They
only knew that, whatever it was, he would say it effectively, and, as
has already been remarked, with the air of one who damned the
consequences. That was the note which was, in later years, to ring
through “Don Juan.” We can already hear it ringing, as it were in
anticipation, through the couplets of “English Bards and Scotch
Reviewers.”
Many examples might be cited; for the Satire, after the way of
Satires, is almost entirely composed of damnatory clauses. Any piece
of gossip was good enough for Byron to lay hold of and use as a
missile when running amok among literary reputations. The best
instance, however, may be found in the passage in which he turned
and rent Carlisle.
His original intention was to make himself pleasant to his guardian.
He had no particular reason for liking him, but he had no definite
case against him. There was the letter, of course, in which Carlisle
had patronised the poet instead of praising his poetry; but he had
got over his irritation about that, and did not bear malice; and so he
prepared for publication these lines of fulsome eulogy:
“Ah, who would take their titles from their rhymes?
On one alone Apollo deigns to smile,
And crowns a new Roscommon in Carlisle.”
But then, before the day of publication, occurred his quarrel with
Carlisle. He thought that his guardian ought to have volunteered to
introduce him when he took his seat in the House of Lords; he had
the more reason for thinking so because his guardian was the only
Peer of the Realm whom he knew. Carlisle, however, did not do so,
Welcome to Our Bookstore - The Ultimate Destination for Book Lovers
Are you passionate about testbank and eager to explore new worlds of
knowledge? At our website, we offer a vast collection of books that
cater to every interest and age group. From classic literature to
specialized publications, self-help books, and children’s stories, we
have it all! Each book is a gateway to new adventures, helping you
expand your knowledge and nourish your soul
Experience Convenient and Enjoyable Book Shopping Our website is more
than just an online bookstore—it’s a bridge connecting readers to the
timeless values of culture and wisdom. With a sleek and user-friendly
interface and a smart search system, you can find your favorite books
quickly and easily. Enjoy special promotions, fast home delivery, and
a seamless shopping experience that saves you time and enhances your
love for reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!

ebooksecure.com

You might also like