100% found this document useful (4 votes)
87 views

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

Control

Uploaded by

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

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

Control

Uploaded by

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

Visit https://ebookluna.

com to download the full version and


explore more ebooks

(eBook PDF) Starting Out with Java: From Control


Structures through Data Structures 3rd Edition

_____ Click the link below to download _____


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

Explore and download more ebooks at ebookluna.com


Here are some recommended products that might interest you.
You can download now and explore!

(eBook PDF) Starting Out with Java: From Control


Structures through Data Structures 4th Edition

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

ebookluna.com

(eBook PDF) Starting Out with Java: From Control


Structures through Objects, 7th Edition

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

ebookluna.com

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


through Objects 8th Edition

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

ebookluna.com

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


through Objects, Brief Version 8th Edition

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

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

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

ebookluna.com

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


Edition

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

ebookluna.com

Data Structures and Abstractions with Java 5th Edition


(eBook PDF)

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

ebookluna.com

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


Global Edition

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

ebookluna.com

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


Edition

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

ebookluna.com
Contents vii

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


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

Chapter 4 Loops and Files   189


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

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


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

Chapter 5 Methods   269


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

Chapter 6 A First Look at Classes   319


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

Chapter 7 Arrays and the ArrayList Class   405


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

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


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

Review Questions and Exercises   483


Programming Challenges   488

Chapter 8 A Second Look at Classes and Objects   495


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

Chapter 9 Text Processing and More


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

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


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

Chapter 10 Inheritance   613


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

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


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

Chapter 11 Exceptions and Advanced File I/O   703


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

Chapter 12 A First Look at GUI Applications   761


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

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


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

Chapter 13 Advanced GUI Applications   849


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

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


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

Chapter 14 Applets and More   917


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

Chapter 15 Creating GUI Applications with JavaFX   991


