0% found this document useful (0 votes)
3 views

Think Java How to Think Like a Computer Scientist 1st Edition Allen B. Downey instant download

The document provides a link to download the ebook 'Think Java: How to Think Like a Computer Scientist' by Allen B. Downey. It includes various other educational resources and ebooks by the same author and others. The content covers fundamental programming concepts and is structured into chapters addressing different aspects of computer science.

Uploaded by

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

Think Java How to Think Like a Computer Scientist 1st Edition Allen B. Downey instant download

The document provides a link to download the ebook 'Think Java: How to Think Like a Computer Scientist' by Allen B. Downey. It includes various other educational resources and ebooks by the same author and others. The content covers fundamental programming concepts and is structured into chapters addressing different aspects of computer science.

Uploaded by

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

Think Java How to Think Like a Computer

Scientist 1st Edition Allen B. Downey pdf


download

https://ebookname.com/product/think-java-how-to-think-like-a-
computer-scientist-1st-edition-allen-b-downey/

Get Instant Ebook Downloads – Browse at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Think Python 1st Edition Allen B. Downey

https://ebookname.com/product/think-python-1st-edition-allen-b-
downey/

Think Stats Exploratory Data Analysis Second Edition


Allen B. Downey

https://ebookname.com/product/think-stats-exploratory-data-
analysis-second-edition-allen-b-downey/

How to Think Like a Radiologist Ordering Imaging


Studies 1st Edition Tara Marie Catanzano

https://ebookname.com/product/how-to-think-like-a-radiologist-
ordering-imaging-studies-1st-edition-tara-marie-catanzano/

Practical Child and Adolescent Psychopharmacology 1st


Edition Stan Kutcher

https://ebookname.com/product/practical-child-and-adolescent-
psychopharmacology-1st-edition-stan-kutcher/
Encyclopedia of Juvenile Violence 1st Edition Laura L.
Finley

https://ebookname.com/product/encyclopedia-of-juvenile-
violence-1st-edition-laura-l-finley/

Dictionary of Human Rights A 2nd Edition David


Robertson

https://ebookname.com/product/dictionary-of-human-rights-a-2nd-
edition-david-robertson/

Zen s Chinese Heritage The Masters and Their Teachings


Andrew Ferguson

https://ebookname.com/product/zen-s-chinese-heritage-the-masters-
and-their-teachings-andrew-ferguson/

Multimedia over Cognitive Radio Networks Algorithms


Protocols and Experiments 1st Edition Fei Hu (Author)

https://ebookname.com/product/multimedia-over-cognitive-radio-
networks-algorithms-protocols-and-experiments-1st-edition-fei-hu-
author/

Visual Data Mining Techniques and Tools for Data


Visualization and Mining 1st Edition Tom Soukup

https://ebookname.com/product/visual-data-mining-techniques-and-
tools-for-data-visualization-and-mining-1st-edition-tom-soukup/
Remote Sensing of Glaciers Techniques for Topographic
Spatial and Thematic Mapping of Glaciers 1st Edition
Petri Pellikka

https://ebookname.com/product/remote-sensing-of-glaciers-
techniques-for-topographic-spatial-and-thematic-mapping-of-
glaciers-1st-edition-petri-pellikka/
Think Java
How to Think Like a Computer Scientist

Version 6.1.3
Think Java
How to Think Like a Computer Scientist

Version 6.1.3

Allen B. Downey and Chris Mayfield

Green Tea Press


Needham, Massachusetts
Copyright © 2016 Allen B. Downey and Chris Mayfield.

Green Tea Press


9 Washburn Ave
Needham, MA 02492

Permission is granted to copy, distribute, and/or modify this work under


the terms of the Creative Commons Attribution-NonCommercial-ShareAlike
3.0 Unported License, which is available at http://creativecommons.org/
licenses/by-nc-sa/3.0/.

The original form of this book is LATEX source code. Compiling this code has
the effect of generating a device-independent representation of the book, which
can be converted to other formats and printed.

The LATEX source for this book is available from http://thinkjava.org.


Contents

Preface xiii

1 The way of the program 1


1.1 What is programming? . . . . . . . . . . . . . . . . . . . . . 1
1.2 What is computer science? . . . . . . . . . . . . . . . . . . . 2
1.3 Programming languages . . . . . . . . . . . . . . . . . . . . 3
1.4 The hello world program . . . . . . . . . . . . . . . . . . . . 4
1.5 Displaying strings . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Escape sequences . . . . . . . . . . . . . . . . . . . . . . . . 7
1.7 Formatting code . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.8 Debugging code . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.9 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

2 Variables and operators 15


2.1 Declaring variables . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3 State diagrams . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Printing variables . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5 Arithmetic operators . . . . . . . . . . . . . . . . . . . . . . 19
vi CONTENTS

2.6 Floating-point numbers . . . . . . . . . . . . . . . . . . . . . 21


2.7 Rounding errors . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.8 Operators for strings . . . . . . . . . . . . . . . . . . . . . . 23
2.9 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.10 Types of errors . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

3 Input and output 33


3.1 The System class . . . . . . . . . . . . . . . . . . . . . . . . 33
3.2 The Scanner class . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3 Program structure . . . . . . . . . . . . . . . . . . . . . . . . 36
3.4 Inches to centimeters . . . . . . . . . . . . . . . . . . . . . . 37
3.5 Literals and constants . . . . . . . . . . . . . . . . . . . . . . 38
3.6 Formatting output . . . . . . . . . . . . . . . . . . . . . . . 38
3.7 Centimeters to inches . . . . . . . . . . . . . . . . . . . . . . 40
3.8 Modulus operator . . . . . . . . . . . . . . . . . . . . . . . . 41
3.9 Putting it all together . . . . . . . . . . . . . . . . . . . . . . 41
3.10 The Scanner bug . . . . . . . . . . . . . . . . . . . . . . . . 43
3.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

4 Void methods 49
4.1 Math methods . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.2 Composition revisited . . . . . . . . . . . . . . . . . . . . . . 50
4.3 Adding new methods . . . . . . . . . . . . . . . . . . . . . . 51
4.4 Flow of execution . . . . . . . . . . . . . . . . . . . . . . . . 54
CONTENTS vii

4.5 Parameters and arguments . . . . . . . . . . . . . . . . . . . 55


4.6 Multiple parameters . . . . . . . . . . . . . . . . . . . . . . . 56
4.7 Stack diagrams . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.8 Reading documentation . . . . . . . . . . . . . . . . . . . . . 59
4.9 Writing documentation . . . . . . . . . . . . . . . . . . . . . 61
4.10 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

5 Conditionals and logic 67


5.1 Relational operators . . . . . . . . . . . . . . . . . . . . . . . 67
5.2 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . 68
5.3 Conditional statements . . . . . . . . . . . . . . . . . . . . . 69
5.4 Chaining and nesting . . . . . . . . . . . . . . . . . . . . . . 71
5.5 Flag variables . . . . . . . . . . . . . . . . . . . . . . . . . . 72
5.6 The return statement . . . . . . . . . . . . . . . . . . . . . . 72
5.7 Validating input . . . . . . . . . . . . . . . . . . . . . . . . . 73
5.8 Recursive methods . . . . . . . . . . . . . . . . . . . . . . . 74
5.9 Recursive stack diagrams . . . . . . . . . . . . . . . . . . . . 76
5.10 Binary numbers . . . . . . . . . . . . . . . . . . . . . . . . . 77
5.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

6 Value methods 85
6.1 Return values . . . . . . . . . . . . . . . . . . . . . . . . . . 85
6.2 Writing methods . . . . . . . . . . . . . . . . . . . . . . . . 88
6.3 Method composition . . . . . . . . . . . . . . . . . . . . . . 90
6.4 Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
viii CONTENTS

6.5 Boolean methods . . . . . . . . . . . . . . . . . . . . . . . . 93


