
E-Bookshop-master / uploads / file / Think-Java-How-to-Think-Like-a-Computer-Scientist_(1)
.pdfx |
Chapter 0. Preface |
Muhammad Saied translated the book into Arabic, and found several errors.
Marius Margowski found an inconsistency in a code example.
Guy Driesen found several typos.
Contents
Preface |
v |
1 The way of the program |
1 |
1.1What is a programming language? . . . . . . . . . . . . . . . 1
1.2What is a program? . . . . . . . . . . . . . . . . . . . . . . . 3
1.3What is debugging? . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Formal and natural languages . . . . . . . . . . . . . . . . . 6
1.5The rst program . . . . . . . . . . . . . . . . . . . . . . . . 8
1.6Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.7Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2 Variables and types |
13 |
2.1More printing . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3 |
Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 |
2.4Printing variables . . . . . . . . . . . . . . . . . . . . . . . . 16
2.5 |
Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
18 |
2.6 |
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
18 |
xii |
Contents |
2.7Order of operations . . . . . . . . . . . . . . . . . . . . . . . 19
2.8Operators for Strings . . . . . . . . . . . . . . . . . . . . . 20
2.9Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3 Methods |
25 |
3.1Floating-point . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.2 |
Converting from double to int . . . . . . . . . . . . . . . . 26 |
3.3Math methods . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.4Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.5Adding new methods . . . . . . . . . . . . . . . . . . . . . . 29
3.6 |
Classes and methods . . . . . . . . . . . . . . . . . . . . . . 31 |
3.7Programs with multiple methods . . . . . . . . . . . . . . . . 32
3.8Parameters and arguments . . . . . . . . . . . . . . . . . . . 33
3.9 |
Stack diagrams . . . . . . . . . . . . . . . . . . . . . . . . . 34 |
3.10Methods with multiple parameters . . . . . . . . . . . . . . . 35
3.11Methods with results . . . . . . . . . . . . . . . . . . . . . . 36
3.12Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.13Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4 Conditionals and recursion |
39 |
|
4.1 |
The modulus operator |
. . . . . . . . . . . . . . . . . . . . . 39 |
4.2Conditional execution . . . . . . . . . . . . . . . . . . . . . . 39
4.3Alternative execution . . . . . . . . . . . . . . . . . . . . . . 40
Contents |
xiii |
4.4Chained conditionals . . . . . . . . . . . . . . . . . . . . . . 41
4.5Nested conditionals . . . . . . . . . . . . . . . . . . . . . . . 42
4.6The return statement . . . . . . . . . . . . . . . . . . . . . . 43
4.7Type conversion . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.8 |
Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 |
4.9Stack diagrams for recursive methods . . . . . . . . . . . . . 46
4.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5 GridWorld: Part One |
51 |
5.1 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . |
51 |
5.2BugRunner . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
6 Fruitful methods |
55 |
6.1Return values . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.2Program development . . . . . . . . . . . . . . . . . . . . . . 57
6.3Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.4Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.5Boolean expressions . . . . . . . . . . . . . . . . . . . . . . . 62
6.6 |
Logical operators . . . . . . . . . . . . . . . . . . . . . . . . |
63 |
6.7 |
Boolean methods . . . . . . . . . . . . . . . . . . . . . . . . |
63 |
6.8More recursion . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.9Leap of faith . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.10 One more example . . . . . . . . . . . . . . . . . . . . . . . 68
6.11Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
6.12Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
xiv |
Contents |
7 Iteration |
75 |
7.1Multiple assignment . . . . . . . . . . . . . . . . . . . . . . . 75
7.2Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
7.3The while statement . . . . . . . . . . . . . . . . . . . . . . 76
7.4 |
Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 |
7.5Two-dimensional tables . . . . . . . . . . . . . . . . . . . . . 81
7.6 |
Encapsulation and generalization . . . . . . . . . . . . . . . 81 |
7.7Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.8More encapsulation . . . . . . . . . . . . . . . . . . . . . . . 83
7.9Local variables . . . . . . . . . . . . . . . . . . . . . . . . . . 84
7.10More generalization . . . . . . . . . . . . . . . . . . . . . . . 84
7.11Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
7.12Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
8 Strings and things |
91 |
8.1Invoking methods on objects . . . . . . . . . . . . . . . . . . 91
8.2Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
8.3Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
8.4Run-time errors . . . . . . . . . . . . . . . . . . . . . . . . . 93
8.5Reading documentation . . . . . . . . . . . . . . . . . . . . . 95
8.6The indexOf method . . . . . . . . . . . . . . . . . . . . . . 96
8.7Looping and counting . . . . . . . . . . . . . . . . . . . . . . 96
8.8Increment and decrement operators . . . . . . . . . . . . . . 97
8.9Strings are immutable . . . . . . . . . . . . . . . . . . . . . 98
8.10 Strings are incomparable . . . . . . . . . . . . . . . . . . . 99
8.11Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
8.12Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Contents |
xv |
9 Mutable objects |
107 |
9.1Points and Rectangles . . . . . . . . . . . . . . . . . . . . . 107
9.2Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
9.3Point objects . . . . . . . . . . . . . . . . . . . . . . . . . . 108
9.4Instance variables . . . . . . . . . . . . . . . . . . . . . . . . 109
9.5 |
Objects as parameters . . . . . . . . . . . . . . . . . . . . . 110 |
9.6Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
9.7Objects as return types . . . . . . . . . . . . . . . . . . . . . 111
9.8Objects are mutable . . . . . . . . . . . . . . . . . . . . . . . 111
9.9 |
Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 |
9.10null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
9.11 Garbage collection . . . . . . . . . . . . . . . . . . . . . . . 114
9.12Objects and primitives . . . . . . . . . . . . . . . . . . . . . 115
9.13Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
9.14Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
10 GridWorld: Part 2 |
123 |
10.1Termites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
10.2Langton's Termite . . . . . . . . . . . . . . . . . . . . . . . . 129
11 Create your own objects |
131 |
11.1Class de nitions and object types . . . . . . . . . . . . . . . 131
11.2Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
11.3Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
11.4More constructors . . . . . . . . . . . . . . . . . . . . . . . . 134
xvi |
Contents |
11.5 |
Creating a new object . . . . . . . . . . . . . . . . . . . . . 135 |
11.6Printing objects . . . . . . . . . . . . . . . . . . . . . . . . . 136
11.7Operations on objects . . . . . . . . . . . . . . . . . . . . . . 137
11.8Pure functions . . . . . . . . . . . . . . . . . . . . . . . . . . 137
11.9Modi ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
11.10 Fill-in methods . . . . . . . . . . . . . . . . . . . . . . . . . 141
11.11Incremental development and planning . . . . . . . . . . . . 142
11.12Generalization . . . . . . . . . . . . . . . . . . . . . . . . . . 143
11.13Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
11.14Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
11.15Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
12 Arrays |
149 |
|
12.1 |
Accessing elements |
. . . . . . . . . . . . . . . . . . . . . . . 150 |
12.2 |
Copying arrays . . |
. . . . . . . . . . . . . . . . . . . . . . . 151 |
12.3for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
12.4 Arrays and objects . . . . . . . . . . . . . . . . . . . . . . . 152
12.5Array length . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
12.6Random numbers . . . . . . . . . . . . . . . . . . . . . . . . 153
12.7 Array of random numbers . . . . . . . . . . . . . . . . . . . 154
12.8Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
12.9The histogram . . . . . . . . . . . . . . . . . . . . . . . . . . 157
12.10A single-pass solution . . . . . . . . . . . . . . . . . . . . . . 158
12.11Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
12.12Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Contents |
xvii |
13 Arrays of Objects |
165 |
13.1 The Road Ahead |
. . . . . . . . . . . . . . . . . . . . . . . . 165 |
13.2Card objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
13.3The printCard method . . . . . . . . . . . . . . . . . . . . . 167
13.4 |
The sameCard method . . |
. . . . . . . . . . . . . . . . . . . 169 |
13.5 |
The compareCard method |
. . . . . . . . . . . . . . . . . . . 170 |
13.6 |
Arrays of cards . . . . . . |
. . . . . . . . . . . . . . . . . . . 171 |
13.7The printDeck method . . . . . . . . . . . . . . . . . . . . . 173
13.8 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
13.9Decks and subdecks . . . . . . . . . . . . . . . . . . . . . . . 177
13.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
13.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
14 Objects of Arrays |
181 |
14.1The Deck class . . . . . . . . . . . . . . . . . . . . . . . . . . 181
14.2Shu ing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
14.3Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
14.4Subdecks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
14.5Shu ing and dealing . . . . . . . . . . . . . . . . . . . . . . 185
14.6 Mergesort . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
14.7Class variables . . . . . . . . . . . . . . . . . . . . . . . . . . 189
14.8Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
14.9Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
xviii |
Contents |
15 Object-oriented programming |
193 |
15.1Programming languages and styles . . . . . . . . . . . . . . . 193
15.2 |
Object methods and class methods |
. |
. . |
. . . . . |
. |
. |
. |
. |
. |
. |
194 |
15.3 |
The toString method . . . . . . . |
. |
. . |
. . . . . |
. |
. |
. |
. |
. |
. |
195 |
15.4The equals method . . . . . . . . . . . . . . . . . . . . . . . 196
15.5Oddities and errors . . . . . . . . . . . . . . . . . . . . . . . 197
15.6Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
15.7The class hierarchy . . . . . . . . . . . . . . . . . . . . . . . 199
15.8 Object-oriented design . . . . . . . . . . . . . . . . . . . . . 199
15.9Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
15.10Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
16 GridWorld: Part 3 |
203 |
16.1ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
16.2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
16.3public and private . . . . . . . . . . . . . . . . . . . . . . 206
16.4Game of Life . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
16.5LifeRunner . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
16.6LifeRock . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
16.7Simultaneous updates . . . . . . . . . . . . . . . . . . . . . . 209
16.8 |
Initial conditions |
. . . . . . . . . . . . . . . . . . . . . . . . 210 |
|
A Graphics |
|
213 |
|
A.1 |
Java 2D Graphics |
. . . . . . . . . . . . . . . . . . . . . . . . |
213 |
A.2 |
Graphics methods |
. . . . . . . . . . . . . . . . . . . . . . . |
214 |
Contents |
|
|
xix |
|
|
A.3 |
Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 |
||
|
A.4 |
Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 |
||
|
A.5 |
Mickey Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . 216 |
||
B |
Input and Output in Java |
221 |
||
|
B.1 |
System objects |
. . . . . . . . . . . . . . . . . . . . . . . . . 221 |
|
|
B.2 |
Keyboard input |
. . . . . . . . . . . . . . . . . . . . . . . . . 222 |
|
|
B.3 |
File input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 |
||
|
B.4 |
Catching exceptions . . . . . . . . . . . . . . . . . . . . . . . 223 |
||
C |
Program development |
225 |
||
|
C.1 |
Strategies . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . 225 |
|
|
C.2 |
Failure modes . . . . . . . . . . |
. . . . . . . . . . . . . . . . 226 |
|
D |
Debugging |
|
229 |
|
|
D.1 |
Syntax errors . . . . . . . . . . |
. . . . . . . . . . . . . . . . 229 |
|
|
D.2 |
Run-time errors . . . . . . . . . |
. . . . . . . . . . . . . . . . 233 |
|
|
D.3 |
Logic errors . . . . . . . . . . . |
. . . . . . . . . . . . . . . . 237 |