Copyright © 2003 David Schmidt
Table of Contents
Preface i
Chapter 1: Computers and Programming 1
-
1.1 What is a Computer? 1
-
1.2 Computer Programming 2
-
1.3 Programs Are Objects 5
-
1.4 Operating Systems and Windows 6
-
1.5 Software Architecture 8
- 1.5.1 Class Diagrams 9
-
1.6 Summary 11
-
1.7 Beyond the Basics 13
- 1.7.1 Stepwise Refinement 14
- 1.7.2 Object-Oriented Design 15
- 1.7.3 Classes Generate Objects 17
- 1.7.4 Frameworks and Inheritance 18
Chapter 2: Simple Java Applications 20
-
2.1 An Application and its Architecture 20
-
2.2 How to Build and Execute an Application 23
- 2.2.1 Using an IDE 23
- 2.2.2 Using the JDK 28
-
2.3 How the Application Works 30
- 2.3.1 An Execution Trace of the Application 33
-
2.4 How One Object Constructs Another 35
-
2.5 Repairing Compiler Error Messages 41
-
2.6 Summary 43
-
2.7 Programming Exercises 46
-
2.8 Beyond the Basics 47
- 2.8.1 Syntax 47
- 2.8.2 Semantics 49
- 2.8.3 Java Packages 51
- 2.8.4 Java API 51
Chapter 3: Arithmetic and Variables 56
-
3.1 Integer Arithmetic 57
-
3.2 Named Quantities: Variables 59
- 3.2.1 Variables Can Vary: Assignments 63
-
3.3 Arithmetic with Fractions: Doubles 68
-
3.4 Booleans 72
-
3.5 Operator Precedences 74
-
3.6 Strings, Characters, and their Operations 75
-
3.7 Data-Type Checking 80
-
3.8 Input via Program Arguments 83
- 3.8.1 Converting between Strings and Numbers and Formatting 86
- 3.8.2 Temperature Conversion with Input 88
-
3.9 Diagnosing Errors in Expressions and Variables 92
-
3.10 Java Keywords and Identifiers 95
-
3.11 Summary 95
-
3.12 Programming Projects 97
-
3.13 Beyond the Basics 102
- 3.13.1 Longs, Bytes, and Floats 102
- 3.13.2 Helper Methods for Mathematics 103
- 3.13.3 Syntax and Semantics of Expressions and Variables 105
Chapter 4: Input, Output, and State 107
-
4.1 Interactive Input 108
- 4.1.1 Dialog Output 112
-
4.2 Graphical Output 113
- 4.2.1 Panels and their Frames 113
- 4.2.2 Customizing Panels with Inheritance 115
-
4.3 Format and Methods for Painting 122
- 4.3.1 Constructor Methods and this Object 124
-
4.4 Objects with State: Field Variables 130
- 4.4.1 Using Fields to Remember Inputs and Answers 135
- 4.4.2 Scope of Variables and Fields 140
-
4.5 Testing a Program that Uses Input 142
-
4.6 Summary 144
-
4.7 Programming Projects 147
-
4.8 Beyond the Basics 151
- 4.8.1 Scope of Variables and Fields 151
- 4.8.2 Partial API for JFrame 152
- 4.8.3 Methods for GregorianCalendar 152
- 4.8.4 Colors for Graphics 152
- 5.8.5 Applets 153
Chapter 5: Component Structure: Method and Class Building 157
-
5.1 Methods 158
-
5.2 Public Methods 159
- 5.2.1 Basic Public Methods 160
- 5.2.2 Constructor Methods 164
-
5.3 Parameters to Methods 168
- 5.3.1 Forms of Parameters 173
-
5.4 Case Study: General Purpose Output Frame 179
-
5.5 Results from Methods: Functions 186
-
5.6 Private Methods 193
-
5.7 Summary 200
-
5.8 Programming Projects 203
-
5.9 Beyond the Basics 212
- 5.9.1 Naming Variables, Methods, and Classes 212
- 5.9.2 Generating Web Documentation with javadoc 213
- 5.9.3 Static Methods 217
- 5.9.4 How the Java Compiler Checks Typing of Methods
220
- 5.9.5 Formal Description of Methods 221
- 5.9.6 Revised Syntax and Semantics of Classes 227
Chapter 6: Control Structure: Conditional Statements 229
-
6.1 Control Flow and Control Structure 230
-
6.2 Condtional Control Structure 231
- 6.2.1 Nested Conditional Statements 235
- 6.2.2 Syntax Problems with Conditionals 240
-
6.3 Relational Operations 241
-
6.4 Uses of Conditionals 245
-
6.5 Altering Control Flow 249
- 6.5.1 Exceptions 250
- 6.5.2 System Exit 251
- 6.5.3 Premature Method Returns 252
-
6.6 The Switch Statement 252
-
6.7 Model and Controller Components 255
- 6.7.1 Designing an Application with a Model-View-Controller Architecture 257
-
6.8 Case Study: Bank Accounts Manager 259
- 6.8.1 Collecting Use-Case Behaviors 259
- 6.8.2 Selecting a Software Architecture 261
- 6.8.3 Specifying the Model 261
- 6.8.4 Writing and Testing the Model 261
- 6.8.5 Specifying the View Components 265
- 6.8.6 A Second Look at the Software Architecture 265
- 6.8.7 Writing the View Classes 265
- 6.8.8 Controller Construction and Testing 272
- 6.8.9 Testing the Assembled Application 276
- 6.8.10 Multiple Objects from the Same Class 277
-
6.9 More about Testing Methods and Classes 283
- 6.9.1 Testing Individual Methods 283
- 6.9.2 Testing Methods and Attributes Together 283
- 6.9.3 Testing a Suite of Methods 284
- 6.9.4 Execution Traces 285
-
6.10 Summary 286
-
6.11 Programming Projects 290
-
6.12 Beyond the Basics 295
- 6.12.1 The Logic of the Conditional Statement 295
- 6.12.2 Interface Specifications and Integration 300
Chapter 7: Patterns of Repetition: Iteration and Recursion 304
-
7.1 Repetition 305
-
7.2 While Loops 306
-
7.3 Definite Iteration 308
- 7.3.1 Definite-Iteration Example: Painting a Bulls-Eye 153
-
7.4 Nontermination 319
-
7.5 Indefinite Iteration: Input Processing 321
- 7.5.1 Indefinite Iteration: Searching 324
-
7.6 For-Statements 328
-
7.7 Nested Loops 329
-
7.8 Writing and Testing Loops 335
-
7.9 Case Study: Bouncing Ball Animation 338
-
7.10 Recursion 347
- 7.10.1 An Execution Trace of Recursion 352
-
7.11 Counting with Recursion 356
- 7.11.1 Loops and Recursions 359
- 7.11.2 Counting with Multiple Recursions 360
-
7.12 Drawing Recursive Pictures 363
-
7.13 Summary 366
-
7.14 Programming Projects 370
-
7.15 Beyond the Basics 378
- 7.15.1 Loop Termination with break 379
- 7.15.2 The do-while Loop 380
- 7.15.3 Loop Invariants 381
- 7.15.4 Loop Termination 386
- 7.12.5 More Applets 387
Chapter 8: Data Structure: Arrays 391
-
8.1 Why We Need Arrays 392
-
-
8.2 Collecting Input Data within Arrays 396
-
8.3 Translation Tables 399
-
8.4 Internal Structure of One-Dimensional Arrays 402
-
8.5 Arrays of Objects 406
-
8.6 Case Study: Databases 3409
- 8.6.1 Behaviors 412
- 8.6.2 Architecture 413
- 8.6.3 Specifications 413
- 8.6.4 Implementation 415
- 8.6.5 Forms of Records and Keys 420
-
8.7 Case Study: Playing Pieces for Card Games 424
-
8.8 Two-Dimensional Arrays 431
-
8.9 Internal Structure of Two-Dimensional Arrays 434
-
8.10 Case Study: Slide-Puzzle Game 437
-
8.11 Testing Programs with Arrays 446
-
8.12 Summary 448
-
8.13 Programming Projects 450
-
8.14 Beyond the Basics 458
- 8.14.1 Sorting 458
- 8.14.2 Searching 462
- 8.14.3 Time-Complexity Measures 465
- 8.14.4 Divide-and-Conquer Algorithms 469
- 8.14.5 Formal Description of Arrays 477
Chapter 9: Programming to Interfaces 483
-
9.1 Why We Need Specifications 484
-
9.2 Java Interfaces 485
- 9.2.1 Case Study: Databases 493
-
9.3 Inheritance 497
-
9.4 Reference Types, Subtypes, and instanceof 500
-
9.5 Abstract Classes 508
- 9.5.1 Case Study: Card Players 509
- 9.5.2 Class Hierarchies 516
- 9.5.3 Frameworks and Abstract Classes 519
-
9.6 Subtypes versus Subclasses 519
-
9.7 class Object and Wrappers 520
-
9.8 Packages 522
- 9.8.1 Generating Package APIs with javadoc 524
-
9.9 Case Study: An Adventure Game 526
- 9.9.1 Interfaces and Inheritance Together 532
- 9.9.2 Inheritance of Interfaces 539
-
9.10 Summary 539
-
9.11 Programming Projects 543
-
9.12 Beyond the Basics 544
- 9.12.1 Subclasses and Method Overriding 545
- 9.12.2 Semantics of Overriding 550
- 9.12.3 final components 555
- 9.12.4 Method Overloading 556
- 9.12.5 Semantics of Overloading 561
Chapter 10: Graphical User Interfaces and Event-Driven Programming 564
-
10.1 Model-View-Controller Revisited 565
-
10.2 Events 567
-
10.3 The AWT/Swing Class Hierarchy 568
-
10.4 Simple Windows: Labels and Buttons 571
-
10.5 Handling an Event 579
- 10.5.1 A View as Action Listener 579
- 10.5.2 A Separate Controller 583
- 10.5.3 A Button-Controller 583
-
10.6 Richer Layout: Panels and Borders 590
- 10.6.1 An Animation in a Panel 594
-
10.7 Grid Layout 600
-
10.8 Scrolling Lists 604
-
10.9 Text Fields 610
-
10.10 Error Reporting with Dialogs 617
-
10.11 TextAreas and Menus 620
- 10.11.1 Case Study: Text Editor 623
-
10.12 Event-Driven Programming with Observers 632
- 10.12.1 Observers and the MVC-Architecture 635
-
10.13 Summary 636
-
10.14 Programming Projects 641
-
10.15 Beyond the Basics 645
- 10.15.1 Applets 646
- 10.15.2 Tables and Spreadsheets 649
- 10.15.3 Handling Mouse Clicks and Drags 655
- 10.15.4 Threads of Execution 664
- 10.15.5 GUI Design and Use-Cases 669
- 10.15.6 Summary of Methods for Graphical Components 671
Chapter 11: Text and File Processing 680
-
11.1 Strings are Immutable Objects 681
- 11.1.1 String Tokenizers 683
-
11.2 Sequential Files 686
- 11.2.1 Output to Sequential Files 688
- 11.2.2 Input from Sequential Files 690
-
11.3 Sequential Input from the Command Window 692
-
11.4 Case Study: Payroll Processing 695
-
11.5 Exceptions and Exception Handlers 700
- 11.5.1 Restarting a Method with an Exception Handler 702
- 11.5.2 Interactive Input with Exception Handlers 705
-
11.6 Exceptions Are Objects 706
- 11.6.1 Programmer-Generated Exceptions 713
-
11.7 Summary 714
-
11.8 Programming Projects 716
-
11.9 Beyond the Basics 719
- 11.9.1 Character-by-Character File Processing 719
- 11.9.2 Binary Files and Files of Objects 720
- 11.9.3 A Taxonomy of File Classes 721
- 11.9.4 A GUI for File Selection 730
Appendix I:Java Language Definition 724
Appendix II:Types and Subtypes 741
Appendix III:Class Diagrams 744
Index 748