6.6 Javadoc tags . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
6.7 More recursion . . . . . . . . . . . . . . . . . . . . . . . . . . 95
6.8 Leap of faith . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
6.9 One more example . . . . . . . . . . . . . . . . . . . . . . . 98
6.10 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
6.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

7 Loops 105
7.1 The while statement . . . . . . . . . . . . . . . . . . . . . . 105
7.2 Generating tables . . . . . . . . . . . . . . . . . . . . . . . . 107
7.3 Encapsulation and generalization . . . . . . . . . . . . . . . 109
7.4 More generalization . . . . . . . . . . . . . . . . . . . . . . . 112
7.5 The for statement . . . . . . . . . . . . . . . . . . . . . . . . 114
7.6 The do-while loop . . . . . . . . . . . . . . . . . . . . . . . . 115
7.7 Break and continue . . . . . . . . . . . . . . . . . . . . . . . 116
7.8 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
7.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

8 Arrays 123
8.1 Creating arrays . . . . . . . . . . . . . . . . . . . . . . . . . 123
8.2 Accessing elements . . . . . . . . . . . . . . . . . . . . . . . 124
8.3 Displaying arrays . . . . . . . . . . . . . . . . . . . . . . . . 125
8.4 Copying arrays . . . . . . . . . . . . . . . . . . . . . . . . . 127
8.5 Array length . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
8.6 Array traversal . . . . . . . . . . . . . . . . . . . . . . . . . 128
8.7 Random numbers . . . . . . . . . . . . . . . . . . . . . . . . 129
CONTENTS ix

8.8 Traverse and count . . . . . . . . . . . . . . . . . . . . . . . 131


8.9 Building a histogram . . . . . . . . . . . . . . . . . . . . . . 131
8.10 The enhanced for loop . . . . . . . . . . . . . . . . . . . . . 133
8.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
8.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

9 Strings and things 139


9.1 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
9.2 Strings are immutable . . . . . . . . . . . . . . . . . . . . . 141
9.3 String traversal . . . . . . . . . . . . . . . . . . . . . . . . . 141
9.4 Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
9.5 The indexOf method . . . . . . . . . . . . . . . . . . . . . . 144
9.6 String comparison . . . . . . . . . . . . . . . . . . . . . . . . 144
9.7 String formatting . . . . . . . . . . . . . . . . . . . . . . . . 145
9.8 Wrapper classes . . . . . . . . . . . . . . . . . . . . . . . . . 146
9.9 Command-line arguments . . . . . . . . . . . . . . . . . . . . 147
9.10 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
9.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

10 Objects 155
10.1 Point objects . . . . . . . . . . . . . . . . . . . . . . . . . . 155
10.2 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
10.3 Objects as parameters . . . . . . . . . . . . . . . . . . . . . 157
10.4 Objects as return types . . . . . . . . . . . . . . . . . . . . . 158
10.5 Mutable objects . . . . . . . . . . . . . . . . . . . . . . . . . 159
10.6 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
10.7 The null keyword . . . . . . . . . . . . . . . . . . . . . . . . 161
x CONTENTS

10.8 Garbage collection . . . . . . . . . . . . . . . . . . . . . . . 162


10.9 Class diagrams . . . . . . . . . . . . . . . . . . . . . . . . . 162
10.10 Java library source . . . . . . . . . . . . . . . . . . . . . . . 163
10.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
10.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

11 Classes 171
11.1 The Time class . . . . . . . . . . . . . . . . . . . . . . . . . 172
11.2 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
11.3 More constructors . . . . . . . . . . . . . . . . . . . . . . . . 174
11.4 Getters and setters . . . . . . . . . . . . . . . . . . . . . . . 176
11.5 Displaying objects . . . . . . . . . . . . . . . . . . . . . . . . 178
11.6 The toString method . . . . . . . . . . . . . . . . . . . . . . 179
11.7 The equals method . . . . . . . . . . . . . . . . . . . . . . . 180
11.8 Adding times . . . . . . . . . . . . . . . . . . . . . . . . . . 181
11.9 Pure methods and modifiers . . . . . . . . . . . . . . . . . . 183
11.10 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
11.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

12 Arrays of objects 189


12.1 Card objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
12.2 Card toString . . . . . . . . . . . . . . . . . . . . . . . . . . 191
12.3 Class variables . . . . . . . . . . . . . . . . . . . . . . . . . . 193
12.4 The compareTo method . . . . . . . . . . . . . . . . . . . . 194
12.5 Cards are immutable . . . . . . . . . . . . . . . . . . . . . . 195
12.6 Arrays of cards . . . . . . . . . . . . . . . . . . . . . . . . . 196
12.7 Sequential search . . . . . . . . . . . . . . . . . . . . . . . . 198
CONTENTS xi

12.8 Binary search . . . . . . . . . . . . . . . . . . . . . . . . . . 199


12.9 Tracing the code . . . . . . . . . . . . . . . . . . . . . . . . 201
12.10 Recursive version . . . . . . . . . . . . . . . . . . . . . . . . 202
12.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
12.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

13 Objects of arrays 205


13.1 The Deck class . . . . . . . . . . . . . . . . . . . . . . . . . 205
13.2 Shuffling decks . . . . . . . . . . . . . . . . . . . . . . . . . . 207
13.3 Selection sort . . . . . . . . . . . . . . . . . . . . . . . . . . 208
13.4 Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
13.5 Subdecks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
13.6 Merging decks . . . . . . . . . . . . . . . . . . . . . . . . . . 210
13.7 Adding recursion . . . . . . . . . . . . . . . . . . . . . . . . 211
13.8 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
13.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

14 Objects of objects 215


14.1 Decks and hands . . . . . . . . . . . . . . . . . . . . . . . . 216
14.2 CardCollection . . . . . . . . . . . . . . . . . . . . . . . . . 216
14.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
14.4 Dealing cards . . . . . . . . . . . . . . . . . . . . . . . . . . 221
14.5 The Player class . . . . . . . . . . . . . . . . . . . . . . . . . 223
14.6 The Eights class . . . . . . . . . . . . . . . . . . . . . . . . . 226
14.7 Class relationships . . . . . . . . . . . . . . . . . . . . . . . 230
14.8 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
14.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
xii CONTENTS

A Development tools 233


A.1 Installing DrJava . . . . . . . . . . . . . . . . . . . . . . . . 233
A.2 DrJava interactions . . . . . . . . . . . . . . . . . . . . . . . 235
A.3 Command-line interface . . . . . . . . . . . . . . . . . . . . . 236
A.4 Command-line testing . . . . . . . . . . . . . . . . . . . . . . 237
A.5 Running Checkstyle . . . . . . . . . . . . . . . . . . . . . . . 239
A.6 Tracing with a debugger . . . . . . . . . . . . . . . . . . . . 240
A.7 Testing with JUnit . . . . . . . . . . . . . . . . . . . . . . . 241
A.8 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

B Java 2D graphics 245


B.1 Creating graphics . . . . . . . . . . . . . . . . . . . . . . . . 245
B.2 Graphics methods . . . . . . . . . . . . . . . . . . . . . . . . 246
B.3 Example drawing . . . . . . . . . . . . . . . . . . . . . . . . 248
B.4 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
B.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

C Debugging 253
C.1 Compile-time errors . . . . . . . . . . . . . . . . . . . . . . . 253
C.2 Run-time errors . . . . . . . . . . . . . . . . . . . . . . . . . 257
C.3 Logic errors . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

Index 267
Preface

Think Java is an introduction to computer science and programming intended


for readers with little or no experience. We start with the most basic concepts
and are careful to define all terms when they are first used. The book presents
each new idea in a logical progression. Larger topics, like recursion and object-
oriented programming, are divided into smaller examples and introduced over
the course of several chapters.

This book is intentionally concise. Each chapter is 12–14 pages and covers the
material for one week of a college course. It is not meant to be a comprehensive
presentation of Java, but rather, an initial exposure to programming constructs
and techniques. We begin with small problems and basic algorithms and work
up to object-oriented design. In the vocabulary of computer science pedagogy,
this book uses the “objects late” approach.

