本书专为没有编程经验的学生编写,是一本使用Java语言进行面向对象编程的入门书籍。作者以对象的使用为起点,逐步讲解了如何编写对象和理解继承,面向对象设计的重要性,直至如何独立建立一个面向对象的系统。这种教学形式有助于学生在对象模型方面打下坚实的基础。
本书覆盖了Java语言的所有重要方面,包括使用Java标准库中的类,以及作者定义的比较容易理解和消化的类。此外,书中还专门介绍了Java语言的高级方面,包括GUI、applet。输入/输出以及基本的数据结构,使学生在编写Java程序时可以使用多种工具。
本书特点
逐步介绍Java语言类的使用,书的荫几章介绍输入/输出以及一组作者定义的工具以帮助刚开始学习Java的学生。第19章使用来自java.io包的类,重点介绍Java的输入和输出第11章让学生有机会综合所学到的概念和技巧,从构建类开始设计一个task organizsr程序第12章详细介绍继承的概念,让学生在准备学习其他高级的面向对象概念的同时,理解和掌握对象模型中继承的重要性第20章介绍applet的创建和使用第五部分讨论面向对象的设计,包括方法学以及案例分析,引导学生遍历开发过程的各个阶段。
This is a book about programming in the Java programming language. It is intended for people who have no previous programming experience, so it is unlike many other Java programming books, which appear to have been written by experts for experts. It was written from scratch and not based on previous C or C++ textbooks by the same authors, which also makes it different from many other books. This book was developed for a specific purpose and is not an
evolution from an earlier book. The book is thus well suited for a first-year undergraduate programming course. This edition has been thoroughly revised and updated for the latest versions of Java.
The authors have between them taught the first-year programming course at Lancaster University for more than 14 years, first in Pascal, then in Ada, and since 1996-1997 in Java,one of the first British universities to use the Java language for first-year programming. This book is based on our experience of teaching the language to classes of some 150 students. One author has experience in object-oriented software development and database management systems from Objective-C to C++ and ONTOS to POET. The other has written a number of textbooks on natural language processing and computer architecture.
Java is an interesting development in the field of programming languages. We briefly explore some of the history of Java in our first chapter, but for now, suffice it to say that it is very much a language for the new millennium. Part of the "buzz" around Java is because it is the language of the World Wide Web (itself a major aspect of computing now and in the future),but a significant feature of the language is that it is object-oriented. This means that we can
think of systems as composed of a number of objects that provide and request services from each other to accomplish an overall task. The object model can be applied from the earliest stages of system requirements and design and carried through to coding, testing, and maintenance.
Rather than avoid or delay object orientation, we have decided to make objects our departure point. We present our study of Java in terms of objects from the very start rather than begin as a conventional programming text and graft on objects at a later point.
We believe that Java is an elegant language with an extensive class library that supports the programming of graphical interfaces and the use of computer networks. We therefore believe it is a good language with which to introduce the concepts of computing, and it is rewarding because the programmer is able to write significant pieces of code quickly.
The book consists of five parts:
· In Part 1, we concentrate on using objects. Here objects are supplied either from the standard Java library of classes or from example classes of our own creation. The programmer writes applications that call upon the services of these preexisting objects and interprets the results.
· In Part 2, we change emphasis and look at what is required to write (and use) objects of our own.
· In Part 3, we explore a major concept in object models, that of inheritance, and how we can apply it in Java. This part also covers two areas of Java that rely heavily on inheri-tance: the use of exceptions for dealing with error conditions in programs and the Abstract Windowing Toolkit (ATT), which supports the creation of graphical user interfaces.
· In Part 4, we examine more advanced features of Java, such as the input/output facilities, and how applets--applications that can be executed over the World Wide Web can be written. We also examine how common data structures can be realized in Java,introducing ideas about the use of pointers and linked data structures.
· In Part 5, we look at object-oriented design and show how we can identify objects from the requirements of a system and move forward through design and into coding. This section is supported by examining the development of a system from start to finish.
We cover all the major parts of the basic language except multithreading, which we introduce only briefly in Chapter 21. In a book of this size, it is possible to cover only a small part of the extensive facilities provided by Java. Thus, we introduce only a few of the main classes of the ATT, and we consider access to networks only in terms of the communication between an applet and its originating computer. Some of the sections are marked as optional on a first
reading. We see Java as having a future as a good general-purpose programming language rather than as only a vehicle for Web programming. We therefore spend most of the book dis-
cussing normal application programs and discuss how applets can be written to be executed over the Web only in Chapter 20.
The book contains many examples and exercises to illustrate the principles discussed. All example programs are also available through a Web site (see Appendix A).
A number of environments are available for developing Java programs. This book describes Version 1.3 of Java, and the development environment it describes is the basic Software Development Kit (SDK) from Sun Microsystems. The examples have all been tested under the Windows NT and UNIX operating systems. Appendix A describes how to obtain a copy of the SDK, as well as the special classes used in this book.
We believe that Java is an exciting development in programming language design. We hope you will enjoy this introduction to the world of Java programming.
R. G.G. &J.A.M.
Lancaster
Part I Using Objects
Programming and the Java Language
1.1 Programs and Programming 2
1.2 Algorithms 3
1.3 High-Level Languages and Programs 5
1.4 A Simple Computer 8
1.5 Machine Code 9
1.6 Files and the Filing System 14
1.7 The World Wide Web 15
1.8 The Java Programming Language 17
1.9 Getting Started with Java 20
1.10 A First Java Program 21
1.11 The pti nt and println Methods 24
1.12 How This Book Is Arranged 25
1.13 Key Points in Chapter 1 26
1.14 Exercises 27
2 Object Orientation 29
2.1 Objects and Classes 29
2.2 Software Objects 32
2.3 More about Single Objects 36
2.4 An Object-Oriented Program 39
2.5 Types 40
2.6 Classes and Instances Revisited 43
2.7 Key Points in Chapter 2 46
2.8 Exercises 47
3 Declaring Objects and Calling Methods 48
3.1 Program Structure 48
3.2 The class Person 49
3.3 A Program to Manipulate a Person 51
3.4 The i mp0rt Statement 52
3.5 Declaring Objects 53
3.6 Using Methods to Set the Attributes of Objects 57
3.7 Using Methods to Extract Object Attributes 60
3.8 Using Constants in Java 63
3.9 Using Objects and Methods 65
3.10 A Variety of Methods 66
3.11 Constructors Revisited 68
3.12 Input to a Program (Optional) 72
3.13 Key Points in Chapter 3 73
3.14 Exercises 74
4 Selecting Among Alternatives 77
4.1 Reading Values from the Keyboard 77
4.2 More on Integer Variables 80
4.3 Type Checking 84
4.4 Making Decisions 85
4.5 Selection Statements in Java 89
4.5.1 Statements 90
4.5.2 Relational Operators and Boolean Expressions 92
4.6 The Boolean Type 96
4.7 The switch Statement 101
4.8 Testing a New Class 107
4.9 Key Points in Chapter 4 110
4.10 Exercises 111
5 Repetition 113
5.1 Repetition as a Basic Control Structure 113
5.2 Looping a Predetermined Number of Times: The for Statement 115
5.3 Looping an Indeterminate Number of Times: The whi 1 e Statement 121
5.4 Stopping in the Middle of an Iteration 124
5.5 for Loops and while Loops 126
5.6 Nested Loops 127
5.7 Boolean Expressions for Loops 126
5.8 Testing at the End of the Loop 134
5.9 Other Java Loop Features 136
5.10 Key Points in Chapter 5 138
5.11 Exercises 139
6 Basic Java Data Types 142
6.1 Objects and Basic Data Types 142
6.1.1 Declaration 142
6.1.2 Setting a Value 144
6.1.3 Obtaining a Value 146
6.1.4 Arguments to Methods 147
6.2 The int Data Type 147
6.3 Other Whole Number Data Types in Java (Optional) 151
6.4 Floating-Point Data Types 153
6.5 The b001 ean Data Type 155
6.6 The char Data Type 156
6.7 The String Class 157
6.8 Methods for the String Class 161
6.9 Wrapper Classes (Optional) 162
6.10 Key Points in Chapter 6 165
6.11 Exercises 166
Part 2 Writing Objects
7 A Simple Class 169
7.1 What We Are Trying to Achieve 169
7.2 Providing the Person Class 172
7.3 Methods for the Person Class 175
7.4 Actual and Formal Arguments 178
7.5 Modes of Argument Passing 182
7.6 Return Values 185
7.7 Lexical Conventions within a Class 185
7.8 Key Points in Chapter 7 186
7.9 Exercises 187
8 More on the Simple Class 189
8.1 Constructor Methods 189
8.2 Overloading 193
8.3 Class Constants 195
8.4 Class Variables 196
8.5 Private Methods 198
8.6 Class or Static Methods 201
8.7 Revisiting the Main Class 202
8.8 Packages and Directories 206
8.8.1 The i mp0rt Statement 209
8.9 Scope and Visibility 210
8.9.1 Intraclass Visibility 210
8.9.2 Use ofthi s 214
8.9.3 Interclass Visibility 214
8.10 Key Points in Chapter 8 215
8.11 Exercises 216
9 Arrays 218
9.1 Collections of Elements 218
9.2 Arrays of Objects 221
9.3 Searching an Array 223
9.4 Binary Search 226
9.5 Sorting an Array 227
9.6 Arrays as Arguments 234
9.7 Multidimensional Arrays 237
9.8 Nonrectangular Arrays (Optional) 241
9.9 Key Points in Chapter 9 242
9.10 Exercises 244
10 Objects within Objects 249
10.1 What We Are Trying to Achieve 249
10.2 Writing the 0urDate Class 250
10.3 Using the OurDate Class 251
10.4 Objects as Arguments 253
10.5 Multiple References to the Same Object 255
10.6 Objects as Arguments and Return Values: Call by Reference 260
10.6.1 Changing the Contents of the Formal and Actual Arguments 263
10.7 Hiding References to Other Objects 266
10.8 Key Points in Chapter 10 267
10.9 Exercises 268
11 Putting Objects to Work 270
11.1 A Task Organizer Program 270
11.2 A Priority Queue Class 271
11.3 Implementing a Priority Queue with an Array 275
11.4 Alternative Implementations of PriorityQueue (Optional) 281
11.5 Testing the PriorityQueue Class 282
11.6 Using the PriorityQueue Class 287
11.7 Outstanding Issues 289
11.8 Key Points in Chapter 11 291
11.9 Exercises 291
Part 3 Advanced Objects
12 Introduction to Inheritance 293
12.1 Motivation 293
12.1.1 Data Modeling 293
12.1.2 Programming 298
12.2 What's the Difference 299
12.3 Overriding Inherited Methods 300
12.4 Access Rights and Subclasses 304
12.5 Airplane Reservations: An Example 305
12.6 Key Points in Chapter 12 310
12.7 Exercises 311
13 Class and Method Polymorphism 313
13.1 Person and Student: An Example 313
13.2 Constructor Methods and Inheritance 314
13.2.1 Constructor Chaining 315
13.3 Multiple Levels of Inheritance: The Inheritance Hierarchy 316
13.4 The Class Object 317
13.5 Polymorphism 319
13.6 Polymorphism and Heterogeneous Collections 322
13.6.1 Dynamic Method Binding (Late Binding) 325
13.7 Calling Overridden Methods 325
13.8 Methods in Derived Classes 327
13.9 Key Points in Chapter 13 329
13.10 Exercises 330
14 Abstract Classes and Interfaces 333
14.1 Abstract Classes 333
14.2 Polymorphism 338
14.3 Interfaces 340
14.4 Key Points in Chapter 14 346
14.5 Exercises 347
15 Throwing and Catching Exceptions 351
15.1 Motivation: Robust Programs 351
15.2 Defining a New Exception 353
15.3 Throwing an Exception 354
15.4 Catching an Exception 355
15.4.1 The finally Clause (Optional) 361
15.5 Key Points in Chapter 15 363
15.6 Exercises 365
16 Graphics and the Abstract Windowing Toolkit 366
16.1 Graphical User Interfaces 366
16.2 A Simple Program with a Graphical Interface 368
16.3 Writing the Chapterl6n0 Class 372
16.3.1 The Constructor for the Chapterl6n0 Class 372
16.3.2 Other Layout Managers 375
16.3.3 The main Method for the Chapterl6n0 Class 376
16.3.4 The actionPerformed Method of the Chapterl6nO Class376
16.3.5 The windowClosing Method of the Chapterl6n0 Class 377
16.4 Writing the CanvasO Class 378
16.5 Writing Text on the Canvas 382
16.6 Animating the Simple Graphics Program 383
16.7 Input of Character Strings in a Graphical Interface 386
16.7.1 Setting Up the Picture 387
16.7.2 Getting a String from a WextField 389
16.7.3 Drawing the Thermometer 391
16.8 Menus, Files, and Images (Optional) 392
16.8.1 Setting Up Menus 393
16.8.2 Selecting a File 397
16.8.3 Displaying an Image 398
16.8.4 Tracking the Mouse 399
16.9 Key Points in Chapter 16 400
16.10 Exercises 401
Part 4 Advanced Java
17 Linked Data Structures 406
17.1 Linear and Linked Data Structures 406
17.2 Implementing a Priority Queue Using a Linked Data Structure 409
17.3 Methods for the PriorityOueue Class 413
17.3.1 The length Method 413
17.3.2 The first Method 415
17.3.3 The remove Method 4t6
17.4 The insert Method 417
17.5 Deletion from a Linked Data Structure (Optional) 423
17.6 Doubly Linked Lists (Optional) 425
17.7 Using Linked Data Structures 428
17.8 Key Points in Chapter 17 429
17.9 Exercises 430
18 Recursion and Binary Trees 434
18.1 Recursion 434
18.2 Solving the Towers of Hanoi Problem 437
18.2.1 A Recursive Solution to the Towers of Hanoi Problem 439
18.2.2 An Iterative Solution to the Towers of Hanoi Problem 441
18.3 Binary Trees 442
18.3.1 Searching and Updating a Binary Tree 445
18.3.2 Writing the Code for the Binary Tree 447
18.3.3 Adding a Word Occurrence to the Lexicon 448
18.3.4 Outputting the Lexicon Information 451
18.4 Key Points in Chapter 18 452
18.5 Exercises 452
19 Input and Output in Java 457
19.1 Input and Output Systems 457
19.2 The Java Classes for Input and Output 460
19.3 The PrintStream Class and System.out 462
19.3.1 Output Redirection 465
19.4 The BufferedReader Class and System. in 466
19.4.1 Tokenizing an Input Line 467
19.4.2 Converting Strings to Numeric Values 468
19.4.3 Redirecting Input 470
19.5 Files and File Handling 471
19.6 Reading and Writing Files 472
19.6.1 Writing to a File 473
19.6.2 Reading from a File 474
19.7 Binary Files (Optional) 476
19.8 Random Access Files (Optional) 477
19.9 Accessing Other Computers (Optional) 478
19.10 Key Points in Chapter 19 479
19.11 Exercises 481
20 Creating and Using Applets 484
20.1 Creating Applets 484
20.2 Using Applets 486
20.3 More about Applets 489
20.4 A Useful Applet 490
20.4.1 ThereadIndex Method 493
20.4.2 The act i0nPerf0rmed Method 494
20.5 Security Aspects of the Use of Applets 495
20.6 Key Points in Chapter 20 496
20.7 Exercises 496
21 Other Features of Java 499
21.1 Vectors and Other Java Data Structures 499
21.1.1 The Vector Class 499
21.1.2 The Hashtable Class 503
21.2 Strings and StringBuffers 505
21.3 Run-Time Type Information (Optional) 506
21.4 Threads (Optional) 507
21.4.1 Synchronizing Threads 513
21.5 Key Points in Chapter 21 514
21.6 Exercises 515
Part 5 Object-Oriented Design
22 Object-Oriented Design 518
22.1 Software Engineering 518
22.2 The Software Life Cycle 519
22.2.1 Requirements 520
22.2.2 Design 520
22.2.3 Coding 520
22.2.4 Testing 520
22.2.5 Maintenance 520
22.3 Design 521
22.3.1 The Design Process 521
22.3.2 Functional Design 522
22.4 Object-Oriented Design (OOD) 522
22.4.1 Capturing Our Design: A Design Notation 522
22.4.2 Object Identification 524
22.5 Key Points in Chapter 22 527
22.6 Exercises 528
22.7 References 529
23 Case Study: Implementing the Personal Organizer 1 530
23.1 First Steps in the Design 530
23.2 File Organization 530
23.2.1 Index Sequential Access 530
23.2.2 The Main File 532
23.2.3 The Rand0mAccessFile Class 535
23.2.4 The Index 536
23.2.5 Suitability of the Vector Class for Internal Representation of the Index 536
23.2.6 Suitability of the Hashtabl e Class for Internal Representation of the Index 537
23.2.7 Using the Vector Class Indirectly 538
23.3 The Classes in Detail 538
23.3.1 Filing System Considerations 540
23.3.2 Clientship 542
23.4 Moving toward Implementation 544
23.4.1 The DirBase Class 544
23.4.2 The DirEntry Class 544
23.4.3 The IndexElem Class 546
23.4.4 The Index Class 546
23.5 Key Points in Chapter 23 547
23.6 Exercise 548
23.7 Reference 548
24 Case Study: Implementing the Personal Organizer 2 549
24.1 Completing the Implementation 549
24.2 Implementation of DirBase, Index, IndexElem, and DirEntry 549
24.2.1 D irEntry Class Source and Commentary 549
24.2.2 IndexElem Class Source and Commentary 552
24.2.3 Index Class Source and Commentary 554
24.2.4 DirBase Class Source and Commentary 560
24.3 Testing What We Have Done So Far 564
24.3.1 Using a StreamTokenizer 564
24.3.2 Test-Based Interface: Intermediate Application and Testing 568
24.3.3 What Are We Testing 570
24.4 Graphical User Interface: The Final Prototype Application 571
24.4.1 Testing the Graphical Interface 574
24.5 Using Inheritance 575
24.6 Key Points in Chapter 24 576
24.7 Exercises 577
25 Criteria for a Good Object-Oriented Design 579
25.1 Introduction 579
25.2 Cohesion 579
25.3 Coupling 582
25.3.1 The Law of Demeter 583
25.4 Clarity 584
25.5 Extensibility of Our Design 585
25.5.1 Adding an Email Attribute to a Directory Entry 585
25.5.2 Adding a Diary Feature to the Personal Organizer 586
25.6 Key Points in Chapter 25 586
25.7 Exercises 587
25.8 References 587
Part 6 Appendixes
A Getting Started with Java 588
B Keywords in Java 589
C ASCII and Unicode Characters 590
D Program Listings 593
D. 1 Person. Java 593
D.2 Chapter20n2. java 599
D.3 The GUI Source Code for the Java Persona] Organizer 604
D.3.1 The Gui Class 604
D.3.2 The DirGui Class 606
D.3.3 The BrowseRecGui Class 608
D.3.4 The NewRecGui Class 610
D.3.5 The AlertDialog Class 612
Index 614