15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991
Event-Driven Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
15.2 Stages and Scenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
15.3 Scene Graphs and Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 995
Creating a Scene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
Visit https://ebookluna.com
now to explore a diverse
collection of ebooks available
in formats like PDF, EPUB, and
MOBI, compatible with all
devices. Don’t miss the chance
to enjoy exciting offers and
quickly download high-quality
materials in just a few simple
steps!
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
Other documents randomly have
different content
were also loaded with unsalable goods, could discover no better
reason than the same neutral rivalry for the cessation of Continental
demand. The shipowners, not yet satisfied by Sir William Scott’s law,
echoed the same cry. All the interested classes of England, except
the manufacturers and merchants who were concerned in commerce
with the United States, agreed in calling upon government to crush
out the neutral trade. Sir William Scott had merely required an
additional proof of its honesty; England with one voice demanded
that, honest or not, it should be stopped.
This almost universal prayer found expression in a famous
pamphlet that has rarely had an equal for ability and effect. In
October, 1805, three months after the “Essex” decision, while
Monroe was advising Madison to press harder than ever on all the
great belligerent Powers, appeared in London a book of more than
two hundred pages, with the title: “War in Disguise; or, the Frauds of
the Neutral Flags.” The author was James Stephen, a man not less
remarkable for his own qualities than for those which two
generations of descendants have inherited from him; but these
abilities, though elevating him immensely above the herd of writers
who in England bespattered America with abuse, and in America
befouled England, were yet of a character so peculiar as to bar his
path to the highest distinction. James Stephen was a high-minded
fanatic, passionately convinced of the truths he proclaimed. Two
years after writing “War in Disguise,” he published another pamphlet,
maintaining that the Napoleonic wars were a divine chastisement of
England for her tolerance of the slave-trade; and this curious thesis
he argued through twenty pages of close reasoning.[42] Through life
a vehement enemy of slavery, at a time when England rang with
abuse of America, which he had done much to stimulate, he had the
honesty and courage to hold America up as an example before
Europe, and to assert that in abolishing the slave-trade she had
done an act for which it was impossible to refuse her the esteem of
England, and in consequence of which he prayed that harmony
between the two countries might be settled on the firmest foundation.
This insular and honest dogmatism, characteristic of many robust
minds, Stephen carried into the question of neutral trade. He had
himself begun his career in the West Indies, and in the prize-court at
St. Kitt’s had learned the secrets of neutral commerce. Deeply
impressed with the injury which this trade caused to England, he
believed himself bound to point out the evil and the remedy to the
British public. Assuming at the outset that the Rule of 1756 was a
settled principle of law, he next assumed that the greater part of the
neutral trade was not neutral at all, but was a fraudulent business, in
which French or Spanish property, carried in French or Spanish
ships, was by means of systematic perjury protected by the
prostituted American flag.
How much of this charge was true will never be certainly known.
Stephen could not prove his assertions. The American merchants
stoutly denied them. Alexander Baring, better informed than Stephen
and far less prejudiced, affirmed that the charge was untrue, and that
if the facts could be learned, more British than enemy’s property
would be found afloat under the American flag. Perhaps this
assertion was the more annoying of the two; but to prove either the
one or the other was needless, since from such premises Stephen
was able to draw a number of startling conclusions which an English
public stood ready to accept. The most serious of these was the
certain ruin of England from the seduction of her seamen into this
fraudulent service; another was the inevitable decay of her merchant
marine; still another pointed to the loss of the Continental market;
and he heightened the effect of all these evils by adding a picture of
the British admiral in the decline of life raised to the peerage for his
illustrious actions, and enjoying a pension from the national bounty,
but still unable to spend so much money as became an English peer,
because his Government had denied to him the “safe booty” of the
neutral trade![43] Humor was not Stephen’s strongest quality, or he
never would have caricatured the British mind so coarsely; but
coarse as the drawing might be, England was not conscious of the
caricature. Cobbett alone could have done justice to the pecuniary
sanctity of the British peerage; but on this point humor was lost to
the world, for Cobbett and Stephen were in accord.
Thus a conviction was established in England that the American
trade was a fraud which must soon bring Great Britain to ruin, and
that the Americans who carried on this commerce were carrying on a
“war in disguise” for the purpose of rescuing France and Spain from
the pressure of the British navy. The conclusion was inevitable.
“Enforce the Rule of 1756!” cried Stephen; “cut off the neutral trade
altogether!” This policy, which went far beyond the measures of Pitt
and the decision of Sir William Scott, was urged by Stephen with
great force; while he begged the Americans, in temperate and
reasonable language, not to make war for the protection of so gross
a fraud. Other writers used no such self-restraint. The austere and
almost religious conviction of Stephen could maintain itself at a
height where no personal animosity toward America mingled its
bitterness with his denunciations; but his followers, less accustomed
than he to looking for motives in their Bibles, said simply that the
moment for going to war with the United States had come, and that
the opportunity should be seized.[44]
CHAPTER III.
The Eighth Congress had hardly expired, March 3, 1805, amid
the confusion and ill-temper which followed the failure of
impeachment, when President Jefferson and Secretary Madison
began to hear the first mutterings of European disaster. Talleyrand’s
letter to Armstrong, Dec. 21, 1804, arrived with its blunt
announcement that Napoleon meant to oppose every step of
Monroe’s negotiation at Madrid, and with its declaration that West
Florida had not been included in the retrocession of Louisiana to
France, but had been refused to France by King Charles. Jefferson
was then at Monticello, and thither the documents from Paris
followed him. He wrote to Madison that Monroe’s case was
desperate.
“I consider,” said the President,[45] “that we may anticipate the
effect of his mission. On its failure as to the main object, I wish he may
settle the right of navigating the Mobile, as everything else may await
further peaceable proceedings; but even then we shall have a difficult
question to decide,—to wit, whether we will let the present crisis in
Europe pass away without a settlement.”
This letter showed that as early as the month of March, 1805, the
President foresaw Monroe’s disasters, and began to speculate upon
the next step to be taken. The attempt to obtain Florida through
Spanish fears had failed; but his first impression was that everything
might go on as before, if the Spaniards would consent to a free
navigation of the Mobile. Madison was still more vague; his first
impulse was to retrace his steps. He wrote to the President a
singular letter of contradictions.
“I cannot entirely despair,” said he, March 27,[46] “that Spain,
notwithstanding the support given by France to her claim to West
Florida, may yield to our proposed arrangement, partly from its
intrinsic value to her, partly from an apprehension of the interference
of Great Britain; and that this latter consideration may, as soon as
France despairs of her pecuniary object, transfer her weight into our
scale. If she [France] should persist in disavowing her right to sell
West Florida to the United States, and above all can prove it to have
been the mutual understanding with Spain that West Florida was no
part of Louisiana, it will place our claim on very different ground,—
such probably as would not be approved by the world, and such
certainly as would not with that approbation be maintained by force. If
our right be good against Spain at all, it must be supported by those
rigid maxims of technical law which have little weight in national
questions generally, and none at all when opposed to the principles of
universal equity. The world would decide that France having sold us
the territory of a third party, which she had no right to sell, that party
having even remonstrated against the whole transaction, the right of
the United States was limited to a demand on France to procure and
convey the territory, or to remit pro tanto the price, or to dissolve the
bargain altogether.”
For eighteen months every French and Spanish agent in
Washington, Paris, and Madrid had assured Madison, in language
varying between remonstrance and insult, that Spain had not ceded
West Florida to France; the records of the State Department proved
that France had asked for West Florida and had been refused;
Jefferson had not ventured to record a claim to West Florida when
he received possession of Louisiana, and had been obliged to
explain, in language which Gallatin and Randolph thought
unsatisfactory, the words of the Mobile Act. In spite of this, Madison
committed himself and government to the claim that West Florida
was a part of Louisiana; he pressed that claim, not against France,
but against Spain; he brought Monroe to a rupture with the Spanish
government on that issue,—yet with these recollections fresh in his
mind, he suddenly told Jefferson that if France could prove a matter
of common notoriety, the world would decide that the United States
had acted without regard to law or equity, while in any case the claim
to West Florida as against Spain was a mistake.
That Madison should have followed a train of reasoning so
singular, was less surprising than that he should have advanced so
far without showing a sign that he was prepared for the next step.
Knowing as early as March, 1805, that his plans were defeated, and
that he might expect a repulse from Spain and France, he selected a
new minister to succeed Pinckney at Madrid. This diplomatist, whose
career was to be as futile if not as noisy as that of his predecessor
from South Carolina, was James Bowdoin of Massachusetts, son of
a celebrated governor of that State. Jefferson wrote privately to him,
April 27, announcing the appointment; and the tone of the letter
implied that in the month’s interval since the arrival of Talleyrand’s
manifesto the President’s pacific views had suffered a change.
“Our relations with that nation are vitally interesting,” he wrote.[47]
“That they should be of a peaceable and friendly character has been
our most earnest desire. Had Spain met us with the same disposition,
our idea was that her existence on this hemisphere and ours should
have rested on the same bottom, should have swum or sunk together.
We want nothing of hers, and we want no other nation to possess
what is hers; but she has met our advances with jealousy, secret
malice, and ill-faith. Our patience under this unworthy return of
disposition is now on its last trial, and the issue of what is now
depending between us will decide whether our relations with her are
to be sincerely friendly or permanently hostile. I still wish, and would
cherish, the former, but have ceased to expect it.”
Jefferson had the faculty, peculiar to certain temperaments, of
seeing what he wished to see, and of believing what he willed to
believe. Few other Americans could have seriously talked of the
Spanish empire in America as swimming or sinking with that of the
United States; but Jefferson, for the moment, thought that the
earthen pot of Spanish dominion could trust itself to float safely
under charge of the iron energy of American democracy. He could
gravely say in regard to Spain, “we want nothing of hers,” when for
eighteen months he had exhausted every resource, short of force, to
gain Baton Rouge, Mobile, and Pensacola, not to speak of East
Florida and Texas. He charged that Spain met his advances with
jealousy, secret malice, and ill-faith, after his ministers had intrigued
with Napoleon for nearly two years, in the constant hope of depriving
her of her property. Dec. 13, 1804, he wrote to General Heath: “With
Spain we shall always be bickering, but never at war till we seek
it;”[48] and six months later he wrote to Bowdoin that her secret
malice and ill faith were leading to permanently hostile relations. He
had not much further to go; for if he meant to maintain his authority
among rulers, the war that would never come till he sought it must be
sought.
As Monroe’s overthrow became more and more evident, the
President grew uneasy, and turned restlessly from one device to
another. In the first days of August Monroe’s despatches arrived,
announcing that he had left Madrid, and that all his offers had been
rejected by Spain. Madison was in Philadelphia, where his wife was
detained by a long and troublesome lameness. The President was at
Monticello. A brisk interchange of letters took place, marking from
day to day the fluctuations of feeling peculiar to the characters of the
two men. One question alone was to be decided,—should they seize
this moment to break with Napoleon?
Madison’s first reflections reached no result. He shrank from
admitting that the government stood between war and humiliation
more dangerous than war.
“The business at Madrid,” he said, August 2,[49] “has had an
awkward termination, and if nothing, as may be expected, particularly
in the absence of the Emperor, should alleviate it at Paris, involves
some serious questions. After the parade of a mission extraordinary, a
refusal of all our overtures in a haughty tone without any offer of other
terms, and a perseverance in withdrawing a stipulated provision for
claims admitted to be just, without ex post facto conditions manifestly
unreasonable and inadmissible, form a strong appeal to the honor and
sensibility of this country.”
The conclusion drawn from this somewhat mild review was not
such as Monroe, Armstrong, or Livingston had recommended.
“I find that, as was apprehended from the tenor of former
communications,” continued the secretary, “the military status quo in
the controverted districts, the navigation of the rivers running through
West Florida, and the spoliations subsequent to the convention of
1802 have never had a place in the discussions. Bowdoin may
perhaps be instructed, consistently with what has passed, to propose
a suspension of the territorial questions, the deposit, and the French
spoliations, on condition that those points be yielded, with an
incorporation of the convention of 1802 with a provision for
subsequent claims. This is the utmost within the Executive purview. If
this experiment should fail, the question with the Legislature must be
whether or not resort is to be had to force, to what extent, and in what
mode. Perhaps the instructions to Bowdoin would be improved by
including the idea of transferring the sequel of business hither. This
would have the appearance of an advance on the part of Spain, the
more so as it would be attended with a new mission to this country,
and would be most convenient for us also, if not made by Spain a
pretext for delay.”
Madison, after enduring one “refusal of all our overtures in a
haughty tone,” suggested that another be invited. The slightly
patronizing air which characterized Jefferson’s attitude toward
Madison, but which he never betrayed toward Gallatin, was
explained by this want of directness in Madison’s nature, and by the
habitual slowness of his decisions. The action suggested by Madison
threw the control of events into the hands of France. This at least
was the opinion of Jefferson, whose mind was wrought by the news
from Pinckney to a state of steadily growing alarm.
“I think the status quo, if not already proposed, should be
immediately offered through Bowdoin,” wrote Jefferson, August 4,
before receiving Madison’s letter of August 2.[50] “Should it even be
refused, the refusal to settle a limit is not of itself a sufficient cause of
war, nor is the withholding a ratification worthy of such a redress. Yet
these acts show a purpose, both in Spain and France, against which
we ought to provide before the conclusion of a peace. I think,
therefore, we should take into consideration whether we ought not
immediately to propose to England an eventual treaty of alliance, to
come into force whenever (within —— years) a war shall take place
with Spain or France.”