The philosophy behind the book


Here are the guiding principles that make the book the way it is:

ˆ One concept at a time. We break down topics that give beginners trouble
into a series of small steps, so that they can exercise each new concept
in isolation before continuing.

ˆ Balance of Java and concepts. The book is not primarily about Java;
it uses code examples to demonstrate computer science. Most chapters
start with language features and end with concepts.
xiv PREFACE

ˆ Conciseness. An important goal of the book is to be small enough so


that students can read and understand the entire text in a one-semester
college or AP course.

ˆ Emphasis on vocabulary. We try to introduce the minimum number


of terms and define them carefully when they are first used. We also
organize them in glossaries at the end of each chapter.

ˆ Program development. There are many strategies for writing programs,


including bottom-up, top-down, and others. We demonstrate multiple
program development techniques, allowing readers to choose methods
that work best for them.

ˆ Multiple learning curves. To write a program, you have to understand


the algorithm, know the programming language, and be able to debug
errors. We discuss these and other aspects throughout the book, and
include an appendix that summarizes our advice.

Object-oriented programming
Some Java books introduce classes and objects immediately; others begin with
procedural programming and transition to object-oriented more gradually.

Many of Java’s object-oriented features are motivated by problems with previ-


ous languages, and their implementations are influenced by this history. Some
of these features are hard to explain when people aren’t familiar with the
problems they solve.

We get to object-oriented programming as quickly as possible, limited by the


requirement that we introduce concepts one at a time, as clearly as possible,
in a way that allows readers to practice each idea in isolation before moving
on. So it takes some time to get there.

But you can’t write Java programs (even hello world) without encountering
object-oriented features. In some cases we explain a feature briefly when it
first appears, and then explain it more deeply later on.

This book is well suited to prepare students for the AP Computer Science
A exam, which includes object-oriented design and implementation. (AP is
PREFACE xv

a registered trademark of the College Board.) We introduce nearly every


topic in the “AP Java subset” with a few exceptions. A mapping of Think
Java section numbers to the current AP course description is available on our
website: http://thinkjava.org.

Appendixes
The chapters of this book are meant to be read in order, because each one
builds on the previous one. We also include three appendixes with material
that can be read at any time:
Appendix 1: Development tools
The steps for compiling, running, and debugging Java code depend on
the details of the development environment and operating system. We
avoided putting these details in the main text, because they can be
distracting. Instead, we provide this appendix with a brief introduction
to DrJava – an interactive development environment (IDE) that is helpful
for beginners – and other development tools, including Checkstyle for
code quality and JUnit for testing.
Appendix 2: Java 2D graphics
Java provides libraries for working with graphics and animation, and
these topics can be engaging for students. The libraries require object-
oriented features that readers will not completely understand until after
Chapter 11, but they can be used much earlier.
Appendix 3: Debugging
We provide debugging suggestions throughout the book, but we also col-
lect our debugging advice in an appendix. We recommend that readers
review this appendix several times as they work through the book.

Using the code examples


Most of the code examples in this book are available from a Git repository at
https://github.com/AllenDowney/ThinkJavaCode. Git is a “version con-
trol system” that allows you to keep track of the files that make up a project.
A collection of files under Git’s control is called a “repository”.
xvi PREFACE

GitHub is a hosting service that provides storage for Git repositories and a
convenient web interface. It provides several ways to work with the code:

ˆ You can create a copy of the repository on GitHub by pressing the Fork
button. If you don’t already have a GitHub account, you’ll need to
create one. After forking, you’ll have your own repository on GitHub
that you can use to keep track of code you write. Then you can “clone”
the repository, which downloads a copy of the files to your computer.

ˆ Alternatively, you could clone the repository without forking. If you


choose this option, you don’t need a GitHub account, but you won’t be
able to save your changes on GitHub.

ˆ If you don’t want to use Git at all, you can download the code in a ZIP
archive using the Download ZIP button on the GitHub page, or this link:
http://tinyurl.com/ThinkJavaCodeZip.

After you clone the repository or unzip the ZIP file, you should have a directory
called ThinkJavaCode with a subdirectory for each chapter in the book.

All examples in this book were developed and tested using Java SE Develop-
ment Kit 8. If you are using a more recent version, the examples in this book
should still work. If you are using an older version, some of them may not.

Contributors over the years


Many people have sent corrections and suggestions, and we appreciate their
valuable feedback!

ˆ Ellen Hildreth used this book to teach Data Structures at Wellesley


College and submitted a whole stack of corrections, along with some
great suggestions.

ˆ Tania Passfield pointed out that some glossaries had leftover terms that
no longer appeared in the text.

ˆ Elizabeth Wiethoff noticed that the series expansion of exp(−x2 ) was


wrong. She has also worked on a Ruby version of the book.
PREFACE xvii

ˆ Matt Crawford sent in a whole patch file full of corrections.

ˆ Chi-Yu Li pointed out a typo and an error in one of the code examples.

ˆ Doan Thanh Nam corrected an example.

ˆ Muhammad Saied translated the book into Arabic, and found several
errors in the process.

ˆ Marius Margowski found an inconsistency in a code example.

ˆ Leslie Klein discovered another error in the series expansion of exp(−x2 ),


identified typos in the card array figures, and gave helpful suggestions
to clarify several exercises.

ˆ Micah Lindstrom reported half a dozen typos and sent corrections.

ˆ James Riely ported the textbook source from LaTeX to Sphinx.


http://fpl.cs.depaul.edu/jriely/thinkapjava/

ˆ Peter Knaggs ported the book to C#.


http://www.rigwit.co.uk/think/sharp/

ˆ Heidi Gentry-Kolen recorded several video lectures that follow the book.
https://www.youtube.com/user/digipipeline

We are especially grateful to our technical reviewers: Blythe Samuels, David


Wisneski, and Stephen Rose. They found errors, made many great suggestions,
and helped make the book much better.

Additional contributors who found one or more typos: Stijn Debrouwere, Guy
Driesen, Andai Velican, Chris Kuszmaul, Daniel Kurikesu, Josh Donath, Rens
Findhammer, Elisa Abedrapo, Yousef BaAfif, Bruce Hill, Matt Underwood,
Isaac Sultan, Dan Rice, Robert Beard, Daniel Pierce, Michael Giftthaler, and
Min Zeng.

If you have additional comments or ideas about the text, please send them to:
[email protected].
xviii PREFACE
Chapter 1

The way of the program

The goal of this book is to teach you to think like a computer scientist. This
way of thinking combines some of the best features of mathematics, engineer-
ing, and natural science. Like mathematicians, computer scientists use formal
languages to denote ideas, specifically computations. Like engineers, they
design things, assembling components into systems and evaluating trade-offs
among alternatives. And like scientists, they observe the behavior of complex
systems, form hypotheses, and test predictions.

The single most important skill for a computer scientist is problem solving.
It involves the ability to formulate problems, think creatively about solutions,
and express solutions clearly and accurately. As it turns out, the process of
learning to program is an excellent opportunity to develop problem solving
skills. That’s why this chapter is called, “The way of the program”.

On one level you will be learning to program, a useful skill by itself. But on
another level you will use programming as a means to an end. As we go along,
that end will become clearer.

1.1 What is programming?


A program is a sequence of instructions that specifies how to perform a
computation. The computation might be something mathematical, like solving
2 Chapter 1 The way of the program

a system of equations or finding the roots of a polynomial. It can also be


a symbolic computation, like searching and replacing text in a document or
(strangely enough) compiling a program. The details look different in different
languages, but a few basic instructions appear in just about every language.

input: Get data from the keyboard, a file, a sensor, or some other device.

output: Display data on the screen, or send data to a file or other device.

math: Perform basic mathematical operations like addition and division.

decisions: Check for certain conditions and execute the appropriate code.

repetition: Perform some action repeatedly, usually with some variation.

