## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

Contents
Preface ......................................... xiii
ListofFigures ..................................... xvii
ListofTables...................................... xix
I Basic Principles 1
1 Computer Hardware and Software 3
1.1 BriefHistoryofComputers........................... 3
1.2 HardwareComponentsofaComputer .................... 3
1.3 BinaryRepresentationofNumbers ...................... 6
1.4 SoftwareCreationandTypesofSoftware................... 6
1.5 TypeofProgrammingLanguages ....................... 7
1.6 BriefHistoryofComputerGames....................... 8
1.7 Summary..................................... 9
1.8 ImportantPoints ................................ 10
1.9 Exercises..................................... 10
2 Data Types and Conditional Statements 11
2.1 OurFirstJavaProgram ............................ 11
2.2 Variables..................................... 16
2.3 Random Numbers and the if Statement ................... 23
2.4 CombiningConditions ............................. 27
2.5 The String Class................................ 29
2.6 The switch Statement ............................. 32
2.7 TheConditionalOperator ........................... 33
2.8 Summary..................................... 35
2.9 Syntax ...................................... 35
2.10 ImportantPoints ................................ 37
2.11 Exercises..................................... 38
2.12 Lab........................................ 39
3Loops 41
3.1 The while Statement.............................. 41
3.2 The do-while Construct............................ 43
3.3 The for Loop .................................. 45
3.4 Nested for Loops................................ 49
3.5 TheModulusOperation ............................ 52
3.6 Summary..................................... 54
3.7 Syntax ...................................... 54
3.8 ImportantPoints ................................ 55
3.9 Exercises..................................... 56
vii
viii Contents
3.10 Lab........................................ 57
3.11 Project...................................... 58
4 Methods and Formatted Output 61
4.1 IntroductiontoMethods ............................ 61
4.2 FormattingOutput ............................... 64
4.3 CodeRefactoring ................................ 66
4.5 SendingDatabetweenMethods ........................ 69
4.7 Summary..................................... 78
4.8 Syntax ...................................... 78
4.9 ImportantPoints ................................ 78
4.10 Exercises..................................... 79
4.11 Lab........................................ 80
4.12 Project...................................... 80
5 Introduction to Arrays 83
5.1 One-DimensionalArrays ............................ 83
5.1.1 DeepversusShallowArrayCopy ................... 93
5.1.2 DeepversusShallowArrayComparison................ 95
5.3 Two-DimensionalArrays............................ 99
5.4 VariableArgumentMethods.......................... 102
5.5 CommandLineArguments........................... 103
5.6 Summary..................................... 103
5.7 Syntax ...................................... 103
5.8 ImportantPoints ................................ 105
5.9 Exercises..................................... 105
5.10 Lab........................................ 106
5.11 Project...................................... 107
6 Introduction to Classes 109
6.1 Classes and Objects ............................... 109
6.2 ClassInteractionandDataEncapsulation .................. 110
6.3 DefaultConstructor............................... 114
6.4 The toString Method ............................. 116
6.5 InstanceversusStatic.............................. 117
6.6 Non-empty Constructors and the Hidden Parameter this ......... 118
6.7 Array of Objects and Multi-Class Solutions .................. 120
6.8 Multi-ClassSolutiontotheBattleshipGame................. 123
6.9 Summary..................................... 133
6.10 Syntax ...................................... 133
6.11 ImportantPoints ................................ 134
6.12 Exercises..................................... 135
6.13 Lab........................................ 136
6.14 Project...................................... 136
Contents ix
7TheArrayList Class and the enum Keyword 137
7.1 Introduction to the ArrayList Class ..................... 137
7.2 Immutable Objects ............................... 138
7.3 The StringBuffer Class............................ 140
7.4 The Interface of an ArrayList ......................... 142
7.5 Introducing the enum Construct ........................ 144
7.6 Summary..................................... 152
7.7 Syntax ...................................... 152
7.8 ImportantPoints ................................ 153
7.9 Exercises..................................... 153
7.10 Lab........................................ 154
7.11 Project...................................... 154
8 Classes Revisited 159
8.1 ClassContainment ............................... 159
8.2 Inheritance and the super Keyword...................... 160
8.3 MultipleInheritance .............................. 163
8.4 ConstructorsofSubclasses ........................... 164
8.5 AbstractClassesandMethods......................... 166
8.6 Auto-casting,Polymorphism,andDynamicBinding............. 167
8.7 Interfaces and the Comparable Interface ................... 172
8.8 Access Privileges ................................ 175
8.9 The final Keyword............................... 178
8.10 StaticMethodsandPolymorphism ...................... 178
8.11 ExplicitTypeChecking............................. 180
8.12 Cloning Objects ................................. 181
8.13 Comparing Objects for Equality ........................ 184
8.14 Summary..................................... 186
8.15 Syntax ...................................... 186
8.16 ImportantPoints ................................ 187
8.17 Exercises..................................... 188
8.18 Lab........................................ 190
8.19 Project...................................... 191
9 Fun with Swing 193
9.1 IntroductiontoSwing.............................. 193
9.2 CreatingWindows................................ 194
9.3 PanelsandDrawing............................... 198
9.4 Summary..................................... 209
9.5 Syntax ...................................... 209
9.6 ImportantPoints ................................ 211
9.7 Exercises..................................... 211
9.8 Lab........................................ 212
x Contents
10 Nested Classes and Event Handling 213
10.1 The Timer Class................................. 213
10.2 NestedClasses.................................. 215
10.2.1 StaticNestedClasses.......................... 215
10.2.2 InnerClasses .............................. 216
10.2.3 LocalClasses .............................. 218
10.3 EventListeners ................................. 220
10.3.1 KeyListeners .............................. 220
10.3.2 MouseListeners............................. 223
10.4 Multicasting................................... 231
10.5 Summary..................................... 233
10.6 Syntax ...................................... 234
10.7 ImportantPoints ................................ 235
10.8 Exercises..................................... 236
10.9 Lab........................................ 237
10.10Project...................................... 237
11 The Breakout Game (Complete Version) 239
11.1 OverviewoftheGame ............................. 239
11.2 GameDesign .................................. 240
11.3 MovingtheBall................................. 241
11.5 DrawingtheStickmen ............................. 250
11.8 Summary..................................... 263
11.9 Syntax ...................................... 263
11.10ImportantPoints ................................ 264
11.11Exercises..................................... 264
11.12Lab........................................ 265
11.13Project...................................... 265
12 Layout Management and GUI Components 267
12.1 CreatingButtons ................................ 267
12.2 FlowLayout................................... 268
12.3 BorderLayout.................................. 269
12.4 TextFieldsandLabels ............................. 270
12.5 GridLayout ................................... 273
12.6 CreatingTextAreaswithScrollBars ..................... 275
12.7 TheComboBox................................. 276
12.8 CheckBoxes................................... 279
12.10DocumentListeners............................... 282
12.11CreatingDialogBoxes ............................. 284
12.13Summary..................................... 288
12.14Syntax ...................................... 288
12.15ImportantPoints ................................ 290
12.16Exercises..................................... 291
12.17Lab........................................ 292
Contents xi
12.18Project...................................... 292
13 Exception Handling and Files 293
13.1 HandlingExceptions .............................. 293
13.2 TextFiles .................................... 299
13.2.1 TheFileChooserDialog ........................ 299
13.2.3 WritingtoTextFiles.......................... 303
13.3 DataFiles .................................... 306
13.4 Summary..................................... 313
13.5 Syntax ...................................... 313
13.6 ImportantPoints ................................ 315
13.7 Exercises..................................... 316
13.8 Lab........................................ 317
13.9 Project...................................... 317
14 Recursion 319
14.1 BaseCaseandGeneralCase.......................... 319
14.2 DynamicProgramming............................. 321
14.3 InternalDetailsofaRecursiveCall ...................... 323
14.4 ArrayAlgorithms ................................ 326
14.4.1 BinarySearch.............................. 326
14.4.2 Bubble Sort ............................... 329
14.4.3 Selection Sort .............................. 331
14.4.4 InsertionSort .............................. 333
14.4.5 QuickSort................................ 334
14.4.6 MergeSort................................ 336
14.5 Summary..................................... 338
14.6 Syntax ...................................... 338
14.7 ImportantPoints ................................ 338
14.8 Exercises..................................... 339
14.9 Lab........................................ 340
14.10Project...................................... 340
15 Java Applets 343
15.1 HTML and the Java Applet Architecture ................... 343
15.2 PrinciplesofJavaApplets ........................... 344
15.3 CreatingPopupWindows ........................... 346
15.4 TheTic-Tac-ToeGame............................. 347
15.5 Summary..................................... 358
15.6 Syntax ...................................... 358
15.7 ImportantPoints ................................ 359
15.8 Exercises..................................... 360
15.9 Lab........................................ 360
15.10Project...................................... 360
Index 361

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required