l In.roducfion I
1 .1 A brief history ot comDutinq 2
y of computing 2
1 .2 What is computer science? 4
1 .3 A brief tour of computer hardware 5
The CFU 6, ....;un)?rsh?:f:oa:es'o?o.. 7, Input/output
. =,.M?m.laprsh?:f:oa:es'o?o,. 7, Input/output
devices 7, Network 8
1.4 Alaorithms 8
.orlthms 8
1 .5 Staaes in the Droaramminq orocess 9
nes in the programming process 9
CreotinQ cud 85n?no:p?::n?sn;?c::so,?m.,,. errors cud
d 9 progroms 9, Frogromming errors cud
d.b,.d7,. 12,,;?::g'omS,:.n;oeo'om",,g errors cud
.ding 12,,.l::g'omS,:.n;oeo'om",,g errors cud
1 .6 Java and the obiect-oriented paradigm 15
The history of
3.3 Operators and operands 65
Combininq,,,..',So:?n+foso,if3-.',,, numbers 66 Integer
d Integers cud flooting-point numbers 66, Integer
division cud the remoinder operotor 67, Precedence 68,
Applying rules of,,...d.,.eeiodlCI .=?onncez I'1
pplying rules of precedence 70, Type conversion 71
3.4 Assianment slot6ments 73
.Inment slot6ments 73
',.,so?deoS::ant:?n?not:.:?o,, 76 The increment cud
.,.,,fo?deoS::ant:?n?not:.:?o,, 76 The increment cud
anment operotors 76, The increment cud
decrement operotors 77
3.5 Boolean expressions 77
seIQtiono[ .;ne[:?:rSn)87To,,..I operotors 78, Shortcircuit
i i. Pe,,tZ,, 78of.Logicol operotorrs.'91 Shortcircuit
evoluotion 81 1 Flogs b'2, AneXom;o,:\s,::eosnn,,,,,,,,,,
l gtion 801K Flogs b'2, AneXom;o,:\s,::eosnn,,,,,,,,,,
colCulotion 82
3.6 Desiqninq for chanae 83
Uning for change 83
The importonce ot reodobility 83, U5ing nomed constonts to
1. mobiling so3) U5ing nomed.constonts to
','D:rrDMA sssi "us;?noa==S,O
support progrom mointenonce 84, Using nomed constonts to
i port progrom mointenonce 84, Using nomed constonts to
I I I O
Support progrom development 85
qllmman' 88
summary 88
y 88
Review questions 89
Proqrammina exercises 91
"ramming exercises 91
4 Sfot6menf Forms 95
4.1 Statement adDes in Java 96
Simple,,.,.me?tt 'nJ,vfoZ,,d stotements 98, Control
I I I ac o
stotements 98
4.2 Control statements and problem solying 99
GenerolizinQ the ..,,..:;=t'a,yg,h.
reDeotNd [he nddZIntegers progrom IOUgTh.
repeotN- F from 100, The repeot-
Ntimes pottern 101 1 The,..d-,,,,l-
,.?tin:,;3:r,'"leor:p,,,,4.3 The if statement 105
finale-line if stotements 107 Multiline if stotements 107 The
.Inqle-line if stotements 107, Multiline if stotements 107, The
.ie-line if stotements 107, Multiline if stotements 107, The
. ~ .ie-line if,,,,,"?:tj 1?71 Multiline,f s,o,e"e,t,s.107I The
if-else stotement 10/, Coscoding if stotements 107, The ?:
-,
l'. I d if stotements 1 07, The ?:
operotor 108
4.4 The switch slot6ment 1 10
4.5 The while slot6ment 1 12
UsinQ tnewhile fooD 1 1 3 Inrinite l000s 1 14 'SolvinQ the look
d lhewhile loop 1 1 3, Intinite loops 1 14, Solving the loop
.d .thsrwnile looP, Ill 3, Infinite loops 1 14, Solving the loop
cud-o-holf problem 1 15
4.6 The for slot6ment 1 18
The relotionship between for cud while 1 20, Using for with
ftootinq-point boot:el;;?
5 Mefhods 133
5.1 A quick overview of methods 1 34
Methods os mechonisms for hiding complexics 1 35, Methods os
tools for progrommers rother thou U,.,, 1 35, Method coils os
.
expressions 1 36, Method coils os messoges 1 37
5.2 Writing yogr own methods 138
The formot of o method 1 39, The return stotement 1 39,
Methods involving internol control structures 141, Methods thot
return nonnumeric volues 142, Fredicote methods 144
5.3 Mechanics of the method-calling process 147
Forometer possing 148, Colting ?eo:odcseffo.IW:,, other
methods 1 52
5.4 Decomposition 158
Stepwise refinement 1 59, Specifying porometers 161
oesigning from the top down 1 63, Looking for common
feotures 164, Completing the decomposition 165
5.5 Algorithmic methods 166
The fib,,t. f.,..il opproQch 1 66, Euclid's olgorithm 1 68,
Defending the correctness of E..lidi, olgorithm 1 68, Compering
the efficiency of the two olgorithms 170
Summary 170
Review questions 171
Programming exercises 172
6 Obiec.s and classes I7T
6.1 Using the Randondenerator class 178
F,.,Jo,..d.. numbers 1 79, Using the Randbenerator
closs 1 80, The role of the rondom number seed 1 83
6.2 The javadoc documentQtion system 185
6.3 Defining your own classes lisse" 185
The structure of o closs definition 1 88, Controlling the visibility of
entries 189, Encopsulotion 189
6.4 Representing student information 190
Y Obiects cud Memory 221
7.1 The structure of memory 222
Bits, bytes, cud words 2222BI.ry cud hexodecimol
i. ..ic',2!2,zBlo7 cud hi-c3tecimol
representotions 223, Memory .d;r.,,., 225
7.2 The allocation of memory to variables 226
Memory diooroms for the 2::acg:?ss % forbore
;... ~., ~. ~ o. ~..' ~. ~.... ~ --~ ~ ~ ~~~~~ ~. ~ ~~ ~ ~' I ~ ~. ~ ~ or
I. ac ac ac
collection 232
7.3 Primitive adDes versus obiects 232
ypes versus obiects 232
Porometer po55ing 234, cropper closses 236, Boxing cud
I. ac o Q
unboxinQ 238
d L O o
...
7.4 Linkinq obiects toqether 240
d I .ether 240
MessoQe .o,,i,.,n?:hed n,,.,{ The beocons ot
d= possing in linked structures f The beocons of
Gondor 240, The internol representotion of linked
l I ac A O
structures 243
qumman' 9
summary 244
y 244
Review questions 245
Proarammina exercises 247
dramming exercises 247
8 Strinas and chorocfers 249
gs and chorocfers 249
8.1 The principle of enumeration 250
kepresenting enumeroted types inside the mochine 25 1 1
representing enumeroted types os integers 252, Defining new
i
i i J n .;, types os integers 252, Defining new
enumeroted adDes 253 /pos os Integers 252, Defining new
lpes 253
8.2 Characters 254
The doto aiDe char 254 The ASCll cud Unicode coding
IPe char 254, The ASCll cud Unicode coding
l 'saing ct?:,:}et.on;f'Zo;? ?inn,,",,.,",,,g I f
sVstems 254 Chorocter constonts 257 I.gco?toentc:fJ:2,,,., of
lorems 254, Chorocter constonts 257, Importont p,.pe,ti., of
the Unicode representotion 257, Speciol .ho?;t:tetrP'oP;r,,,, of
Chorocter orithmetic 259, Useful methods in the character
l n 4 1 ~ I I I 1. 1. I I n' ac
closs 261 1 Control stotements involving chorocters 263
8.3 Strinqs as an abstract idea 263
ac as an abstract idea 263
Holistic cud reductionist views of strinQs 264 The notion ot on
or I
I I I I ac A 4
obstroct aiDe 264
/po 264
8.4 Usinq the methods in the String class 265
U the methods in the String class 265
DetermininQ the lenQth of o string 265, Selecting chorocters trom
or .rh ot o string 265, Selecting chorocters trom
.. .2--rhe length ot o string 265, Selecting chorocters trom
o string 267, Concotenotion 267, ExtroctinQ forts:oC,,,, from
d L67, Concotenotion 267, Extrocting forts:oC,,,, from
',,,,.'.if;e::::?ZiVMC:.
.trlnQ 2o9, ComDorinQ one strinQ .:X,;onc:'tne,P31so?f:e.,.h,,.
d L69, Compering one string with onother 270, Seorching
. d L69, Compering one string with onother 270, Seorching
within o
9 Obiec.-orien'ed Grophics 295
9.1 The acm.graphics model 296
9.2 Structure ot the acm.graphics package ?97
The ocanvas closs 297, Further detoils .:t?,:eL,..
class 300, The GPoint, GDhension, cud GRectangle
closses 301, The breath closs 302, The subject closs cud its
I I
Subclosses 303
9.3 Using the shape classes 306
The ffeabel .11?? 3'O:?e!hes:aee.. closs cud its subclosses
(~undaect cud G3Daect) 31 1, The coyal closs 31 1, The
GLine closs 31 2, The GArc cio55 31 3, The GImage clQss 3 1 o,
The GPolygon closs 32 1
9.4 Creating compound obieCts 328
A simple GC~ound exomple 328, The GCompound coordinote
system 331, Obiect
constroints 390, Using TableLayout to creote o simple
l l' and
colculotor 390
Rummanf '?99
summary 399
y 399
Review questions 401
Proqrammina exercises 402
"ramming exercises 402
l 1 Arrays and ArrayLists 4O9
ys and ArrayLists 4O9
1 1 .1 Introduction to arrays 410
Arroy declorotion 41y' trloov selection 41 2, An exomple ot o
i I I .rroy selection 41 2, An exompte ot o
. i I I .rroy Selection 41 2, An,eXompte ot o
Simple orroy 41 3, Chonging the index rouge 414, Arroys ot
l .pie orroy 41 3, Chonging the index rouge 414, Arroys ot
obiects 415, U5ing .,,.,S'M:ColPZ:, 4I A,,;v, of
diqression on the ++ cud "- .;o'r:'\sr;g,,", 41 5, A
.resslon on the ++ cud "- .;o'r:'\sr;g,,", 41 5, A
1 1 .2 Im6rnal representotion of arrays 419
1 1 .3 Passina arrays as parameters 421
u arrays as parameters 421
1 1 .4 Usina arrays for tabulation 427
u arrays for tabulation 427
1 1 .5 Initialization of arrays 428
ys 428
1 1 .6 Multidimensional arrays 430
FossinQ .,I,,d,..,,,.,.lxosr,o'v??o methods 431, Initiolizinq
d multidimensionol'orroys to methods 431 1 Initiolizing
IJ. l.. I I n n
multidimensionol orroVs 432
ye 432
1 1 .7 lmaae Drocessina 432
d processing 432
Kepresentotion of imoges 433, Using the Glance closs tO
i des 433, Using the Glance closs tO
. 1. des 433, Using the Glance closs tO
monlpulote imoges 433, Bit ..,,,,totroen';7rt?"4t:,,
I i
operotions to isolote components of o pixel 437, Creoting o
i. ponents oT o pixel 437, Creoting o
qroVscole imoQe 438, in:::::;i:?::l.:?"tiJ:si:3ti?; o
.royscole imoge 438, Smoothing .,imc',e7"{6,;',,,,,, o
.% tymsn::::);$?il,rs::3ting o
overoqinQ 4i00? H?u?d.M'ge7"{6u;',,,,,, o
.lug 439, Hiding complexity 439
1 1 .8 The ArrayList class 442
qllmman, 448
summary 448
y 448
Review questions 449
Proaramminq exercises 450
Urammlng exercises 450
12 Searching and Sorfino 461
9 and Soaking 461
1 2.1 Searchina 462
d
q..,.hi,.?, on inteoer orro\/ 462 Seorchina o tobte 463
...,.hi,ql, on integer orroy 4o2 SeorchinQ o tobte 4o3,
d in on integer orroy 462, Seorching o tobte 463,
computotionol complexity from code structure 483, Worst-cose
I
versus overoQe-cose comDlexify 485
d pie-city 485
12.4 Usinq data tiles 486
U data files 486
The concept of o file 486, 6eodinq text files in Jovo 488,
Exception F;:;;.',48,'n:,moomt?:'r+;'::,S n,
'...:!;can;SLY't?,Ztt5Z3,,,
selecting files,,,.7o.;?e?i 4Cb?gtt5Z34,,,
d tiles
14.3 Usina the network 562
U the network 562
14.4 Proaramminq oatterns 562
cramming patterns 562
The model/view/controller pottern 563, An illustrotive exomplef
Grophing spreodsheet d.t.' 564 1 p,':
qumman' F70
summary 570
y 570
Review questions 570
Proaramminq exercises 571
"ramming exercises 571
Index 5T5
..
xviI