Believe it or not, that’s pretty much all there is to it. Every program you’ve
ever used, no matter how complicated, is made up of small instructions that
look much like these. So you can think of programming as the process of
breaking down a large, complex task into smaller and smaller subtasks. The
process continues until the subtasks are simple enough to be performed with
the basic instructions provided by the computer.

1.2 What is computer science?


One of the most interesting aspects of writing programs is deciding how to
solve a particular problem, especially when there are multiple solutions. For
example, there are numerous ways to sort a list of numbers, and each way has
its advantages. In order to determine which way is best for a given situation,
we need techniques for describing and analyzing solutions formally.

Computer science is the science of algorithms, including their discovery and


analysis. An algorithm is a sequence of steps that specifies how to solve a
problem. Some algorithms are faster than others, and some use less space
in computer memory. As you learn to develop algorithms for problems you
haven’t solved before, you also learn to think like a computer scientist.

Designing algorithms and writing code is difficult and error-prone. For histor-
ical reasons, programming errors are called bugs, and the process of tracking
1.3 Programming languages 3

them down and correcting them is called debugging. As you learn to debug
your programs, you will develop new problem solving skills. You will need to
think creatively when unexpected errors happen.

Although it can be frustrating, debugging is an intellectually rich, challenging,


and interesting part of computer programming. In some ways, debugging is
like detective work. You are confronted with clues, and you have to infer the
processes and events that led to the results you see. Thinking about how to
correct programs and improve their performance sometimes even leads to the
discovery of new algorithms.

1.3 Programming languages


The programming language you will learn is Java, which is a high-level lan-
guage. Other high-level languages you may have heard of include Python, C
and C++, Ruby, and JavaScript.

Before they can run, programs in high-level languages have to be translated


into a low-level language, also called “machine language”. This translation
takes some time, which is a small disadvantage of high-level languages. But
high-level languages have two advantages:

ˆ It is much easier to program in a high-level language. Programs take


less time to write, they are shorter and easier to read, and they are more
likely to be correct.

ˆ High-level languages are portable, meaning they can run on different


kinds of computers with few or no modifications. Low-level programs
can only run on one kind of computer, and have to be rewritten to run
on another.

Two kinds of programs translate high-level languages into low-level languages:


interpreters and compilers. An interpreter reads a high-level program and
executes it, meaning that it does what the program says. It processes the pro-
gram a little at a time, alternately reading lines and performing computations.
Figure 1.1 shows the structure of an interpreter.

In contrast, a compiler reads the entire program and translates it completely


before the program starts running. In this context, the high-level program
4 Chapter 1 The way of the program

Figure 1.1: How interpreted languages are executed.

is called the source code, and the translated program is called the object
code or the executable. Once a program is compiled, you can execute it
repeatedly without further translation. As a result, compiled programs often
run faster than interpreted programs.

Java is both compiled and interpreted. Instead of translating programs directly


into machine language, the Java compiler generates byte code. Similar to
machine language, byte code is easy and fast to interpret. But it is also
portable, so it is possible to compile a Java program on one machine, transfer
the byte code to another machine, and run the byte code on the other machine.
The interpreter that runs byte code is called a “Java Virtual Machine” (JVM).

Figure 1.2: The process of compiling and running a Java program.

Figure 1.2 shows the steps of this process. Although it might seem complicated,
these steps are automated for you in most program development environments.
Usually you only have to press a button or type a single command to compile
and run your program. On the other hand, it is important to know what steps
are happening in the background, so if something goes wrong you can figure
out what it is.

1.4 The hello world program


Traditionally, the first program you write when learning a new programming
language is called the hello world program. All it does is display the words
“Hello, World!” on the screen. In Java, it looks like this:
1.4 The hello world program 5

public class Hello {

public static void main(String[] args) {


// generate some simple output
System.out.println("Hello, World!");
}
}

When this program runs it displays:

Hello, World!

Notice that the output does not include the quotation marks.

Java programs are made up of class and method definitions, and methods are
made up of statements. A statement is a line of code that performs a basic
operation. In the hello world program, this line is a print statement that
displays a message on the screen:

System.out.println("Hello, World!");

System.out.println displays results on the screen; the name println stands


for “print line”. Confusingly, print can mean both “display on the screen” and
“send to the printer”. In this book, we’ll try to say “display” when we mean
output to the screen. Like most statements, the print statement ends with a
semicolon (;).

Java is “case-sensitive”, which means that uppercase and lowercase are not the
same. In this example, System has to begin with an uppercase letter; system
and SYSTEM won’t work.

A method is a named sequence of statements. This program defines one


method named main:

public static void main(String[] args)

The name and format of main is special: when the program runs, it starts at
the first statement in main and ends when it finishes the last statement. Later,
we will see programs that define more than one method.

A class is a collection of methods. This program defines a class named Hello.


You can give a class any name you like, but it is conventional to start with a
6 Chapter 1 The way of the program

capital letter. The name of the class has to match the name of the file it is in,
so this class has to be in a file named Hello.java.

Java uses squiggly braces ({ and }) to group things together. In Hello.java,


the outermost braces contain the class definition, and the inner braces contain
the method definition.