Three days later he wrote again, and his alarm had increased:[51]

“The papers now enclosed to you confirm me in the opinion of the
expediency of a treaty with England, but make the offer of the status
quo [to Spain] more doubtful; the correspondence will probably throw
light on that question. From the papers already received I infer a
confident reliance on the part of Spain on the omnipotence of
Bonaparte, but a desire of procrastination till peace in Europe shall
leave us without an ally.”

Ten days more passed; the whole mortification became evident;


the President’s anger and alarm rose to feverishness.[52] He wrote to
Madison, August 17,—
“I am anxious to receive opinions respecting our procedure with
Spain, as should negotiations with England be advisable they should
not be postponed a day unnecessarily, that we may lay their result
before Congress before they rise next spring. Were the question only
about the bounds of Louisiana, I should be for delay. Were it only for
spoliations, just as this is as a cause of war, we might consider if no
other expedient were more eligible for us. But I do not view peace as
within our choice. I consider the cavalier conduct of Spain as evidence
that France is to settle with us for her,—and the language of France
confirms it,—and that if she can keep us insulated till peace, she
means to enforce by arms her will, to which she foresees we will not
truckle, and therefore does not venture on the mandate now. We
should not permit ourselves to be found off our guard and friendless.”
The President’s plan presented difficulties which Madison could
not fail to see. That Jefferson should wish Pitt to fight the battles of
the United States was natural; but Pitt was little in the habit of doing
gratuitous favors, and might reasonably ask what price he was to
receive for conquering the Floridas and Texas for the United States.
Madison’s comments on the President’s proposed British treaty
pointed out this objection. Madison agreed that the Executive should
take provisional measures, on which Congress might act.[53] “An
eventual alliance with Great Britain, if attainable from her without
inadmissible conditions, would be for us the best of all possible
measures; but I do not see the least chance of laying her under
obligations to be called into force at our will without correspondent
obligations on our part.” Objection to the President’s plan was easy;
but when the secretary came to a plan of his own, he could suggest
nothing more vigorous than to renew a moderate degree of coquetry
with Merry, which would have the side advantage of alarming France
and Spain, “from whom the growing communication with Great
Britain would not be concealed.”
Such a weapon was no doubt as effective against Napoleon as
heelless slippers against Pitt; but the President thought the situation
to have passed beyond such tactics. Madison’s proposed coquetry
with Merry met with less favor in Jefferson’s eyes than his own
proposed one-sided alliance with England had met in the eyes of
Madison. Upon a treaty of alliance with England the President was
for the moment bent, and he met Madison’s objections by arguments
that showed lively traits of the writer’s sanguine temper. He
complained that Madison had misconceived the nature of the
proposed British treaty. England should stipulate not to make peace
without securing West Florida and the spoliation claims to America,
while American co-operation in the war would be sufficient
inducement to her for making this contract.[54]
“Another motive much more powerful would indubitably induce
England to go much further. Whatever ill humor may at times have
been expressed against us by individuals of that country, the first wish
of every Englishman’s heart is to see us once more fighting by their
sides against France; nor could the King or his ministers do an act so
popular as to enter into an alliance with us. The nation would not
weigh the consideration by grains and scruples; they would consider it
as the price and pledge of an indissoluble friendship. I think it possible
that for such a provisional treaty they would give us their general
guaranty of Louisiana and the Floridas. At any rate we might try them;
a failure would not make our situation worse. If such a one could be
obtained, we might await our own convenience for calling up the
casus fœderis. I think it important that England should receive an
overture as early as possible, as it might prevent her listening to terms
of peace.”
If Jefferson was right in thinking that every Englishman’s heart
yearned toward America, he was unfortunate in delaying his offer of
indissoluble friendship until the moment when Sir William Scott
delivered his opinion in the case of the “Essex.” Madison’s scheme
was equally unpromising, because he had made a personal enemy
of Merry, on whom the success of Madison’s tactics depended. Each
of the two high authorities felt the weakness of the other, and the
secretary even went so far as to hint, in courteous language, that the
President’s idea was unpractical:—
“The more I reflect on the papers from Madrid, the more I feel the
value of some eventual security for the active friendship of Great
Britain, but the more I see at the same time the difficulty of obtaining it
without a like security to her of ours. If she is to be bound, we must be
so too, either to the same thing,—that is to join her in the war,—or to
do what she will accept as equivalent to such an obligation. What can
we offer to her? A mutual guaranty, unless so shaped as to involve us
pretty certainly in her war, would not be satisfactory. To offer
commercial regulations or concessions on points in the law of nations
as a certain payment for aids which might never be received or
required, would be a bargain liable to obvious objections of the most
serious kind. Unless, therefore, some arrangement which has not
occurred to me can be devised, I see no other course than such an
one as is suggested in my last letter.”[55]
In this state of things, the remaining members of the Cabinet
were asked for their opinions; and in the course of a few days the
President received written papers from Gallatin and Robert Smith.
Gallatin was annoyed at the results of Jefferson’s diplomacy.
Emphatically a Northern man, he cared little for Florida; and a war
with Spain would have been in his eyes a Southern war. He made no
concealment of his opinion that the whole negotiation rested on a
blunder; and he told Madison as much, with a bluntness which the
secretary could scarcely have relished.
“The demands from Spain were too hard,” said he,[56] “to have
expected, even independent of French interference, any success from
the negotiation. It could only be hoped that the tone assumed by our
negotiators might not be such as to render a relinquishment or
suspension of some of our claims productive of some loss of
reputation. If we are safe on that ground, it may be eligible to wait for
a better opportunity before we again run the risk of lowering the
national importance by pretensions which our strength may not at this
moment permit us to support. If from the manner in which the
negotiation has been conducted that effect has already been
produced, how to save character without endangering peace will be a
serious and difficult question.”
These words were written before he had seen Monroe’s
despatches. When the whole correspondence was put into his hands
he read it, and in returning it to Madison made the dry comment that
the business had not ended quite so badly as he had previously
supposed.[57] The phrase bore a double meaning, for even Madison
must have admitted that the business could not have ended much
worse.
Gallatin sent to the President a remarkable paper,[58] in
substance an argument for peace, and in tenor a criticism of the
grounds which Jefferson, Madison, Monroe, Livingston, and
Pinckney had thought proper to take in their dispute with Spain.
Gallatin held that, owing to the “unpardonable oversight or
indifference” of Livingston and Monroe in failing to insist on a
boundary to Louisiana, the United States government was debarred
from holding Spain responsible for the inevitable consequences of its
own fault. Neither Spain’s qualified refusal to ratify the claims
convention of 1802 nor her rejection of the French spoliation claims
would justify war. As a matter of abstract justice, war was not to be
defended; as a matter of policy, it could not be recommended. The
expense and loss would exceed the value of Florida; the political
result would entangle America in alliance with England; and, “in fine,
a subversion of all our hopes must be the natural consequence.”
Renewal of negotiation was the proper step, with the Sabine and
Perdido as boundaries and a temporary arrangement under the
status quo, acceptance of the Spanish condition precedent to
ratifying the claims convention, and insistence against the new
spoliations which French and Spanish privateers were daily making
on American commerce in the West Indies. Pending the result of this
negotiation Congress might spend some money on the militia, and
might appropriate a million dollars annually to build ships of the line.
In effect, Gallatin threw his influence on the side of Madison
against the President’s semi-warlike views. The opinion of Robert
Smith did not weaken the force of Gallatin’s reasoning. Already a
perceptible division existed in the Cabinet between the Treasury and
the Navy. Hardly three months before the Spanish embarrassment,
Gallatin had spoken to the President in strong terms of Robert
Smith’s administration, and had added,[59]—
“On this subject,—the expense of the Navy greater than the object
seemed to require, and a merely nominal accountability,—I have, for
the sake of preserving perfect harmony in your councils, however
grating to my feelings, been almost uniformly silent.”
Smith’s present views tended to confirm Gallatin in his irritation,
and to reconcile Jefferson to abandoning his energetic schemes.
The Secretary of the Navy said that throughout these negotiations
Spain had presumed much on American predilection for peace, and
on the want of means to annoy her either by land or by water. He
urged the necessity of working on her fears, and advised that
Congress be recommended to provide additional gunboats, to put all
the frigates in commission, and to build twelve seventy-fours. With
these means he was disposed to take a commanding attitude; and if
Spain were supported by France, to make an alliance with England.
[60]

