本書對面向對象的思想和機制進行了準確而透徹的剖析,為讀者深入學習Java語言程序設計提供了全面、詳細的指導,并在前幾版的基礎上進行了全面的修訂與更新。全書覆蓋了面向對象設計的廣泛內容,介紹了Java語言的基本數據類型、流程控制、類和對象等。在深入分析面向對象設計方法的基礎上,介紹了封裝機制、繼承機制和多態(tài)性的實現與應用、異常的捕捉和處理、集合類的定義方法和泛型類的概念,書中提供了大量具有可實踐性的程序實例、自測題及答案、練習題和編程項目、生動的"軟件失誤案例”和"視頻講解”學習輔導短片,并且在每章中都補充了針對本章主題的圖形用戶界面(GUI)設計知識和實例,這部分內容可形成完整的Java GUI設計知識體系。此外,本書還有內容豐富的附錄,并提供了在線程序設計實踐網站服務。
John Lewis教授是美國知名的編程語言暢銷教材的作者,撰寫了多部有關Java編程導論、Java軟件解決方案、Java數據結構以及計算機應用的相關教材。Lewis教授從美國弗吉尼亞大學獲得了博士學位并一直在該校任教。
John Lewis教授是美國知名的編程語言暢銷教材的作者,撰寫了多部有關Java編程導論、Java軟件解決方案、Java數據結構以及計算機應用的相關教材。Lewis教授從美國弗吉尼亞大學獲得了博士學位并一直在該校任教。
Contents
Chapter 1 Introduction 1
1.1 Computer Processing 2
Software Categories 3
Digital Computers 5
Binary Numbers 7
1.2 Hardware Components 10
Computer Architecture 11
Input/Output Devices 12
Main Memory and Secondary Memory 13
The Central Processing Unit 17
1.3 Networks 20
Network Connections 20
Local-Area Networks and Wide-Area Networks 22
The Internet 23
The World Wide Web 24
Uniform Resource Locators 25
1.4 The Java Programming Language 26
A Java Program 28
Comments 30
Identifiers and Reserved Words 31
White Space 34
1.5 Program Development 36
Programming Language Levels 36
Editors, Compilers, and Interpreters 39
Development Environments 40
Syntax and Semantics 41
Errors 42
1.6 Object-Oriented Programming 44
Problem Solving 45
Object-Oriented Software Principles 46
Chapter 2 Data and Expressions 57
2.1 Character Strings 58
The print and println Methods 58
String Concatenation 60
Escape Sequences 63
2.2 Variables and Assignment 65
Variables 65
The Assignment Statement 67
Constants 69
2.3 Primitive Data Types 71
Integers and Floating Points 71
Characters 73
Booleans 74
2.4 Expressions 75
Arithmetic Operators 75
Operator Precedence 76
Increment and Decrement Operators 80
Assignment Operators 81
2.5 Data Conversion 83
Conversion Techniques 85
2.6 Interactive Programs 87
The Scanner Class 87
2.7 Graphics 92
Coordinate Systems 92
Representing Color 94
2.8 Applets 95
Executing Applets Using the Web 98
2.9 Drawing Shapes 99
The Graphics Class 99
Software Failure:
NASA Mars Climate Orbiter and Polar Lander 111
Chapter 3 Using Classes and Objects 113
3.1 Creating Objects 114
Aliases 116
3.2 The String Class 118
3.3 Packages 122
The import Declaration 124
3.4 The Random Class 126
3.5 The Math Class 129
3.6 Formatting Output 132
The NumberFormat Class 132
The DecimalFormat Class 134
The printf Method 135
3.7 Enumerated Types 138
3.8 Wrapper Classes 141
Autoboxing 143
3.9 Components and Containers 143
Frames and Panels 144
3.10 Nested Panels 148
3.11 Images 151
Chapter 4 Writing Classes 159
4.1 Classes and Objects Revisited 160
4.2 Anatomy of a Class 162
Instance Data 167
UML Class Diagrams 167
4.3 Encapsulation 169
Visibility Modifiers 170
Accessors and Mutators 171
4.4 Anatomy of a Method 172
The return Statement 174
Parameters 175
Local Data 175
Bank Account Example 176
4.5 Constructors Revisited 181
4.6 Graphical Objects 182
4.7 Graphical User Interfaces 191
4.8 Buttons 192
4.9 Text Fields 196
Software Failure:
Denver Airport Baggage Handling System 205
Chapter 5 Conditionals and Loops 207
5.1 Boolean Expressions 208
Equality and Relational Operators 209
Logical Operators 210
5.2 The if Statement 213
The if-else Statement 216
Using Block Statements 219
Nested if Statements 223
5.3 Comparing Data 226
Comparing Floats 226
Comparing Characters 227
Comparing Objects 228
5.4 The while Statement 230
Infinite Loops 234
Nested Loops 236
The break and continue Statements 239
5.5 Iterators 241
Reading Text Files 242
5.6 The ArrayList Class 245
5.7 Determining Event Sources 248
5.8 C heck Boxes and Radio Buttons 251
Check Boxes 251
Radio Buttons 255
Software Failure:
Therac-25 267
Chapter 6 More Conditionals and Loops 269
6.1 The switch Statement 270
6.2 The Conditional Operator 274
6.3 The do Statement 275
6.4 The for Statement 279
The for-each Loop 282
Comparing Loops 284
6.5 Drawing with Loops and Conditionals 285
6.6 Dialog Boxes 291
Chapter 7 Object-Oriented Design 301
7.1 Software Development Activities 302
7.2 Identifying Classes and Objects 303
Assigning Responsibilities 305
7.3 Static Class Members 305
Static Variables 306
Static Methods 306
7.4 C lass Relationships 310
Dependency 310
Dependencies Among Objects of the Same Class 310
Aggregation 316
The this Reference 320
7.5 Interfaces 322
The Comparable Interface 327
The Iterator Interface 328
7.6 Enumerated Types Revisited 329
7.7 Method Design 332
Method Decomposition 333
Method Parameters Revisited 338
7.8 Method Overloading 343
7.9 Testing 345
Reviews 346
Defect Testing 346
7.10 GUI Design 349
7.11 Layout Managers 350
Flow Layout 352
Border Layout 356
Grid Layout 359
Box Layout 361
7.12 Borders 365
7.13 C ontainment Hierarchies 369
Software Failure:
2003 Northeast Blackout 377
Chapter 8 A rrays 379
8.1 A rray Elements 380
8.2 Declaring and Using Arrays 381
Bounds Checking 384
Alternate Array Syntax 389
Initializer Lists 389
Arrays as Parameters 390
8.3 A rrays of Objects 392
8.4 C ommand-Line Arguments 402
8.5 Variable Length Parameter Lists 404
8.6 Two-Dimensional Arrays 408
Multidimensional Arrays 412
8.7 P olygons and Polylines 413
The Polygon Class 416
8.8 Mouse Events 418
8.9 Key Events 427
Software Failure:
LA Air Traffic Control 441
Chapter 9 Inheritance 443
9.1 C reating Subclasses 444
The protected Modifier 447
The super Reference 450
Multiple Inheritance 453
9.2 Overriding Methods 455
Shadowing Variables 457
9.3 C lass Hierarchies 458
The Object Class 460
Abstract Classes 461
Interface Hierarchies 463
9.4 Visibility 463
9.5 Designing for Inheritance 466
Restricting Inheritance 467
9.6 The Component Class Hierarchy 468
9.7 Extending Adapter Classes 471
9.8 The Timer Class 475
Software Failure:
Ariane 5 Flight 501 485
Chapter 10 P olymorphism 487
10.1 Late Binding 488
10.2 P olymorphism via Inheritance 489
10.3 P olymorphism via Interfaces 502
10.4 Sorting 504
Selection Sort 505
Insertion Sort 511
Comparing Sorts 512
10.5 Searching 513
Linear Search 513
Binary Search 515
Comparing Searches 519
10.6 Designing for Polymorphism 519
10.7 Event Processing 521
10.8 F ile Choosers 522
10.9 C olor Choosers 525
10.10 Sliders 527
Chapter 11 Exceptions 537
11.1 Exception Handling 538
11.2 Uncaught Exceptions 539
11.3 The try-catch Statement 540
The finally Clause 544
11.4 Exception Propagation 545
11.5 The Exception Class Hierarchy 549
Checked and Unchecked Exceptions 552
11.6 I/O Exceptions 553
11.7 Tool Tips and Mnemonics 557
11.8 C ombo Boxes 564
11.9 Scroll Panes 569
11.10 Split Panes 572
Chapter 12 R ecursion 583
12.1 R ecursive Thinking 584
Infinite Recursion 584
Recursion in Math 585
12.2 R ecursive Programming 586
Recursion vs. Iteration 589
Direct vs. Indirect Recursion 589
12.3 Using Recursion 590
Traversing a Maze 591
The Towers of Hanoi 596
12.4 R ecursion in Graphics 601
Tiled Pictures 601
Fractals 604
Chapter 13 Collections 617
13.1 C ollections and Data Structures 618
Separating Interface from Implementation 618
13.2 Dynamic Representations 619
Dynamic Structures 619
A Dynamically Linked List 620
Other Dynamic List Representations 625
13.3 Linear Data Structures 627
Queues 627
Stacks 628
13.4 Non-Linear Data Structures 631
Trees 631
Graphs 632
13.5 The Java Collections API 634
Generics 634
Appendix A Glossary 641
Appendix B Number Systems 665
Appendix C The Unicode Character Set 673
Appendix D Java Operators 677
Appendix E Java Modifiers 683
Appendix F Java Coding Guidelines 687
Appendix G Java Applets 693
Appendix H R egular Expressions 695
Appendix I Javadoc Documentation Generator 697
Appendix J The PaintBox Project 703
Appendix K GUI Events 715
Appendix L Java Syntax 719
Appendix M The Java Class Library 733
Appendix N A nswers to Self-Review Questions 735
Index 789