The line that begins with two slashes (//) is a comment, which is a bit of
English text that explains the code. When the compiler sees //, it ignores
everything from there until the end of the line. Comments have no effect on
the execution of the program, but they make it easier for other programmers
(and your future self) to understand what you meant to do.

1.5 Displaying strings


You can put as many statements as you like in main. For example, to display
more than one line of output:

public class Hello {

public static void main(String[] args) {


// generate some simple output
System.out.println("Hello, World!"); // first line
System.out.println("How are you?"); // another line
}
}

As this example shows, you can put comments at the end of a line as well as
on lines all by themselves.

Phrases that appear in quotation marks are called strings, because they con-
tain a sequence of “characters” strung together. Characters can be letters,
numbers, punctuation marks, symbols, spaces, tabs, etc.

System.out.println appends a special character, called a newline, that


moves to the beginning of the next line. If you don’t want a newline at the
end, you can use print instead of println:
1.6 Escape sequences 7

public class Goodbye {

public static void main(String[] args) {


System.out.print("Goodbye, ");
System.out.println("cruel world");
}
}

In this example, the first statement does not add a newline, so the output
appears on a single line as Goodbye, cruel world. Notice that there is a
space at the end of the first string, which appears in the output.

1.6 Escape sequences


It is possible to display multiple lines of output in just one line of code. You
just have to tell Java where to put the line breaks.

public class Hello {

public static void main(String[] args) {


System.out.print("Hello!\nHow are you doing?\n");
}
}

The output is two lines, each ending with a newline character:

Hello!
How are you doing?

The \n is an escape sequence, which is a sequence of characters that rep-


resents a special character. The backslash allows you to “escape” the string’s
literal interpretation. Notice there is no space between \n and How. If you add
a space there, there will be a space at the beginning of the second line.

Another common use of escape sequences is to have quotation marks inside


of strings. Since double quotes indicate the beginning and end of strings, you
need to escape them with a backslash.

System.out.println("She said \"Hello!\" to me.");


Exploring the Variety of Random
Documents with Different Content
THE STUDY AND TEACHING OF
HISTORY.
By Herbert B. Adams, Ph.D.
Professor of History in the Johns Hopkins University, Baltimore.
Among all the subjects of college study and college teaching, among
all the means of liberal education fitting young men for civic life and
public duty not one stands higher than the study and teaching of
History.
In my senior year at Amherst College, President Julius H. Seelye
gave my class a single lecture on the Philosophy of History. Among
other good things he said: "History is the grandest study in the
world." That remark made the profoundest impression upon my
student imagination. I said to myself, "If History is the grandest
study in the world, that is exactly the study I want." The good
President proved his statement to my satisfaction by showing the
relation of Greek and Roman civilizations to the spread of Christianity
and the education of Europe.
In Germany I first learned the true method, and at the same time,
the most practicable ways and means of studying and teaching
history. Amid a pleasant variety of academic courses by brilliant
lecturers like Kuno Fischer, Zeller, Ernst Curtius, Grimm, Treitschke,
Droysen, Du Bois Raymond, Lepsius, and others, I somehow felt a
lack of educational unity and system. There was need of some
backbone to unite the skeleton of human deeds and historic
experiences. This I found at last in the teachings of my old master,
Dr. J. C. Bluntschli, at Heidelberg. In his lecture courses on the
State, on the Constitutional Law, on Politics and on the International
Law of Modern Civilized States, I first began to realize that
government and law are the real forces which bind society and the
world together. I began to see that the true unity of the world's life
is to be found in the succession of States, Empires, Federations, and
in the International Relations, which are slowly leading to such great
aggregations as the United States of America and the United States
of Europe. In Germany I learned from a reading of Bluntschli's
various writing, including many noble articles in his
Staatsworterbuch, that there is such a thing as the World-State now
in process of evolution. From the published records of the Institute
of International Law, of which Dr. Bluntschli was the president, and
from a study of the subjects of Arbitration and International
Tribunals, I thought I could dimly discern the beginnings of that
Parliament of Man, the Federation of the World, of which the Poet
Tennyson sings in his Locksley Hall.
When I came to Baltimore three ideas of study and teaching were
uppermost in my mind: (1) the study of the origins of municipal life,
in order to find out whether it was Roman or Germanic; (2) the
study of the relations of Church and State, from their beginning
down to the present, for I had learned to believe in Germany that
the separation of civil from religious society is America's greatest
contribution to the world's progress; (3) the continued study of art
history for its own sake and as illustrating the history of civilization.
Out of the first of these ideas, developed by a reading of the works
of Sir Henry Maine, has grown my Historical Seminary and a long
series of University Studies in Historical and Political Science (chiefly
on Municipal, Economic and Institutional themes). Out of the second
idea evolved successive courses of lectures on Church and State, or
Religion and Government in the Ancient and Graeco-Roman World,
together with my whole system of graduate instruction upon the
Early History of Society, Greek and Roman Polities, Jewish and
Church History, and certain modern States like Prussia and France.
The third idea never had a good chance for development until recent
years when I have fairly begun to realize my original conception of
illustrating in concrete, artistic ways the progress of civilization.
Goldwin Smith, in his Lecture on History, says there can be no
philosophy of history until we realize the unity of the human race
and that history must be studied as a whole. Twenty years ago, at
the Johns Hopkins University, I began to teach Local History, as
representative of Universal History. I began with New England
Village Communities, with Plymouth Plantations, Salem and the
Massachusetts Bay Towns, those little republics which seemed to me
the very protoplasm of State life. The survival, continuity or revival
of old Germanic forms of village settlement, with common fields and
town commons, impressed my imagination and interested my
students. They carried this kind of study into this State of Maryland
and original papers by Maryland boys were published upon such
subjects as Parishes, Manors, and other local institutions. These lines
of inquiry were extended down the Atlantic seaboard to Virginia and
the Carolinas. Gradually the field of interest has been widened from
towns, plantations, parishes, and counties until now the
constitutional, economic and educational history of entire States is in
review or contemplation.
While I still believe in Local History and in limited subjects of student
research, I now recognize more fully than I used to do, the
importance of General History, especially for college students and
college graduates in the early part of their course. After all, the great
fact in History, as well as in Geography, is that the world is round.
You must recognize all human experience on this globe as parts of
one great whole, just as you recognize that the continents and
outlying islands are but related parts of one vast geographical
system. In every properly arranged course of school and college
instruction in the domain of History, this doctrine of unity ought to
be taken for granted. It is like the doctrine of divine unity in theology
or in nature, like the sun in our heavens. It gives light and rationality
to any and every course of study.
I used to think that it was the first duty of a boy to know the history
of his own State and country; but I am now persuaded that he
should know the history of mankind and of the world. Nobody would
study geography or geology from a purely local point of view. You
must have a consciousness of the whole in order to appreciate the
parts of any subject. It is a mistake to imagine that a boy or girl
cares most for what is nearest and most familiar. Children are always
gifted with imagination. They rejoice in the thought of lands that are
far off, of men who lived in olden times. They take the greatest
pleasure in heroic tales of Cyrus and of Hannibal, of Horatius and of
the great twin brethren, Castor and Pollux. Mythology, minstrelsy,
Bible stories, and lives of great wariors, explorers, discoverers,
inventors, these are of supreme interest to boys and girls. American
History should be taught to American youth, but chiefly the heroic,
the romantic, the biographical, in short the more human sides of our
colonel and national life.
History begins and ends with Man. Biographical approaches to the
world's life are the oldest, and best beaten paths for youth to follow.
Carlyle and Froude are among the champions of the biographical
method of studying and teaching History. When Froude succeeded
Freeman at Oxford the biographical idea was at once brought to the
front. Froude quoted Carlyle as saying: "The history of mankind is
the history of its great men; to find out these, clean the dirt from
them, and place them on their proper pedestals, is the true function
of the historian." And Froude, the new professor, entered at once
upon those splendid and inspiring courses of lectures, in which the
personal and biographical elements entered so strongly.
Every American student should read Froude's lectures on "English
Seamen in the Sixteenth Century," that brilliant account of Sir
Francis Drake, Sir John Hawkins, and the great captains of England
who gained a new world for Elizabeth and defeated the Spanish
Armada. You should also read Froude's Lectures on the "Life and
Letters of Erasmus" if you would understand the relation of the great
religious reform to the new learning, which Erasmus represented.
Is it not wonderful that by reading a brief biography, which perhaps
occupies our leisure hours for a week, we can grasp and understand
the life-work of a great man? Think of it! A whole life in one book. A
whole history is in one of Plutarch's chapters. By turning to that new
series of biographies called "Heroes of the Nations," you can study
or teach the lessons derived from the lives and characters of such
great men as Pericles, Cicero, Julius Caesar, Julian the Philosopher,
Theodoric the Goth, Wyclif, the first of the English reformers, Prince
Henry the Navigator, Henry of Navarre, Sir Philip Sidney, Gustavus
Adolphus, Napoleon, Nelson, and Lincoln. Another excellent
biographical series is that called "English Men of Action," published
by Macmillan, and containing such noble lives as those of Wolfe, the
conqueror of Quebec; David Livingstone, the Explorer of Africa, Lord
Lawrence and Sir Henry Havelock, the saviors of India; General
Gordon, the Hero of Khartoum. If your taste runs toward literature,
you should read select biographies in the series called "English Men
of Letters," embracing such characters as Gibbon, Carlyle, Byron,
Shelly, and Hume. There is a corresponding American series, edited
by Charles Dudley Warner, and embracing such men as Washington
Irving, J. Fenimore Cooper, and Edgar Allan Poe. But among all
biographies for boys and young men I know nothing better than the
Autobiography of Franklin. This has encouraged and quickened many
young Americans to a desire of knowledge and self-culture.
But let no student or teacher believe for one moment that historical
biography is the full equivalent of History. Not all the biographies
that have ever been written could possibly contain the world's great
life. As the poet Tennyson truly says: "The individual withers, but the
world is more and more." There must be great men in Church and
State, to lead society forward, but there must be unnumbered
thousands, yea millions, of good men and true, and of faithful,
devoted women, in order to support good leadership and carry
humanity forward from generation to generation. It is often the
biography of some plain man or self-sacrificing woman that affords
the greatest encouragement and incentive to ordinary humanity. But
we must remember that no man, no woman is worthy of
biographical or historical record, unless in some way he or she has
contributed to the welfare of society and the progress of the world.
Only those deeds which affect our fellow men in some noteworthy
manner are fit for commemoration. What you do as a private
individual, what you ate for breakfast, what you do in the seclusion
of your own room, is not necessarily historic; but whether Napoleon
was able to eat his breakfast on the morning of the Battle of
Waterloo, or whether an army has been properly fed, may have the
greatest historic significance.
Not man alone, but man in organized society, is the subject of
History. Man in his relation to his fellows, man as a military, political,
social, intellectual, and religious being may become historic. Dr.
Thomas Arnold sometimes defined History as the biography of
nations. This is a large and noble conception, although not the
largest, and it may be profitably emphasized, like human biography
in the study and teaching of History. It is the duty of every school
and college to lay great stress upon the history of England and of
the United States in addition to General History. We all need to know
the lives of our own people as well as the lives of great Englishmen
like Pitt and Gladstone, and great Americans like Washington and
Lincoln. We should teach and study the histories of those nations
which are nearest our mother country—Scotland, France, Germany,
and Italy. As Germany is now the great seat of culture and of
university life for American students who go abroad, so was Italy for
wandering English students in the days of the Renaissance. English
literature from that time onward is pervaded with Italian elements,
with the influences of "all Etruscan three," Dante, Petrarch, and
Boccaccio, and also with the ideas of Machiavelli and the Italian
historians. We cannot understand the literature of England or
America without going back to its French and Italian sources. It
would be wise for college professors of history to devote special
attention to the Italian Renaissance or Revival of Learning, without
which an understanding of the German Reformation and modern
education is an impossibility.
In reading the biography of men or the biography of nations,
teachers and students should note carefully the most interesting and
memorial points. If you own the book which you are reading, use for
note-taking the fly leaves at the end. Otherwise, use reference
cards, like those employed in a library for a card catalogue, or else
sheets of note paper. When you have found a fact or illustration
which you think will prove useful at some future time, in connection
with your work as a teacher or a student, note it briefly on paper
with the proper reference to the book and page. Remember Captain
Cuttle's advice: "When found, make a note of!" Recall the saying of
Lord Bacon: "Reading maketh a full man, conference a ready man,
and writing an exact man. And, therefore, if a man write little, he
had need have a great memory."
In the multitude of modern books and amid all the variety of our
modern reading, it is impossible to remember exact quotations and
historical details. We must have a good system of note-taking and
index-making. Every student and teacher can invent his own system.
Mine is the use of fly leaves in books and cards topically and
alphabetically arranged for miscellaneous data. I always carry a few
of these reference cards in my pocket and make all my notes under
appropriate catch words, for example, "Chautauqua" or "Johns
Hopkins University," with the name of the writer on this subject and
the exact reference or quotation.
Begin to collect a library for yourselves. Students and teachers do
not always appreciate the opportunities they enjoy of acquiring good
books of History. I would strongly urge students to save their money
instead of spending it on poor theaters and variety shows. Buy
standard books of literature, art, and history; devote your leisure
hours to good reading, always with pen and pencil in hand, and with
a dictionary and an atlas beside you. Seize the moment of excited
curiosity and look up every point on which you need exact
information. One of my former students, Dr. Albert Shaw, now editor
of The Review of Reviews, said he was more grateful to me for that
advice than for any other one feature of my instruction: Seize the
moment of excited curiosity, or it will be lost forever.
An English writer, Langford, in his Praise of Books, well says: "In
books the past is ours as well as the present. With them we live
yesterday over again. All the bygone ages are with us, and we look
on the face of the infancy of the world. We see the first dawning of
thought in man. We are present at the beginnings of cities, states,
and nations; and can trace the growth and development of
governments, policies, and laws. The marvelous story of humanity is
enacted again for our edification, instruction, and delight. We behold
civilizations begin, struggle, triumph, and decay, giving place to
higher and nobler as they pass away. Poet, lawgiver, and soldier sing
their songs, make their codes, and fight their battles again, while we
follow the never-dying effects of song, of law, and of battle. We sit
down with 'princes, potentates, and powers,' watching them, as they
think, governing the world.... Shut up in a little room we can witness
the whole drama of man's history played on the vast stage of the
world. All that he has thought and done from the earliest dawn of
recorded time to our own day is enacted before us; and our hopes
are strengthened, our faith deepened, in the great destiny yet
awaiting mankind; in the higher, holier work yet to be done by those
who have accomplished such mighty things, achieved such noble
victories. Books which record the history of the past are the infallible
and unerring prophets of the future."
"History is the grandest study in the world." My College President,
Dr. Julius H. Seelye, was right. There is no art or science comparable
to it, for it embraces the whole experience of man in organized
society. History takes hold of all the past and points the way to all
the future. The French historian, Guizot, in his "History of My Time"
(III. 162) says: "Religion opens the future and places us in the
presence of eternity. History brings back the past and adds to our
own existence the lives of our fathers." Pliny said of History: "Quanta
potestas, quanta dignitas, quanta majestes, quantum denique
numen sit historiae." Perhaps the highest conception of History
comes from the Greek. The etymology of the ward is an inspiration
for both student and teacher. History, from the Greek word historia,
is a knowing or learning by inquiry. To study History is to understand
by means of research, for History is a science; its very essence
consists in knowledge. Historical science is perhaps the most
comprehensive and the noblest of all sciences, for it is the self-
knowledge of Humanity. The subject of History is Humanity itself; it
is the self-conscious development of the human race. History,
therefore, does not consist in dead facts, but is itself a living fact; it
is the self-knowledge of the present with regard to its evolution from
the past. Clio is a living muse, not a dead, cold form. She stands
upon that very threshold of the future and glances backwards over
the long vista Humanity has traversed. In the plastic art of the
Greeks you will notice that the muse of History is represented in the
attitude of reflection; the pen is uplifted, but the word unwritten.
We sometimes speak of written history and of its standard works as
though the essence of that science consisted in books and not in
knowledge. "There are no standards of history," said Droysen, a
German professor to an American student who had asked his advice
respecting the choice of standard works for an historical library. In
this caustic saying there lies a profound truth. History is a living,
self-developed science, not a collection of fossils. Books like facts,
are sometimes dead to history, and historical standards, like
historical facts, are grander in their spiritual influence than in their
material form. In the onward march of historical science, historians
are perhaps the standard bearers of fact and their works may be
called the battle-flags of history which kindle the zeal of the ever-
advancing present in men and awaken a sense of unity with the
great past, which has gone on before us. But written history often
becomes shot-riddled by criticism and is set away, at last, like battle-
flags, after many honorable campaigns, in some museums of relics
or some temple of fame. Unless such trophies continue to awaken in
the living present a sort of enthusiasm and a sense of unity with the
past experience of our race, then are our historic standards but
antiquarian rubbish, indeed, as useless and unmeaning as the
banners and symbols of heraldry.
The subject of History is the self-conscious development of the
human race, the Ego of Humanity. The realization of this Ego does
not lie in any fictitious personality, but in the universal consciousness
that man is one in all ages and that the individual human mind may
mirror to itself and to others the thought and experience of the race.
As the heavens are reflected in a single drop of dew, so in the
thoughts of the individual human mind we may sometimes behold a
reflection of the self-knowledge of Humanity. For the individual is
sometimes the very best expression of the whole with which it
stands in connection. The onward march of world-history seems to
have concentrated itself in the development of individual peoples like
the Jews, Greeks, Romans, and the Germanic peoples. As these
nations best typify historic progress and certain world-historic ideas,
so the historic thought of manhood may be most fully realized by
individual minds. For example, a single historian, like Thucydides,
may reflect the self-consciousness of his age, and a single mind, in
our own day, may realize, in some measure, at least through the
works of history, the self-knowledge, the Ego of Humanity. It should
be the aim of every student of History thus to realize in his own
consciousness the historic thought of mankind. "The life of each
individual," says Dr. W. T. Harris, the American Hegel, "presupposes
the life of the race before him, and the individual cannot
comprehend himself without comprehending first the evolution of his
day and generation historically from the past."
Let us then regard the study of History, not as something wholly
objective but as an unfolding panorama of the human self-
consciousness, for history is merely the reflecting spirit of mankind
in which we ourselves may have an immediate share, which we all
may help to perpetuate and in some way enlarge. Let us remember
that History is a constant knowing and learning, the self-knowledge
and communion of reflecting spirits in all ages and a perpetual
"Know Thyself" to advancing time. There is something indescribably
solemn in the historic pausing of Man before the temple of the
unknown future and seeking to realize in himself the gnothi seauton
or "Know Thyself" of Humanity. He glances backward through the
long vista he has traversed and as far as the eye can see, his
pathway is cumbered with ruins. Crumbling monuments and fallen
columns reveal the wreck of all material greatness, while the distant
pyramids but remind him of the more than Egyptian darkness out of
which Humanity has been mysteriously led unto this mountain of
light which we call the Present or that Living Age. Man sees the
immense distance he has come and he remembers the perils and
disasters he has encountered in his upward way; he is conscious too
of having brought a vast wealth of experience to this temple of the
Future before which he now stands, but that which fills and
overwhelms the historic consciousness of Man is the feeling that the
place whereon he stands is holy ground and that there is a
mysterious power in his own soul calling him to self-knowledge and
to self-judgment before he may lift the veil of the future. This is the
supreme moment of History. The facts of human experience become
suddenly transfigured in the light of a divine principle, namely the
self-consciousness of reason, that God-given spirit which recognizes
the purpose of History to be the increasing self-knowledge of Man.
"History is a divine drama, designed to educate man into self-
knowledge and the knowledge of God," (Henry James, Sr., on
"Carlyle," in Atlantic Monthly, May, 1881.) Tennyson recognized the
divine element in human history in that prophetic verse:
"And I doubt not through the ages one increasing
purpose runs,
And the thoughts of men are widened with the process
of the suns."
It is by this "increasing purpose" that God reveals himself in human
history. By the widened thought, Humanity is led forward, as it were
by a pillar of fire, unto a higher life, and unto a conscious unity with
Divine Reason, the Unseen One, who dwells in a temple not made
with hands.
SOME FACTS CONCERNING THE
SETTLEMENT AND EARLY HISTORY
OF MISSISSIPPI.
By R. W. JONES, M. A., LL.D.
The "Miscellaneous Papers" as collected by Col. J. F. H. Claiborne
constitute a rich mine for the future historian. They also indicate
what can be done by others by well-direct inquiry, in the way of
gathering information from "old settlers" and by going to other
sources that may be accessible. The importance of this work can be
scarcely overstated, and the sooner it is begun the better. A volume
could be written composed of adventures and daring exploits that
would be as thrilling as highly wrought fiction and make us proud of
our ancestors. As an illustration of the large number of well known
characters introduced, within a limited space, and of most
interesting and instructive incidents I shall quote from a letter of