Gallatin and Robert Smith agreed only on one point,—that the


affair had been mismanaged. Both secretaries held that America had
made pretensions which she had not strength at the moment to
support. Rather than “again run the risk of lowering national
importance,” Gallatin preferred to submit to the consequent loss of
reputation, and return to a true peace-policy. Robert Smith wished to
maintain a high tone, and to arm. All Jefferson’s instincts were with
Gallatin; but the path that Gallatin proposed was hard and mortifying,
and although he made it as little abrupt as possible, he could not
prevent it from seeming what it was,—a severe humiliation to the
President. Not without some inward struggle could a President of the
United States bow his neck to such a yoke as Spain and France
imposed.
At that moment, the middle of September, arrived Armstrong’s
letter advising the military occupation of Texas and a cessation of
intercourse with Spain. His plan was the first well-considered
suggestion yet made for carrying out the policy hitherto pursued; and
although contrary to Gallatin’s advice, it agreed so well with the
President’s views that he caught at it with the relief of a man unable
to solve his own problem, who hears another explain what to himself
is inexplicable. Jefferson seized Armstrong’s idea, and uniting it with
his own, announced the result to Madison as the true solution of the
difficulty:[61]—
“Supposing a previous alliance with England to guard us in the
worst event, I should propose that Congress should pass acts (1)
authorizing the Executive to suspend intercourse with Spain at
discretion; (2) to dislodge the new establishments of Spain between
the Mississippi and Bravo; (3) to appoint commissioners to examine
and ascertain all claims for spoliation.”
Here at length was a plan,—uncertain, indeed, because
dependent on British help, but still a scheme of action which could
be discussed. The President appointed October 4 as the day on
which the Cabinet should reunite at Washington to consider his
project, but Madison replied that he could not return so soon; and in
order that the Cabinet should know his views, he explained at some
length the course he advised, which differed widely from that of the
President.
“With respect to Great Britain,” he said,[62] “I think we ought to go
as far into an understanding on the subject of an eventual coalition in
the war as will not preclude us from an intermediate adjustment, if
attainable, with Spain. I see not, however, much chance that she will
positively bind herself not to make peace, while we refuse to bind
ourselves positively to make war,—unless, indeed, some positive
advantage were yielded on our part in lieu of an engagement to enter
into the war. No such advantage as yet occurs as would be admissible
to us and satisfactory to her.”
In regard to England, therefore, Madison had nothing to propose
except negotiation without end. Having settled this point, he went on:

