100% found this document useful (2 votes)
8 views48 pages

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

The document provides links to various eBook PDFs related to Java and C++ programming, including multiple editions of 'Starting Out with Java' and 'Data Structures and Abstractions with Java'. It encourages users to download these resources from the specified website, ebookluna.com. Additionally, it outlines the contents of the books, covering topics such as control structures, loops, and file handling.

Uploaded by

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

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

The document provides links to various eBook PDFs related to Java and C++ programming, including multiple editions of 'Starting Out with Java' and 'Data Structures and Abstractions with Java'. It encourages users to download these resources from the specified website, ebookluna.com. Additionally, it outlines the contents of the books, covering topics such as control structures, loops, and file handling.

Uploaded by

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

(eBook PDF) Starting Out with Java: From Control

Structures through Data Structures 3rd Edition


download

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

Download full version ebook from https://ebookluna.com


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

(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/

(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/

(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/

(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/
(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/

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

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

Data Structures and Abstractions with Java 5th Edition (eBook PDF)

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

(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/

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

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

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


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

Chapter 4 Loops and Files   189


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

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


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

Chapter 5 Methods   269


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

Chapter 6 A First Look at Classes   319


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

Chapter 7 Arrays and the ArrayList Class   405


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

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


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

Review Questions and Exercises   483


Programming Challenges   488

Chapter 8 A Second Look at Classes and Objects   495


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

Chapter 9 Text Processing and More


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

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


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

Chapter 10 Inheritance   613


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

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


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

Chapter 11 Exceptions and Advanced File I/O   703


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

Chapter 12 A First Look at GUI Applications   761


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

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


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

Chapter 13 Advanced GUI Applications   849


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

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


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

Chapter 14 Applets and More   917


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

Chapter 15 Creating GUI Applications with JavaFX   991


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

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


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

Chapter 16 Recursion   1041


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

Chapter 17 Sorting, Searching, and Algorithm Analysis   1069


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

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


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

Chapter 18 Generics   1121


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

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


Review Questions and Exercises   1161
Programming Challenges   1164

Chapter 19 Collections and the Stream API   1167


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

Chapter 20 Linked Lists   1237


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

Chapter 21 Stacks and Queues   1289


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

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


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

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


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

Index    1501
Contents xxi

Companion Website:
Chapter 23 Databases
Appendix A Working with Records and Random Access Files
Appendix B The ASCII/Unicode Characters
Appendix C Operator Precedence and Associativity
Appendix D Java Key Words
Appendix E Installing the JDK and JDK Documentation
Appendix F Using the javadoc Utility
Appendix G More about the Math Class
Appendix H Packages
Appendix I More about JOptionPane Dialog Boxes
Appendix J Answers to Checkpoints
Appendix K Answers to Odd-Numbered Review Questions
Appendix L Getting Started with Alice
Appendix M Configuring JavaDB
Case Study 1 Calculating Sales Commission
Case Study 2 The Amortization Class
Case Study 3 The PinTester Class
Case Study 4 Parallel Arrays
Case Study 5 The FeetInches Class
Case Study 6 The SerialNumber Class
Case Study 7 A Simple Text Editor Application
Another Random Scribd Document
with Unrelated Content
place full of beans, he stopped there, saying that it was better to be
caught than to trample on the beans, and better to be slain than to
speak; and so he was murdered by those who were pursuing him.
And in this way, also, most of his companions were slain; being in
number about forty; but that a very few did escape, among whom
were Archippus, of Tarentum, and Lysis, whom I have mentioned
before.
But Dicæarchus relates that Pythagoras died afterwards, having
escaped as far as the temple of the Muses, at Metapontum, and that
he died there of starvation, having abstained from food for forty
days. And Heraclides says, in his abridgment of the life of Satyrus,
that after he had buried Pherecydes in Delos, he returned to Italy,
and finding there a superb banquet prepared at the house of Milo, of
Crotona, he left Crotona, and went to Metapontum, and there put an
end to his life by starvation, not wishing to live any longer. But
Hermippus says, that when there was war between the people of
Agrigentum and the Syracusans, Pythagoras went out with his usual
companions, and took the part of the Agrigentines; and as they were
put to flight, he ran all round a field of beans, instead of crossing it,
and so was slain by the Syracusans; and that the rest, being about
five-and-thirty in number, were burnt at Tarentum, when they were
trying to excite a sedition in the state against the principal
magistrates.
Hermippus also relates another story about Pythagoras. For he
says that when he was in Italy, he made a subterraneous apartment,
and charged his mother to write an account of everything that took
place, marking the time of each on a tablet, and then to send them
down to him, until he came up again; and that his mother did so;
and that Pythagoras came up again after a certain time, lean, and
reduced to a skeleton; and that he came into the public assembly,
and said that he had arrived from the shades below, and then he
recited to them all that had happened during his absence. And they,
being charmed by what he told them, wept and lamented, and
believed that Pythagoras was a divine being; so that they even
entrusted their wives to him, as likely to learn some good from him;
and that they too were called Pythagoreans. And this is the story of
Hermippus.
XXII. And Pythagoras had a wife, whose name was Theano; the
daughter of Brontinus, of Crotona. But some say that she was the
wife of Brontinus, and only a pupil of Pythagoras. And he had a
daughter named Damo, as Lysis mentions in his letter to Hipparchus;
where he speaks thus of Pythagoras: “And many say that you
philosophize in public, as Pythagoras also used to do; who, when he
had entrusted his Commentaries to Damo, his daughter, charged her
to divulge them to no person out of the house. And she, though she
might have sold his discourses for much money, would not abandon
them, for she thought poverty and obedience to her father’s
injunctions more valuable than gold; and that too, though she was a
woman.”
He had also a son, named Telauges, who was the successor of his
father in his school, and who, according to some authors, was the
teacher of Empedocles. At least Hippobotus relates that Empedocles
said:—

“Telauges, noble youth, whom in due time,


Theano bore to wise Pythagoras.”

But there is no book extant, which is the work of Telauges, though


there are some extant, which are attributed to his mother Theano.
And they tell a story of her, that once, when she was asked how long
a woman ought to be absent from her husband to be pure, she said,
the moment she leaves her own husband, she is pure; but she is
never pure at all, after she leaves any one else. And she
recommended a woman, who was going to her husband, to put off
her modesty with her clothes, and when she left him, to resume it
again with her clothes; and when she was asked, “What clothes?”
she said, “Those which cause you to be called a woman.”
XXIII. Now Pythagoras, as Heraclides, the son of Sarapion, relates,
died when he was eighty years of age; according to his own account
of his age, but according to the common account, he was more than
ninety. And we have written a sportive epigram on him, which is
couched in the following terms:—

You’re not the only man who has abstained


From living food, for so likewise have we;
And who, I’d like to know did ever taste
Food while alive, most sage Pythagoras?
When meat is boil’d, or roasted well and salted,
I don’t think it can well be called living.
Which, therefore, without scruple then we eat it,
And call it no more living flesh, but meat.

And another, which runs thus:—

Pythagoras was so wise a man, that he


Never eat meat himself, and called it sin.
And yet he gave good joints of beef to others.
So that I marvel at his principles;
Who others wronged, by teaching them to do
What he believed unholy for himself.

And another, as follows:—


Should you Pythagoras’ doctrine wish to know,
Look on the centre of Euphorbus’ shield.
For he asserts there lived a man of old,
And when he had no longer an existence,
He still could say that he had been alive,
Or else he would not still be living now.

And this one too:

Alas! alas! why did Pythagoras hold


Beans in such wondrous honour? Why, besides,
Did he thus die among his choice companions?
There was a field of beans; and so the sage,
Died in the common road of Agrigentum,
Rather than trample down his favourite beans.

XXIV. And he flourished about the sixtieth olympiad; and his


system lasted for nine or ten generations. And the last of the
Pythagoreans, whom Aristoxenus knew, were Xenophilus, the
Chalcidean, from Thrace; and Phanton, the Phliasian, and
Echecrates, and Diodes, and Polymnestus, who were also Phliasians,
and they were disciples of Philolaus and Eurytus, of Tarentum.
XXV. And there were four men of the name of Pythagoras, about
the same time, at no great distance from one another. One was a
native of Crotona, a man who attained tyrannical power; the second
was a Phliasian, a trainer of wrestlers, as some say; the third was a
native of Zacynthus; the fourth was this our philosopher, to whom
they say the mysteries of philosophy belong, in whose time that
proverbial phrase, “Ipse dixit,” was introduced into ordinary life.
Some also affirm, that there was another man of the name of
Pythagoras, a statuary of Rhodes; who is believed to have been the
first discoverer of rhythm and proportion; and another was a Samian
statuary; and another an orator, of no reputation; and another was a
physician, who wrote a treatise on Squills; and also some essays on
Homer; and another was a man, who wrote a history of the affairs
of the Dorians, as we are told by Dionysius.
But Eratosthenes says, as Phavorinus quotes him, in the eighth
book of his Universal History, that this philosopher, of whom we are
speaking, was the first man who ever practised boxing in a scientific
manner, in the forty-eighth olympiad, having his hair long, and being
clothed in a purple robe; and that he was rejected from the
competition among boys, and being ridiculed for his application, he
immediately entered among the men, and came off victorious. And
this statement is confirmed among other things, by the epigram
which Theætetus composed:—

Stranger, if e’er you knew Pythagoras,


Pythagoras, the man with flowing hair,
The celebrated boxer, erst of Samos;
I am Pythagoras. And if you ask
A citizen of Elis of my deeds,
You’ll surely think he is relating fables.

Phavorinus says, that he employed definitions, on account of the


mathematical subjects to which he applied himself. And that
Socrates and those who were his pupils, did so still more; and that
they were subsequently followed in this by Aristotle and the Stoics.
He too, was the first person, who ever gave the name of κόσμος
to the universe, and the first who called the earth round; though
Theophrastus attributes this to Parmenides, and Zeno to Hesiod.
They say too, that Cylon used to be a constant adversary of his, as
Antidicus was of Socrates. And this epigram also used to be
repeated, concerning Pythagoras the athlete:—
Pythagoras of Samos, son of Crates,
Came while a child to the Olympic games,
Eager to battle for the prize in boxing.

XXVI. There is a letter of this philosopher extant, which is couched


in the following terms:—

PYTHAGORAS TO ANAXIMENES.

“You too, my most excellent friend, if you were not superior to


Pythagoras, in birth and reputation, would have migrated from
Miletus and gone elsewhere. But now the reputation of your father
keeps you back, which perhaps would have restrained me too, if I
had been like Anaximenes. But if you, who are the most eminent
man, abandon the cities, all their ornaments will be taken from
them, and the Median power will be more dangerous to them. Nor is
it always seasonable to be studying astronomy, but it is more
honourable to exhibit a regard for one’s country. And I myself am
not always occupied about speculations of my own fancy, but I am
busied also with the wars which the Italians are waging against one
another.”
But since we have now finished our account of Pythagoras, we
must also speak of the most eminent of the Pythagoreans. After
whom, we must mention those who are spoken of more
promiscuously in connection with no particular school; and then we
will connect the whole series of philosophers worth speaking of, till
we arrive at Epicurus, as we have already promised.
Now Telauges and Theano we have mentioned; and we must now
speak of Empedocles, in the first place, for, according to some
accounts, he was a pupil of Pythagoras.

LIFE OF EMPEDOCLES.
I. Empedocles, as Hippobotus relates, was the son of Meton, the
son of Empedocles, and a citizen of Agrigentum. And Timæus, in the
fifteenth book of his Histories, gives the same account, adding that
Empedocles, the grandfather of the poet, was also a most eminent
man. And Hermippus tells the same story as Timæus; and in the
same spirit Heraclides, in his treatise on Diseases, relates that he
was of an illustrious family, since his father bred a fine stud of
horses. Erastothenes, in his List of the Conquerors at the Olympic
Games, says, that the father of Meton gained the victory in the
seventy-first olympiad, quoting Aristotle as his authority for the
assertion.
But Apollodorus, the grammarian, in his Chronicles, says that he
was the son of Meton; and Glaucus says that he came to Thurii
when the city was only just completed. And then proceeding a little
further, he adds:—

And some relate that he did flee from thence,


And came to Syracuse, and on their side
Did fight in horrid war against th’ Athenians;
But those men seem to me completely wrong—
For by this time he must have been deceased,
Or very old, which is not much believed;
For Aristotle, and Heraclides too,
Say that he died at sixty years of age.

But certainly the person who got the victory with a single horse in
the seventy-first olympiad was a namesake of this man, and that it is
which deceived Apollodorus as to the age of this philosopher.
But Satyrus, in his Lives, asserts, that Empedocles was the son of
Exænetus, and that he also left a son who was named Exænetus.
And that in the same Olympiad, he himself gained the victory with
the single horse; and his son, in wrestling, or, as Heraclides says in
his Abridgment, in running. But I have found in the Commentaries of
Phavorinus, that Empedocles sacrificed, and gave as a feast to the
spectators of the games, an ox made of honey and flour, and that he
had a brother named Callicratidas.
But Telauges, the son of Pythagoras, in his letters to Philolaus,
says that Empedocles was the son of Archinomus; and that he was a
citizen of Agrigentum, he himself asserts at the beginning of his
Purifications.

Friends, who the mighty citadel inhabit,


Which crowns the golden waves of Acragas.

And this is enough to say about his family.


II. Timæus, in his ninth book, relates that he was a pupil of
Pythagoras, saying that he was afterwards convicted of having
divulged his doctrines, in the same way as Plato was, and therefore
that he was forbidden from thenceforth to attend his school. And
they say that Pythagoras himself mentions him when he says:—

And in that band there was a learned man


Of wondrous wisdom; one, who of all men
Had the profoundest wealth of intellect.

But some say that when the philosopher says this, he is referring to
Parmenides.
Neanthes relates, that till the time of Philolaus and Empedocles,
the Pythagoreans used to admit all persons indiscriminately into
their school; but when Empedocles made their doctrines public by
means of his poems, then they made a law to admit no Epic poet.
And they say that the same thing happened to Plato; for that he too
was excluded from the school. But who was the teacher of the
Pythagorean school that Empedocles was a pupil of, they do not say;
for, as for the letter of Telauges, in which he is stated to have been a
pupil of Hippasus and Brontinus, that is not worthy of belief. But
Theophrastus says that he was an imitator and a rival of
Parmenides, in his poems, for that he too had delivered his opinions
on natural philosophy in epic verse.
Hermippus, however, says that he was an imitator, not of
Parmenides, but of Xenophanes with whom he lived; and that he
imitated his epic style, and that it was at a later period that he fell in
with the Pythagoreans. But Alcidamas, in his Natural Philosophy,
says, that Zeno and Empedocles were pupils of Parmenides, about
the same time; and that they subsequently seceded from him; and
that Zeno adopted a philosophical system peculiar to himself; but
that Empedocles became a pupil of Anaxagoras and Pythagoras, and
that he imitated the pompous demeanour, and way of life, and
gestures of the one, and the system of Natural Philosophy of the
other.
III. And Aristotle, in his Sophist, says that Empedocles was the
first person who invented rhetoric, and Zeno the first person who
invented dialectics. And in his book on Poetry, he says, that
Empedocles was a man of Homeric genius, and endowed with great
power of language, and a great master of metaphor, and a man who
employed all the successful artifices of poetry, and also that when he
had written several poems, and among them one on the passage of
the Hellespont, by Xerxes, and also the proœmium of a hymn to
Apollo, his daughter subsequently burnt them, or, as Hieronymus
says, his sister, burning the proœmium unintentionally, but the
Persian poem on purpose, because it was incomplete. And speaking
generally, he says that he wrote tragedies and political treatises.
But Heraclides, the son of Sarapion, says that the tragedies were
the work of some other Empedocles; and Hieronymus says that he
had met with forty-three. Neanthes, too, affirms that when he was a
young man, he wrote tragedies, and that he himself had
subsequently met with them; and Satyrus, in his Lives, states that
he was a physician, and also a most excellent orator. And
accordingly, that Gorgias, of Leontini, was his pupil, a man of the
greatest eminence as a rhetorician, and one who left behind him a
treatise containing a complete system of the art; and who, as we are
told by Apollodorus, in his Chronicles, lived to the age of a hundred
and nine years.
IV. Satyrus tells us that he used to say that he had been present
when Empedocles was practising magic; and that he professes this
science, and many others too in his poems when he says:—
And all the drugs which can relieve disease,
Or soften the approach of age, shall be
Revealed to your inquiries; I do know them,
And I to you alone will them disclose.
You shall restrain the fierce unbridled winds,
Which, rushing o’er the earth, bow down the corn,
And crush the farmer’s hopes. And when you will,
You shall recall them back to sweep the land:
Then you shall learn to dry the rainy clouds,
And bid warm summer cheer the heart of men.
Again, at your behest, the drought shall yield
To wholesome show’rs: when you give the word
Hell shall restore its dead.

V. And Timæus, in his eighteenth book, says, that this man was
held in great esteem on many accounts; for that once, when the
etesian gales were blowing violently, so as to injure the crops, he
ordered some asses to be flayed, and some bladders to be made of
their hides, and these he placed on the hills and high places to catch
the wind. And so, when the wind ceased, he was called wind-
forbidder (κωλυσανέμας). And Heraclides, in his treatise on Diseases,
says that he dictated to Pausanias the statement which he made
about the dead woman. Now Pausanias, as both Aristippus and
Satyrus agree, was much attached to him; and he dedicated to him
the works which he wrote on Natural Philosophy, in the following
terms:—

Hear, O Pausanias, son of wise Anchites.

He also wrote an epigram upon him:—


Gela, his native land, does boast the birth
Of wise Anchites’ son, that great physician,
So fitly named Pausanias,[115] from his skill;
A genuine son of Æsculapius,
Who has stopped many men whom fell disease
Marked for its own, from treading those dark paths
Which lead to Proserpine’s infernal realms.

VI. The case of the dead woman above mentioned, Heraclides


says, was something of this sort; that he kept her corpse for thirty
days dead, and yet free from corruption; on which account he has
called himself a physician and a prophet, taking it also from these
verses:—

Friends who the mighty citadel inhabit,


Which crowns the golden waves of Acragas
Votaries of noble actions, Hail to ye;
I, an immortal God, no longer mortal,
Now live among you well revered by all,
As is my due, crowned with holy fillets
And rosy garlands. And whene’er I come
To wealthy cities, then from men and women
Due honours meets me; and crowds follow me,
Seeking the way which leads to gainful glory.
Some ask for oracles, and some entreat,
For remedies against all kinds of sickness.

VII. And he says that Agrigentum was a very large city, since it
had eight hundred thousand inhabitants; on which account
Empedocles, seeing the people immersed in luxury, said, “The men
of Agrigentum devote themselves wholly to luxury as if they were to
die to-morrow, but they furnish their houses as if they were to live
for ever.”
VIII. It is said that Cleomenes, the rhapsodist, sung this very
poem, called the Purifications, at Olympia; at least this is the
account given by Phavorinus, in his Commentaries.
IX. And Aristotle says, that he was a most liberal man, and far
removed from anything like a domineering spirit; since he constantly
refused the sovereign power when it was offered to him, as Xanthus
assures us in his account of him, showing plainly that he preferred a
simple style of living. And Timæus tells the same story, giving at the
same time the reason why he was so very popular. For he says that
when on one occasion, he was invited to a banquet by one of the
magistrates, the wine was carried about, but the supper was not
served up. And as every one else kept silence, he, disapproving of
what he saw, bade the servants bring in the supper; but the person
who had invited him said that he was waiting for the secretary of the
council. And when he came he was appointed master of the feast, at
the instigation of the giver of it, and then he gave a plain intimation
of his tyrannical inclinations, for he ordered all the guests to drink,
and those who did not drink were to have the wine poured over their
heads. Empedocles said nothing at the moment, but the next day he
summoned them before the court, and procured the execution of
both the entertainer and the master of the feast.
And this was the beginning of his political career. And at another
time, when Acron, the physician, asked of the council a place where
he might erect a monument to his father, on account of his
eminence as a physician, Empedocles came forward and opposed
any such grant, adducing many arguments on the ground of
equality, and also putting the following question:—“And what elegy
shall we inscribe upon it? Shall we say:—

“Ἄκρον ἰητρὸν Ἄκρων’ Ἀκραγαντῖνον πατρὸς ἄκρου


κρύπτει κρημνὸς ἄκρος πατρίδος ἀκροτάτης.”[116]
But some give the second line thus:—

Ἀκροτάτης κορυφῆς τύμβος ἄκρος κατέχει.

And others assert that it is the composition of Simonides.


But afterwards Empedocles abolished the assembly of a thousand,
and established a council in which the magistrates were to hold
office for three years, on such a footing that it should consist not
only of rich men, but of those who were favourers of the interests of
the people. Timæus, however, in his first and second book (for he
often mentions him), says that he appeared to entertain opinions
adverse to a republic. And, as far as his poetry goes, any one may
see that he was arrogant and self-satisfied. Accordingly, he says:—

Hail to ye,
I, an immortal God, no longer mortal,
Now live among you:

And so on.
But when he went to the olympic games he was considered a
worthy object of general attention; so that there was no mention
made of any one else in comparison of Empedocles.
X. Afterwards, indeed, when Agrigentum was settled, the
descendants of his enemies opposed his return; on which account he
retired to Peloponnesus, where he died. And Timon has not let even
Empedocles escape, but satirises him in this style, saying:—
And then Empedocles, the honeyed speaker
Of soft forensic speeches; he did take
As many offices as he was able,
Creating magistrates who wanted helpers.

But there are two accounts of the manner of his death.


XI. For Heraclides, relating the story about the dead woman, how
Empedocles got great glory from sending away a dead woman
restored to life, says that he celebrated a sacrifice in the field of
Pisianax, and that some of his friends were invited, among whom
was Pausanias. And then, after the banquet, they lay down, some
going a little way off, and some lying under the trees close by in the
field, and some wherever they happened to choose. But Empedocles
himself remained in the place where he had been sitting. But when
day broke, and they arose, he alone was not found. And when he
was sought for, and the servants were examined and said that they
did not know, one of them said, that at midnight he had heard a
loud voice calling Empedocles; and that then he himself rose up and
saw a great light from heaven, but nothing else. And as they were
all amazed at what had taken place, Pausanias descended and sent
some people to look for him; but afterwards he was commanded not
to busy himself about the matter, as he was informed that what had
happened was deserving of thankfulness, and that they behoved to
sacrifice to Empedocles as to one who had become a God.
Hermippus says also, that a woman of the name of Panthea, a
native of Agrigentum, who had been given over by the physicians,
was cured by him, and that it was on this account that he celebrated
a sacrifice; and that the guests invited were about eighty in number.
But Hippobotus says that he rose up and went away as if he were
going to mount Ætna; and that when he arrived at the crater of fire
he leaped in, and disappeared, wishing to establish a belief that he
had become a God. But afterwards the truth was detected by one of
his slippers having been dropped. For he used to wear slippers with
brazen soles. Pausanias, however, contradicts this statement.[117]
But Diodorus, of Ephesus, writing about Anaximander, says that
Empedocles imitated him; indulging in a tragic sort of pride, and
wearing magnificent apparel. And when a pestilence attacked the
people of Selinus, by reason of the bad smells arising from the
adjacent river, so that the men died and the women bore dead
children, Empedocles contrived a plan, and brought into the same
channel two other rivers at his own expense; and so, by mixing their
waters with that of the other river, he sweetened the stream. And as
the pestilence was removed in this way, when the people of Selinus
were on one occasion holding a festival on the bank of the river,
Empedocles appeared among them; and they rising up, offered him
adoration, and prayed to him as to a God. And he, wishing to
confirm this idea which they had adopted of him, leaped into the
fire.
But Timæus contradicts all these stories; saying expressly, that he
departed into Peloponnesus, and never returned at all, on which
account the manner of his death is uncertain. And he especially
denies the tale of Heraclides in his fourth book; for he says that
Pisianax was a Syracusan, and had no field in the district of
Agrigentum; but that Pausanias erected a monument in honour of
his friend, since such a report had got about concerning him; and,
as he was a rich man, made it a statue and little chapel, as one
might erect to a God. “How then,” adds Timæus, “could he have
leaped into a crater, of which, though they were in the
neighbourhood, he had never made any mention? He died then in
Peloponnesus; and there is nothing extraordinary in there being no
tomb of his to be seen; for there are many other men who have no
tomb visible.” These are the words of Timæus; and he adds further,
“But Heraclides is altogether a man fond of strange stories, and one
who would assert that a man had fallen from the moon.”
Hippobotus says, that there was a clothed statue of Empedocles
which lay formerly in Agrigentum, but which was afterwards placed
in front of the Senate House of the Romans divested of its clothing,
as the Romans had carried it off and erected it there. And there are
traces of some inscriptions or reliefs still discernible on it.
Neanthes of Cyzicus, who also wrote about the Pythagoreans,
says, that when Meton was dead, the seeds of tyrannical power
began to appear; and that then Empedocles persuaded the
Agrigentines to desist from their factious disputes, and to establish
political equality. And besides, as there were many of the female
citizens destitute of dowry, he portioned them out of his own private
fortune. And relying on these actions of his, he assumed a purple
robe and wore a golden circlet on his hand, as Phavorinus relates in
the first book of his Commentaries. He also wore slippers with
brazen soles, and a Delphian garland. His hair was let grow very
long, and he had boys to follow him; and he himself always
preserved a solemn countenance, and a uniformly grave
deportment. And he marched about in such style, that he seemed to
all the citizens, who met him and who admired his deportment, to
exhibit a sort of likeness to kingly power. And afterwards, it
happened that as on the occasion of some festival he was going in a
chariot to Messene, he was upset and broke his thigh; and he was
taken ill in consequence, and so died, at the age of seventy-seven.
And his tomb is in Megara.
But as to his age, Aristotle differs from this account of Neanthes;
for he asserts that he died at sixty years of age; others again say,
that he was a hundred and nine when he died. He flourished about
the eighty-fourth olympiad. Demetrius, of Trœzen, in his book
against the Sophists, reports that, as the lines of Homer say:—

He now, self-murdered, from a beam depends,


And his mad soul to blackest hell descends.[118]

But in the letter of Telauges, which has been mentioned before, it is


said that he slipped down through old age, and fell into the sea, and
so died.
And this is enough to say about his death.
There is also a jesting epigram of ours upon him, in our collection
of Poems in all Metres, which runs thus:—

You too, Empedocles, essayed to purge


Your body in the rapid flames, and drank
The liquid fire from the restless crater;
I say not that you threw yourself at once
Into the stream of Ætna’s fiery flood.
But seeking to conceal yourself you fell,
And so you met with unintended death.

And another:—

’Tis said the wise Empedocles did fall


Out of his chariot, and so broke his thigh:
But if he leapt into the flames of Ætna,
How could his tomb be shown in Megara?

XII. The following were some of his doctrines. He used to assert


that there were four elements, fire, water, earth, and air. And that
that is friendship by which they are united, and discord by which
they are separated. And he speaks thus on this subject:—

Bright Jove, life-giving Juno, Pluto dark,


And Nestis, who fills mortal eyes with tears.

Meaning by Jove fire, by Juno the earth, by Pluto the air, and by
Nestis water. And these things, says he, never cease alternating with
one another; inasmuch as this arrangement is perpetual.
Accordingly, he says subsequently:—

Sometimes in friendship bound they coalesce,


Sometimes they’re parted by fell discord’s hate.

And he asserts that the sun is a vast assemblage of fire, and that it
is larger than the moon. And the moon is disk-shaped; and that the
heaven itself is like crystal; and that the soul inhabits every kind of
form of animals and plants. Accordingly, he thus expresses himself.

For once I was a boy, and once a girl.


A bush, a bird, a fish who swims the sea,

XIII. His writings on Natural Philosophy and his Purifications


extend to five thousand verses; and his Medical Poem to six
hundred; and his Tragedies we have spoken of previously.

LIFE OF EPICHARMUS.
I. Epicharmus was a native of Cos, the son of Helothales; he also
was a pupil of Pythagoras. When he was three months old he was
brought to Megara, in Sicily, and from thence he came to Syracuse,
as he himself tells us in his writings. And on his statue there is the
following inscription.

As the bright sun excels the other stars,


As the sea far exceeds the river streams:
So does sage Epicharmus men surpass,
Whom hospitable Syracuse has crowned.
II. He has left behind him Commentaries in which he treats of
natural philosophy, and delivers apophthegms, and discusses
medicine. He has also added brief notes to many of his
commentaries, in which he declares plainly that he is the author of
the works.
III. He died at the age of ninety years.

LIFE OF ARCHYTAS.
I. Archytas was a native of Tarentum, and the son of Mnesagoras;
or, as Aristoxenus relates, of Histiæus.
II. He also was a Pythagorean; and he it was who saved Plato’s
life by means of a letter, when he was in danger of being put to
death by Dionysius.
III. He was a man held in very general esteem on account of his
universal virtue; and he was seven times appointed general of his
countrymen, when no one else had ever held the office for more
than one year, as the law forbade it to be held for a longer period.
IV. Plato wrote his letters to him; as he had begun the
correspondence by writing himself to Plato, which he did in the
following manner:—

ARCHYTAS TO PLATO, GREETING.

“I am very glad that you have recovered from your delicate state
of health; for you yourself have sent me word of your recovery, and
Lamiscus gives the same account. I have been much occupied with
some commentaries, and have been among the Lucanians, and have
met with the descendants of Ocellus. I have now in my possession,
and I send to you the treatises on Law, and Kingly Power, and Piety,
and the Creation of the Universe. As for the rest, I have not been
able to find them, but whenever I do find any, I will send them to
you.”
Thus wrote Archytas. And Plato sent him an answer in the
following terms:—

PLATO TO ARCHYTAS, GREETING.

“I was exceedingly glad to receive the Commentaries which came


from you, and I have admired their author in the greatest possible
degree; and he seems to us to be a man worthy of his ancient
ancestors. For they are said to have been originally natives of Myra;
and to have been among the Trojans, whom Laomedon took with
him, gallant men, as the story handed down by tradition attests. As
for my Commentaries which you ask me for, they are not yet
completed, but such as they are I send them to you. And on the
propriety of taking care of such things we are both agreed, so that I
have no need to impress anything on you on that head. Farewell.”
These then are the letters which these philosophers wrote to one
another.
V. There were four people of the name of Archytas. The first, this
man of whom we are speaking. The second was a Mytilenean, a
musician. The third wrote a treatise on Agriculture. The fourth was
an epigrammatic poet. Some writers also make mention of a fifth,
who was an architect; and there is a book on mechanics extant
which is attributed to him; which begins in this way:—
“This is what I heard from Teucer, the Carthaginian.”
And concerning the musician, the following story is told: That once
he was reproached for not making himself heard, and he replied,
“My organ contends on my behalf, and speaks.”
VI. Aristoxenus says, that this Pythagorean was never once
defeated while acting as general. But that as he was attacked by
envy, he once gave up his command, and his army was immediately
taken prisoner.
VII. He was the first person who applied mathematical principles
to mechanics, and reduced them to a system; and the first also who
gave a methodical impulse to descriptive geometry in seeking, in the
sections of a demicylinder for a proportional mean, which should
enable him to find the double of a given cube. He was also the first
person who ever gave the geometrical measure of a cube, as Plato
mentions in his Republic.

LIFE OF ALCMÆON.
I. Alcmæon was a citizen of Crotona; he also was a pupil of
Pythagoras. And the chief part of his writings are on medical
subjects; but he also at times discusses points of natural philosophy,
and asserts that the greater part of human affairs have two sides.
He appears to have been the first person who wrote a treatise on
Natural Philosophy, as Phavorinus affirms, in his Universal History;
and he used to argue that the moon had the same nature for ever
which she had at that moment.
II. He was the son of Pirithus, as he himself states at the
beginning of his treatise, where he says, “Alcmæon, of Crotona, the
son of Pirithus, says this to Brontinus, and Leon, and Bathyllus.
About things invisible, and things mortal, the Gods alone have a
certain knowledge; but men may form conjectures.…” And so on.
He used also to say that the soul was immortal, and that it was in
a state of perpetual motion in the same way as the sun.

LIFE OF HIPPASUS.
I. Hippasus was a citizen of Metapontum, and a pupil of
Pythagoras.
II. He used to say that the time of the changes of the world was
definite, and that the universe also was finite, and in a state of
perpetual motion.
III. Demetrius, in his treatise on People of the same Name, says
that he left no writings behind him.
IV. There were two people of the name of Hippasus; this man, and
another who wrote an account of the Constitution of the
Lacedæmonians, in five books. And he was himself a
Lacedæmonian.

LIFE OF PHILOLAUS.
I. Philolaus was a native of Crotona, and a pupil of Pythagoras, it
was from him that Plato wrote to Dion to take care and purchase the
books of Pythagoras.
II. And he died under suspicion of having designed to seize on the
tyranny; and we have written an epigram on him:—

I say that all men ought above all things


To guard against suspicion. For, though innocent,
Still if you are suspected, you’re unfortunate.
And thus his native city of Crotona
Slew Philolaus; for the jealous citizens
Thought that his house betrayed a tyrant’s purpose.

III. His theory was, that everything was produced by harmony and
necessity. And he was the first person who affirmed that the earth
moved in a circle; though some attribute the assertion of this
principle to Icetas of Syracuse.
IV. He wrote one book, which Hermippus reports, on the authority
of some unknown writer, that Plato the philosopher purchased when
he was in Sicily (having come thither to the court of Dionysius), of
the relations of Philolaus, for forty Alexandrian minæ of silver; and
that from this book he copied his Timæus. But others say that Plato
received it as a present, after having obtained his liberty for a young
man, one of the disciples of Philolaus, who had been arrested by
Dionysius. Demetrius, in his treatise on people of the same name,
says that he was the first of the Pythagoreans who wrote a treatise
on Natural Philosophy; and it begins thus:—
“But nature in the world has been composed of bodies infinite and
finite, and so is the whole world and all that is in it.”

LIFE OF EUDOXUS.
I. Eudoxus was the son of Æschines, and a native of Cnidus. He
was an astronomer, a geometrician, a physician, and a lawgiver. In
geometry he was a pupil of Archytas, and in medicine of Philistion,
the Sicilian, as Callimachus relates in his Tablets; and Sotion, in his
Successions, asserts that he was likewise a pupil of Plato; for that,
when he was twenty-three years of age, and in very narrow
circumstances, he came to Athens with Theomedon the physician,
by whom he was chiefly supported, being attracted by the reputation
of the Socratic school. Some say that his attachment to Theomedon
was cemented by nearer ties. And when he had arrived at Piræus,
he went up to the city every day, and when he had heard the
Sophists lecture he returned. And having spent two months there,
he returned home again; and being again aided by the contributions
of his friends, he set sail for Egypt, with Chrysippus the physician,
bearing letters of introduction from Agesilaus to Nectanabis, and
that he recommended him to the priests.
II. And having remained there a year and four months, he shaved
his eyebrows after the manner of the Egyptian priests, and
composed, as it is said, the treatise called the Octaeteris. From
thence he went to Cyzicus, and to the Propontis, in both of which
places he lived as a Sophist; he also went to the court of Mausolus.
And then, in this manner, he returned again to Athens, having a
great many disciples with him, for the sake, as some say, of
annoying Plato, because he had originally discarded him from his
school. Some say, that when Plato gave an entertainment on one
occasion, Eudoxus, as the guests were very numerous, introduced
the fashion of sitting in a semicircle.
Nicomachus, the son of Aristotle, affirms that he used to say, that
pleasure was the good.
III. He was received in his own country with great honours, as the
decree that was passed respecting him shows. He was also
accounted very illustrious among the Greeks, having given laws to
his own fellow citizens, as Hermippus tells us in the fourth book of
his account of the Seven Wise Men; and having also written treatises
on Astronomy and Geometry, and several other considerable works.
He had three daughters, Actis, Philtis, and Delphis. And
Eratosthenes asserts, in his books addressed to Baton, that he also
composed dialogues entitled Dialogues of Dogs; others say that
these were written by some Egyptians, in their own language, and
that Eudoxus translated them, and published them in Greece. One of
his pupils was Chrysippus, of Cnidos, son of Erineus, who learnt of
him all that he knew about the Gods, and the world, and the
heavenly bodies; and who learnt medicine from Philistion the
Sicilian. He also left some very admirable Reminiscences.
IV. He had a son of the name of Aristagoras, who was the teacher
of Chrysippus, the son of Aëthlius; he was the author of a work on
Remedies for the Eyes, as speculations on natural philosophy had
come very much under his notice.
V. There were three people of the name of Eudoxus. The first, this
man of whom we are speaking; the second, a Rhodian, who wrote
histories; the third, a Siciliot, a son of Agathocles, a comic poet, who
gained three victories at the Dionysia in the city, and five at the
Lenæa,[119] as Apollodorus tells us in his Chronicles. We also find
another, who was a physician of Cnidos, who is mentioned by this
Eudoxus, in his Circuit of the World, where he says that he used to
warn people to keep constantly exercising their limbs in every kind
of exercise, and their senses too.
VI. The same author says, that the Cnidean Eudoxus flourished
about the hundred and third olympiad; and that he was the inventor
of the theory of crooked lines. And he died in his fifty-third year. But
when he was in Egypt with Conuphis, of Heliopolis, Apis licked his
garment; and so the priests said that he would be short-lived, but
very illustrious, as it is reported by Phavorinus in his Commentaries.
And we have written an epigram on him, that runs thus:—

’Tis said, that while at Memphis wise Eudoxus


Learnt his own fate from th’ holy fair-horned bull;
He said indeed no word, bulls do not speak
Nor had kind nature e’er calf Apis gifted
With an articulately speaking mouth.
But standing on one side he lick’d his cloak,
Showing by this most plainly—in brief time
You shall put off your life. So death came soon,
When he had just seen three and fifty times
The Pleiads rise to warn the mariners.

And instead of Eudoxus, they used to call him Endoxus,[120] on


account of the brilliancy of his reputation. And since we have gone
through the illustrious Pythagoreans, we must now speak of the
Promiscuous philosophers, as they call them. And we will first of all
speak of Heraclitus.
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