DR. A. R. KILPATRICK
to Col. Claiborne, written at Navasota, Grimes county, Texas,
May 2, 1877:
* * "You ask for my contributions to De Bow's Review, but I am
entirely unable to furnish them. When I left Concordia, La., in
September, 1863, I moved none of my books, and the scoundrel
in whose hands I left the place proved to be a traitor joined the
Yankees, and when Natchez was occupied he went partners
with some of the Federal officers, who brought over several
wagons, gutted my house and sold the furniture and other
property in Natchez. Out of a library of 2,000 volumes I have
none left. Among my books were (12) twelve volumes of De
Bow's Review bound." In these twelve volumes Dr. K. had
written a great deal that was interesting and instructing to those
who inquire into the settlement and colonial history of
Mississippi. They contain accounts of many of the best known
families who lived at and near Natchez and Woodville and in the
counties wherein these towns are situated; also similar writings
concerning Concordia, La.
He says; "Before I wrote those accounts of Concordia Parish, I
wrote some Sketches of the early Baptist in Mississippi and
Louisiana which were printed in a Baptist newspaper of New
Orleans under the management of a Minister named Duncan: I
think he was Rev. W. Duncan, D. D. Get copies of those papers
and make use of the historical facts, because your work will be
incomplete if you leave out the churches. These papers were
published about 1849-51.
"My Grandfather (Maternal), Robert Turner, was an early settler
in Miss., and a pioneer of the Baptist Church, though not a
minister. He moved a colony of nearly (100) one hundred, white
and black from Beaufort District, S. C., starting in 1804. He went
up near Nickajack on the Tennessee River, built boats, put on
his horses, cows, hogs, furniture and floated down to Natchez,
reaching there early in 1805, he found there no settlement to
suit, went down to Fort Adams, landed, and settled four miles S.
W., of where Woodville now stands. There they built old Bethel
Church with whipsawed lumber and wrought iron nails, each
one furnishing his part of materials, or work. The Chaplain or
preacher of the colony was Rev. Moses Hadley. At that time,
1805, there were only a few houses, temporary shanties, where
Woodville is. Ole Uncle Bob Lecky, who kept hotel so many
years in Alexandria, La., and old John S. Lewis of Woodville,
were the first to put up houses. My Grandfather, R. Turner was a
Surveyor and was employed to measure and lay off the streets,
squares, etc., of the town in 1808. He was also summoned and
served in the arrest of Aaron Burr above Natchez about 1807;
he said it was so cold in February that in handling oars of the
skiff the blood poured from the tips of his fingers. He
represented Aaron Burr as remarkably polite, genteel, urbane,
good looking, though small, and as having eyes whose glance
was most penetrating and fascinating.
"There was another party of pioneers from Georgia, preceding
Grandfathers; in this party were the Ogdens and Nolands."
"Captain John Ogden, near Woodville, (1796-1837) served as
Captain at the battle of New Orleans, 1814. Robert Tanner and
several of his colonists moved to Rapides Parish, La. There the
old gentleman died September, 1839, of yellow fever, aged 71
years. Wilkinson county furnished one Governor (H. Johnson) to
Louisiana and (4) four, I think, to Mississippi.
"The old original editor of the Woodville Republican, W.
Chisholm, had all the volumes of that paper bound for over
twenty years—from about 1820 to 1845. In it will be found
much of Poindexter's history; also much of Moses Waddell, of
Abbeville, S. C., brother-in-law of John C. Calhoun."