“At Paris I think Armstrong ought to receive instructions to
extinguish in the French government every hope of turning our
controversy with Spain into a French job, public or private; to leave
them under apprehensions of an eventual connection between the
United States and Great Britain; and to take advantage of any change
in the French Cabinet favorable to our objects with Spain.”
To leave Bonaparte “under apprehensions” was to be the object
of Madison’s diplomacy at Paris,—a task which several European
governments were then employing half a million armed men to
accomplish, hitherto without success, but which Madison hoped to
effect by civilities to Merry.
After this decision, nothing remained but to mark out a line of
conduct in regard to Spain. In the course of the summer Bowdoin,
the new minister, had sailed; but on arriving in Spain, and learning
the failure of Monroe’s negotiation, he went to Paris and London
without visiting Madrid.
“As to Spain herself,” continued Madison, “one question is,
whether Bowdoin ought to proceed or not to Madrid. My opinion is that
his trip to Great Britain was fortunate, and that the effect of it will be
aided by his keeping aloof until occurrences shall invite him to
Spain.... The nicest question, however, is whether any, or what, steps
should be taken for a communication with the Spanish government on
the points not embraced by the late negotiation. On this question my
reflections disapprove of any step whatever other than such as may
fall within the path to be marked out for Armstrong, or as may be
within the sphere of Claiborne’s intercourse with the Marquis of Casa
Calvo. Perhaps the last may be the best opportunity of all for
conveying to Spain the impressions we wish, without committing the
government in any respect more than may be advisable. In general it
seems to me proper that Claiborne should hold a pretty strong
language in all cases, and particularly that he should go every length
the law will warrant against Morales and his project of selling lands. If
Congress should be not indisposed, proceedings may be authorized
that will be perfectly effectual on that as well as other points; but
before their meeting there will be time to consider more fully what
ought to be suggested for their consideration.”
Having brought the government face to face with the government
of Spain, in the belief that Spain and France must yield to a
peremptory demand,—finding that Spain not only refused every
concession, but renewed depredations on American commerce and
took an attitude of indifference to threats or entreaties,—Madison
proposed no more vigorous measure than to “go every length the
law will warrant” against certain Spanish land-grants.
Such a course pleased no one, and threatened to create new
dangers. Monroe and Armstrong urged that a supposed devotion to
peace on the part of the President weighed heavily against him with
Spain and France. Jefferson approved their proposed aggressive
policy, as he wrote to Madison, chiefly because it would “correct the
dangerous error that we are a people whom no injuries can provoke
to war.”[63] He shrank from war, except under the shield of England,
and yet he feared England for an ally even more than Spain for an
enemy. His perplexity ended in helplessness. The Cabinet meeting
was held October 4; but he reported to Madison that nothing came of
it:[64]
“The only questions which press on the Executive for decision are
whether we shall enter into a provisional alliance with England, to
come into force only in the event that during the present war we
become engaged in war with France, leaving the declaration of the
casus fœderis ultimately with us; whether we shall send away Yrujo,
Casa Calvo, Morales; whether we shall instruct Bowdoin not to go to
Madrid till further orders. But we are all of the opinion that the first of
these questions is too important and too difficult to be decided but on
the fullest consideration, in which your aid and counsel should be
waited for.”
Again Madison wrote back his opinion. More than six months had
elapsed since the President, March 23, despaired of Monroe’s
mission; every alternative had been repeatedly discussed; every
advice had been taken. Congress would soon meet; something must
be decided,—in reality delay was itself a decision; yet the President
and Secretary of State seemed no nearer a result than they had
been six months before. Meanwhile the European packets brought
news that put a different face on the problem. Sir William Scott’s
decision in the case of the “Essex” arrived; seizures of American
ships by England began; Pitt’s great coalition with Russia and
Austria against Napoleon took the field, and August 27 Napoleon
broke up the camp at Boulogne and began his long-intended
movement across the Rhine. Upon Madison’s mind this European
convulsion acted as an additional reason for doing nothing:[65]—
“Considering the probability of an extension of the war against
France, and the influence that may have on her temper toward the
United States, the uncertainty of effecting with England such a shape
for an arrangement as alone would be admissible, and the possible
effects elsewhere of abortive overtures to her, I think it very
questionable whether a little delay may not be expedient, especially
as in the mean time the English pulse will be somewhat felt by the
discussions now on foot by Mr. Monroe.”
Accordingly the Secretary advised that Morales, Casa Calvo, and
Yrujo should be ordered out of the country, while Bowdoin should
remain in England,—and so left it.
Madison’s measures and conduct toward Europe showed the
habit of avoiding the heart of every issue, in order to fret its
extremities. This mark of Madison’s character as a diplomatist led
him into his chief difficulties at home and abroad; but the Spanish
imbroglio of 1805 first brought the weakness into public notoriety,
and he recovered from the subsequent revelation only after years of
misfortune. The same habit of mind made him favor commercial
restrictions as a means of coercion. So he disregarded Armstrong’s
idea of seizing Texas, but warmly approved of his passing
suggestion as to an embargo:[66]—
“The efficacy of an embargo cannot be doubted. Indeed, if a
commercial weapon can be properly shaped for the Executive hand, it
is more and more apparent to me that it can force all the nations
having colonies in this quarter of the globe to respect our rights.”
This mental trait was closely connected with Madison’s good
qualities,—it sprang from the same source as his caution, his respect
for law, his instinctive sense of the dangers that threatened the
Union, his curious mixture of radical and conservative tastes; but
whatever its merits or defects, it led to a strange delusion when it
caused him to believe that a man like Napoleon could be forced by a
mere pin-prick to do Jefferson’s will.
Jefferson himself was weary of indecision. He had rested his
wish for an English alliance on the belief that Napoleon meant to
make peace in Europe in order to attack America; and this idea,
never very reasonable, could have no weight after Napoleon had
plunged into a general European war. No sooner did he receive
Madison’s letter of October 16, than he again changed his plan.
“The probability of an extensive war on the continent of Europe,
strengthening every day for some time past, is now almost certain,” he
wrote October 23 to Madison.[67] “This gives us our great
desideratum, time. In truth it places us quite at our ease. We are
certain of one year of campaigning at least, and one other year of
negotiation for their peace arrangements. Should we be now forced
into war, it is become much more questionable than it was whether we
should not pursue it unembarrassed by any alliance, and free to retire
from it whenever we can obtain our separate terms. It gives us time,
too, to make another effort for peaceable settlement. Where should
this be done? Not at Madrid, certainly. At Paris! through Armstrong, or
Armstrong and Monroe as negotiators, France as the mediator, the
price of the Floridas as the means. We need not care who gets that,
and an enlargement of the sum we had thought of may be the bait to
France, while the Guadalupe as the western boundary may be the
soother of Spain; providing for our spoliated citizens in some effectual
way. We may announce to France that determined not to ask justice of
Spain again, yet desirous of making one other effort to preserve
peace, we are willing to see whether her interposition can obtain it on
terms which we think just; that no delay, however, can be admitted;
and that in the mean time should Spain attempt to change the status
quo, we shall repel force by force, without undertaking other active
hostilities till we see what may be the issue of her interference.”
A similar letter was sent on the same day to Gallatin; and the
next day Jefferson wrote to Robert Smith, suggesting the same idea,
with some characteristic additions.[68]
Jefferson’s idea that Napoleon would require two years of war
seemed reasonable; for how could Jefferson know that Ulm had
already surrendered, that Austerlitz would be fought within six
weeks, and that peace would be restored before the new year, with
the Emperor Napoleon more terrible than ever? In truth Jefferson
only reverted to his policy of peace which he had seemed to
abandon, but to which he really clung even when most earnest for a
British alliance. His conduct in that sense was at least consistent. So
much could hardly be said for Madison, even though the President
apparently yielded to the secretary’s advice. Of all the points on
which Madison, and Monroe in obedience to his orders, had most
strongly insisted, even to the extent of offending Talleyrand, the
strongest was that under no circumstances should the Florida
negotiation be turned into a bribe to France. As late as September
30, in writing the opinion intended to guide the Cabinet, Madison
asked authority “to extinguish in the French government every hope
of turning our controversy with Spain into a French job, public or
private.”[69] The President’s suggestion of October 23 avowedly
turned the controversy with Spain into a French job, which must
inevitably become private as well as public.
Madison made no protest. He soon returned to Washington, and
there, Nov. 12, 1805, a Cabinel meeting was held, whose
proceedings were recorded by the President in a memorandum,
probably written at the moment. This memorandum closed a record,
unusually complete, of an episode illustrating better than any other
the peculiarities of Jefferson and Madison, and the traits of character
most commonly alleged as their faults.[70]
“1805, Nov. 12. Present, the four secretaries; subject, Spanish
affairs.—The extension of the war in Europe leaving us without danger
of a sudden peace, depriving us of the chance of an ally, I proposed
we should address ourselves to France, informing her it was a last
effort at amicable settlement with Spain, and offer to her, or through
her, (1) A sum of money for the rights of Spain east of Iberville, say
the Floridas; (2) To cede the part of Louisiana from the Rio Bravo to
the Guadalupe; (3) Spain to pay within a certain time spoliations under
her own flag, agreed to by the convention (which we guess to be a
hundred vessels, worth two millions), and those subsequent (worth as
much more), and to hypothecate to us for those payments the country
from Guadalupe to Rio Bravo. Armstrong to be employed. The first
was to be the exciting motive with France, to whom Spain is in arrears
for subsidies, and who will be glad also to secure us from going into
the scale of England; the second, the soothing motive with Spain,
which France would press bonâ fide, because she claimed to the Rio
Bravo; the third, to quiet our merchants. It was agreed to unanimously,
and the sum to be offered fixed not to exceed five million dollars. Mr.
Gallatin did not like purchasing Florida under an apprehension of war,
lest we should be thought in fact to purchase peace. We thought this
overweighed by taking advantage of an opportunity which might not
occur again of getting a country essential to our peace and to the
security of the commerce of the Mississippi. It was agreed that Yrujo
should be sounded through Dallas whether he is not going away, and
if not, he should be made to understand that his presence at
Washington will not be agreeable, and that his departure is expected.
Casa Calvo, Morales, and all the Spanish officers at New Orleans are
to be desired to depart, with a discretion to Claiborne to let any
friendly ones remain who will resign and become citizens, as also
women receiving pensions to remain if they choose.”
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookluna.com

You might also like