Rev. Wm. Winans, D. D., lived and died at Mount Pleasant, about
sixteen miles east southeast of Woodville. Major Butler, of Kentucky,
lived there; also General Van Dorn's father. Major Butler served in
General Wilkinson's command. The general was very strict in regard,
not only to his own dress, but also the dress, etc., of the officers and
men under him. It was the fashion then to wear the hair long and
plait it into a queue, or pig-tail behind. General Wilkinson had the
misfortune to lose his pig-tail and issued an order for all to cut off
their pig-tails.
Major Butler refused; Wilkinson threatened court martial; Butler
resigned and retired to the farm of his sister, Mrs. Cook. In a few
months he died; before dying he left special injunctions with Mr. and
Mrs. Cook to have an auger-hole bored in his coffin, to have his hair
neatly dressed and the pig-tail tied with a blue ribbon and run
through that auger-hole, so that Wilkinson and his officers might see
that he was pluck to the last and distained his authority.
Dr. Franklin L. Riley, in a lecture, gives another version of this
incident, which is very amusing. Dr. Kilpatrick narrates many
incidents concerning Governor Poindexter, Mr. Percy, Audubon, Jeff
Davis and others.
The Audubon mentioned by him was the distinguished John James
Audubon, the Naturalist. Born 1781, in Louisiana, died 1851, on the
Hudson; Author of Birds of America, Quadrupeds of America, etc.
Audubon was at the house of Mr. Percy, spent several months with
him; he furnished Audubon with many specimens of birds for his
sketches. One day Percy says he brought home a "magnificent
gobbler" which weighed about 28 pounds and Audubon would have
it. He pinned it up beside the wall so as get a good view of it and
spent several days lazily sketching it. Percy said: "The —— fellow
kept it pinned up there till it rotted and stunk. I hated to lose so
much good eating."
It is said that while Aububon was at or near Woodville, his money
gave out; he refused to accept gifts; but taught a dancing school, in
order to get funds sufficient to enable him to proceed with his
researches in Natural History. The people patronized him generously.

"Jeff. Davis spent part of his boyhood in Wilkinson county, Miss.


There was a boy on a place adjoining where Jeff. Davis lived
named Bob Irion, son of a Baptist preacher. The two boys went
hunting one day, each alone, and after some time they met
behind a field. Jeff. Davis was out of shot and Bob was out of
powder, but had shot. Davis wanted some shot and asked for
some, but Bob was unaccommodating and saucy—jeered at
Davis, and finally told him he had a mind to shoot him any how,
and made some threatening demonstration which aroused
Davis. Davis jerked out a small pocket knife dropped it down his
gun on the load of powder and raised his gun and said: "Now,
sir, I'm ready for you; I dare you to shoot.' Bob told me this
himself during the Mexican campaign, as illustrating Davis'
bravery and fertility of resources in emergencies. Of course the
boys stopped their foolishness and exchanged ammunition." * *
"I got on the Sultana at Fort Adams when S. S. Prentiss was
aboard on his bridal trip—married that morning at Natchez, and
the whole bridal troupe went down to New Orleans. It was my
first sight and acquaintance with Prentiss. I was charmed with
his manners and appearance. He had the most handsome head,
and it sat better on his neck and shoulders than any person I
know. That was in 1843, when his fame was world wide; yet,
sir, he was as bashful, timid and quiet as a boy of 16 in the
presence of those ladies."
"At table he had nothing to say, but ate his meals quietly,
almost stealthily. But as soon as he came down in the social
hall, he was lively and chatted enough."

I could give other extracts of value and interest from this same
letter, but I will not worry you. I hope it will not be long before this
letter and other important historical manuscripts will be printed.
PRE-HISTORIC JASPER
ORNAMENTS IN MISSISSIPPI
R. B. FULTON, M. A., LL. D.
In the annual Report of the Smithsonian Institute for 1877, Dr. Chas.
Rau, under the title of "The Stock-in-Trade of an Aboriginal
Lapidary," emphasizes his conjecture "that among the aborigines
certain individuals who were by inclination or practice particularly
qualified for a distinct kind of manual labor, devoted themselves
principally or entirely to that labor." He referred to several instances
where, in certain localities, finds of a large number of similarly
wrought specimens of work in stone seemed to indicate that each
set of specimens came from the hands of a special lapidary.
One of the most remarkable of these deposits was found in
Lawrence County, Mississippi, in 1875, and was carefully described
by Dr. Rau. It consisted of 469 imperfectly finished objects made by
chipping, cutting and grinding out of reddish or orange-colored or
brown jasper pebbles, and was found accidentally about two and
one-half feet below the surface of the ground in the northern part of
Lawrence County,[67] The objects were evidently intended for
ornaments, and when finished all would have been polished and
probably perforated. The majority were cylindrical in shape, and
varied from one-fourth to one inch in diameter and from one-fourth
to three inches in length. Others were roughly fashioned into
ornamental shapes. Several showed an attempt at perforation, and
one, not received at the National Museum, was said to be completely
perforated.
When the hardness of the material used—jasper—is considered, the
patience and skill needed to give their form and polish to these
objects command admiration. From the fact that only one specimen
was perforated completely, one might readily suppose that the
workman found the difficulties of this part of his undertaking too
great, and buried his unfinished work in despair.
Some time ago there came into my hands a set of similar articles
found in the county of Lincoln, Mississippi, about twenty-five miles
west of the spot where the above-mentioned find was made.
These last found objects were exhibited at the Cleveland meeting of
the American Association for the Advancement of Science, in the
hope of learning whether similar specimens had been found, as they
appeared to me at that time to be entirely unique.
Following out suggestions made at that meeting by several
gentlemen, and afterward by two of the best informed Southern
archaeologists, I found that the above-mentioned region in
Mississippi has yielded a number of carved, polished and perforated
objects of this hard red or brown quartzite (or jasper), and nearly all
such specimens of this material which I have been able to learn
about came from this region.
The collection of specimens of this style of workmanship described
by Dr. Rau probably contains the majority of pieces extant. A few
specimens of polished jasper ornaments from other States than
Mississippi are shown in the National Museum. There are two or
three specimens from Indiana, one from California, and one from
Louisiana (Claiborne Parish), which seem to be similarly made and
from the same material.
The late Dr. Joseph Jones of New Orleans had in his collection some
jasper ornaments, mostly from Mississippi, including a beautiful
ceremonial ax of reddish translucent jasper.
Besides those mentioned I have not been able to learn of other
similar objects. Probably there are a few scattered ones in other
hands.
The collection of these objects in my possession includes thirty
pieces. They were found on a farm four miles west of Wesson, in
Lincoln County. And were plowed up on the summit of a hill where
no earthworks were noticed. A few other relics were found at the
same time and were not preserved. With them were two other
beads, one of a gray stone and the other of bone very truly shaped,
as if in a lathe.
Among the jasper ornaments (all of which are perforated
longitudinally with holes from one-tenth to one-eighth of an inch in
diameter) are three cylinders between two and a half and three
inches long and about one-fourth of an inch in diameter; ten
cylinders ranging from a quarter to an inch and a quarter in length
and less than one-quarter in diameter; five nearly spherical beads;
one accurately shaped short cylinder three-quarters of an inch long
and five-eighths in diameter, with a well-drilled perforation three-
eights of an inch in diameter; and ten carved ornaments of various
shapes. One of these, an inch long, is a strikingly sculptured deer.
Four are evidently intended for birds, and four others resemble each
other and in form are indistinctly bird-like. A separate ring of the
same material is firmly fixed on one of the long beads.
All of the specimens have evidently seen service as personal
ornaments. They have a fine polish externally, and the interior of the
borings is worn smooth as by a string. An artistic color-perception is
shown in the beautiful variety of tints brought out in various pieces
of jasper used.
As to all these ornaments in red jasper mentioned in this paper,
comparison of the specimens forcibly suggests that they may be the
work of one skilled artist. In the western pebble belt of Mississippi,
which extends along the border of the Mississippi and Yazoo river
bottoms southward from near Memphis to Natchez, and thence
eastward through the counties in which these relics have been
found, quartzite of almost every variety occurs, and chipped
implements of almost every variety and color are common. The
maker of these ornaments has passed by all other tints save red and
brown. In the cylindrical and other carved forms that have been
found there is a striking similarity both in design and workmanship.
One will readily believe the perforation of these ornaments with
small and accurately made drillings to have been the most difficult
part of their manufacture. And yet in all the specimens seen the
perforations have been in the longest direction through the
ornament. The total length of the borings in the set of thirty beads I
have is twenty-eight inches. A lapidary not remarkably expert in the
art of drilling these holes would probably have simplified his work by
shorter borings, arranging the ornaments as pendants.
Again, the rarity of any objects of carved or polished or perforated
quartzite suggests a very limited manufacture even in the region
under consideration.
As to the means used in making these perforations, drills of stone
are excluded from consideration on account of the smallness and
length of the borings.
There is one specimen in the collection of Dr. Joseph Jones of New
Orleans, in which a boring has been began, evidently with a hollow
tube as a drill, probably a joint of a reed fed with sand, as there is a
core in the centre of the boring; but hollow drills as small as one-
twelfth of an inch in diameter could scarcely have been used. Some
of the specimens described by Dr. Rau show the beginning of the
drilling process, apparently with a solid drill, fed with sand.
We are forced to the conclusion that the drilling implement used
must have been a needle of copper, or more probably of the hard
outer wood of the Southern cane tipped with quartz, or fed with
sand. The borings are about as true in direction and form as the
best modern appliances could make them.
It is worthy of note that these highly wrought jasper ornaments
have been found in that portion of Mississippi once occupied by the
Natchez, that these aboriginal people were more or less familiar with
Mexican or Aztec art and customs, and that carved and polished
workmanship in hard stones was not uncommon among the
aborigines of Mexico and Central America.[68]
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!

ebookname.com

You might also like