ощ 'ршорвшэш! цвн-эориэу ощ 'sajbpossv # PIPG DUJ 'ssjmoossv ^ PIPG pipa w н OX ЛЮН VAV
Contents Preface Chapter 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.20 Introduction to Computers and Java Applets Introduction What is a Computer? Computer Organization Evolution of Operating Systems Personal Computing, Distributed Computing, and Client/Server Computing Machine Languages, Assembly Languages, and High-level Languages History of C++ History of Java Java Class Libraries Other High-level Languages Structured Programming Basics of a Typical Java Environment A Tour of the Book General Notes About Java and this Book Introduction to Java Programming A Simple Program: Printing a Line of Text Another Java Program: Adding Integers Memory Concepts Arithmetic Decision Making: Equality and Relational Operators 1 2 6 7 8 8 9 10 11 12 13 13 14 16 20 22 22 27 34 35 38 Practices Performance Tips Portability Tip Software Engineering Observations Self-Review Exercises Answers to Self- Review Exercises Exercises
viii CONTENTS Chapter 2 Developing Java Applications 60 2.1 Introduction 62 2.2 Algorithms 62 2.3 Pseudocode 63 2.4 Control Structures 63 2.5 The if Selection Structure 66 2.6 The if/else Selection Structure 68 2.7 The while Repetition Structure 72 2.8 Formatting Algorithms: Case Study 1 (Counter-Controlled Repetition) 74 2.9 Formulating Algorithms with Top-down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) 79 2.10 Formulating Algorithms with Top-down, Stepwise Refinement: Case Study 3 (Nested Control Structures) 86 2.11 Assignment Operators 90 2.12 Increment and Decrement Operators 91 2.13 Primitive Data Types 94 2.14 Common Escape Sequences 95 Practices Performance Tips Portability Tip Software Engineering Observations Testing and Debugging Tip Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 3 Control Structures 110 3.1 Introduction 112 3.2 Essentials of Counter-Controlled Repetition 112 3.3 The for Repetition Structure 114 3.4 Examples Using the for Structure 118 3.5 The switch Multiple-Selection Structure 122 3.6 The do/while Repetition Structure 127 3.7 The break and continue Structure 128 3.8 The Labeled break and continue Statements 131 3.9 Logical Operators 131 3.10 Structured Programming Summary 138 Practices Performance Tips Software Engineering Observation Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 4 Methods 154 4.1 Introduction 156 4.2 Program Modules in Java 156 4.3 Math Class Methods 157 4.4 Methods 159 4.5 Method Definitions 160 4.6 Java API Packages 166
CONTENTS IX 4.7 Random Number Generation 168 4.8 Example: A Game of Chance 171 4.9 Automatic Variables 176 4.10 Scope Rules 177 4.11 Recursion 180 4.12 Example Using Recursion: The Fibonacci Series 183 4.13 Recursion vs. Iteration 187 4.14 Method Overloading 190 4.15 Methods of the Applet Class 191 Practices Performance Tips Portability Tips Software Engineering Observations Testing and Debugging Tip Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 5 Arrays 214 5.1 Introduction 216 5.2 Arrays 216 5.3 Declaring and Allocating Arrays 218 5.4 Examples Using Arrays 219 5.5 Passing Arrays to Methods 229 5.6 Sorting Arrays 231 5.7 Searching Arrays: Linear Search and Binary Search 231 5.8 Multiple-Subscripted Arrays 235 Practices Performance Tips Software Engineering Observations Testing and Debugging Tip Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 6 Object-Based Programming 272 6.1 Introduction 274 6.2 Implementing a Time Abstract Data Type with a Class 275 6.3 Class Scope 280 6.4 Controlling Access to Members 280 6.5 Utility Methods 282 6.6 Initializing Class Objects: Constructors 286 6.7 Using Overloaded Constructors 287 6.8 Using Set and Get Methods 290 6.9 Software Reusability 297 6.10 Final Instance Variables 297 6.11 Composition: Objects as Instance Variables of Other Classes 299 6.12 Friendly Members 302 6.13 Using the this Reference 304 6.14 Finalizers 308 6.15 Static Class Members 309
x CONTENTS Chapter 7 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 7.10 7.11 7.12 7.13 7.14 7.15 7.16 7.17 7.18 6.16 Data Abstraction and Information Hiding 313 Practices Performance Tips Software Engineering Observations Testing and Debugging Tip Self-Review Exercises Answers to Self-Review Exercises Exercises Object-Oriented Programming 324 Introduction 326 Superclasses and Subclasses 328 protected Members 330 Relationship between Superclass Objects and Subclass Objects 330 Using Constructors and Finalizers in Subclasses 335 Implicit Subclass-Object-to-Superclass-Object Conversion 339 Software Engineering with Inheritance 340 Composition Vs. Inheritance 341 Case Study: Point, Circle, Cylinder 341 Introduction to Polymorphism 347 Type Fields and Switch Statements 347 Dynamic Method Binding 348 final Methods and Classes 348 Abstract Superclasses and Concrete Classes 349 Polymorphism Examples 350 Case Study: A Payroll System Using Polymorphism 352 New Classes and Dynamic Binding 359 Case Study: Inheriting Interface and Implementation 360 Type Wrapper Classes for Primitive Types 366 7.19 Summary Terminology Common Programming Errors Performance Tip Software Engineering Observations Testing and Debugging Tips Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 8 Strings and Characters 376 8.1 Introduction 378 8.2 Fundamentals of Characters and Strings 379 8.3 String Constructors 379 8.4 String Methods length, charat, getchars, getbytes 382 8.5 Comparing Strings 385 8.6 String Method hashcode 391 8.7 Locating Characters and Substrings in Strings 392 8.8 Extracting Substrings from Strings 394 8.9 Concatenating Strings 395 8.10 Miscellaneous String Methods 397 8.11 Using String valueof and copyvalueof 398 8.12 String Method intern 401 8.13 StringBuff er Class 403
CONTENTS XI 8.14 StringBuff er Constructors 404 8.15 StringBuff er length, capacity, setlength and ensure- Capacity Methods 404 8.16 StringBuff er charat, setcharat and getchars Methods 407 8.17 StringBuff er append Methods 408 8.18 StringBuff er insert Methods 410 8.19 Character Class Examples 412 8.20 Class StringTokenizer 420 8.21 A Card Shuffling and Dealing Simulation 423 Summary Terminology Common Programming Errors * Performance Tips Self- Review Exercises Answers to Self-Review Exercises Exercises Special Section: Advanced String Manipulation Exercises Special Section: Challenging String Manipulation Projects Chapter 9 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 9.10 9.11 9.12 9.13 9.14 Graphics Introduction Graphics Contexts and Graphics Objects Drawing Strings, Characters, and Bytes Color Control Font Control Drawing Lines Drawing Rectangles Drawing Rounded Rectangles Drawing 3-Dimensional Rectangles Drawing Ovals Drawing Arcs Drawing Polygons Screen Manipulation Paint Modes Summary Terminology Common Programming Errors Portability Tips 438 440 441 442 445 449 459 460 462 464 466 466 469 474 476 Soft- ware Engineering Observation Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 10 10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9 Basic Graphical User Interface Components Introduction Labels Push Buttons TextFields Choice Buttons Checkboxes and Radio Buttons Lists Panels Mouse Events 486 488 490 493 497 502 505 511 516 517
хп CONTENTS 10.10 Keyboard Events 525 10.11 FlowLayout Layout Manager 531 Practices Portability Tip Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 11 Advanced Graphical User Interface Components 556 11.1 Introduction 558 11.2 Text Areas 559 11.3 Canvases 562 11.4 Scrollbars 568 11.5 Custom Components 572 11.6 Frames 575 11.7 Menus 586 11.8 Dialogs 596 11.9 Advanced Layout Managers 604 11.10 CardLayout Layout Manager 604 11.11 GridBagLayout Layout Manager 608 11.12 Not Using a Layout Manager 617 11.13 Programmer-defined Layout Managers 619 Practices Performance Tip Portability Tip Software Engineering Observations Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 12 Exception Handling 632 12.1 Introduction 622 12.2 When Exception Handling Should Be Used 637 12.3 Other Error-Handling Techniques 637 12.4 The Basics of Java Exception Handling 637 12.5 A Simple Exception Handling Example: Divide by Zero 638 12.6 Try Blocks 642 12.7 Throwing an Exception 643 12.8 Catching an Exception 644 12.9 Rethrowing an Exception 646 12.10 Throws Clause 646 12.11 Constructors, Finalizers, and Exception Handling 649 12.12 Exceptions and Inheritance 652 12.13 finally Block 652 12.14 Using printstacktrace and getmessage 657 Practices Performance Tips Software Engineering Observations Testing and Debugging Tips Self-Review Exercises Answers to Self-Review Exercises Exercises
CONTENTS хш Chapter 13 Multithreading 668 13.1 Introduction 670 13.2 Class Thread: An Overview of the Thread Methods 672 13.3 Thread States: Life-Cycle of a Thread 674 13.4 Thread Priorities and Thread Scheduling 675 13.5 Thread Synchronization 679 13.6 Producer/Consumer Relationship without Thread Synchronization 680 13.7 Producer/Consumer Relationship with Thread Synchronization 684 13.8 Producer/Consumer Relationship: The Circular Buffer 688 13.9 Daemon Threads 693 13.10 Runnable Interface 694 13.11 Thread Groups 698 Summary Terminology Common Programming Errors Performance Tips Portability Tip Software Engineering Observation Testing and Debugging Tips Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 14 Multimedia: Images, Animation and Audio 710 14.1 Introduction 712 14.2 Loading, Displaying and Scaling Images 713 14.3 Introduction to Animation: An Image Loop 715 14.4 Graphics Double Buffering 719 14.5 Using the MediaTracker to Monitor Image Loading 722 14.6 Using a Separate Thread to Run an Animation 426 14.7 Loading and Playing Audio Clips 729 14.8 Customizing Applets via the HTML param Tag 730 14.9 Image Maps 736 Summary Terminology Good Programming Practices Performance Tips Software Engineering Observation Self-Review Exercises Answers to Self-Review Exercises Exercises Special Section: Building Your Own Compiler Chapter 15 Files and Streams 750 15.1 Introduction 752 15.2 The Data Hierarchy 753 15.3 Files and Streams 755 15.4 Creating a Sequential Access File 759 15.5 Reading Data from a Sequential Access File 765 15.6 Updating Sequential Access Files 775 15.7 Random Access Files 775 15.8 Creating a Random Access File 776 15.9 Writing Data Randomly to a Random Access File 778 15.10 Reading Data Sequentially from a Random Access File 783 15.11 Example: A Transaction Processing Program 788 15.12 Class File 799 15.13 Input/Output of Objects 803
xiv CONTENTS Practices Performance Tips Software Engineering Observation Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 16 Networking 816 16.1 Introduction 818 16.2 Manipulating URLs 819 16.3 Using a URL Stream Connection to Read a File on a Server 822 16.4 Establishing a Simple Server (Using Stream Sockets) 824 16.5 Establishing a Simple Client (Using Stream Sockets) 825 16.6 Client/Server Interaction with Stream Socket Connections 826 16.7 Connectionless Client/Server Interaction with Datagrams 831 16.8 Client/Server Tic-Tac-Toe Using a Multithreaded Server 838 16.9 Security and the Network 849 16.10 Forthcoming Networking and Security APIs 852 Summary Terminology Common Programming Errors Performance Tips Portability Tip Software Engineering Observations Self-Review Exercises Answers to Self-Review Exercises Exercises Chapter 17 17.1 17.2 17.3 17.4 17.5 17.6 Data Structures Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues 17.7 Trees Practices Performance Tips Self-Review Exercises Answers to Self-Review Exercises Exercises Special Section: Building Your Own Compiler 860 862 863 863 864 874 877 880 Chapter 18 Java Utilities Package and Bit Manipulation 910 18.1 Introduction 912 18.2 Vector Class and Enumeration Interface 913 18.3 Stack Class 918 18.4 Dictionary Class 921 18.5 HashTable Class 922 18.6 Date Class 927 18.7 Observable Class and Observer Interface 929 18.8 Properties Class 932 18.9 Random Class 937 18.10 Bit Manipulation an the Bitwise Operators 938 18.11 BitSet Class 951
CONTENTS XV Summary Terminology Common Programming Errors Performance Tips Testing and Debugging Tips Self-Review Exercises Answers to Self-Review Exercises Exercises Appendix A A.l A.2 Demos Demos Demos 964 964 965 Appendix В B.l B.2 B.3 B.4 B.5 B.6 Java Resources Java Resources Java Frequently Asked Questions (FAQs) Tutorials for Learning Java Java Books Java Newsgroups Virtual Reality Modeling Language (VRML) 969 961 971 972 972 972 972 Appendix С Operator Precedence Chart 973 Appendix D ASCII Character Set 975 Appendix E E.l E.2 E.3 E.4 E.5 E.6 Number Systems 976 Introduction 977 Abbreviating Binary Numbers as Octal Numbers and Hexadecimal Numbers 980 Converting from Octal Numbers and Hexadecimal Numbers to Binary Numbers 981 Converting from Binary, Octal, or Hexadecimal to Decimal 981 Converting from Decimal to Binary, Octal, or Hexadecimal 982 Negative Binary Numbers: Two's Complement Notation 984 Summary Terminology cises Exercises Self-Review Exercises Answers to Self-Review Exer- Appendix F Object-Oriented Elevator Simulator F. 1 Introduction F.2 Problem Statement F.3 Elevator Laboratory Assignment 1 F.4 Elevator Laboratory Assignment 2 F.5 Elevator Laboratory Assignment 3 F.6 Elevator Laboratory Assignment 4 F.7 Elevator Laboratory Assignment 5 F.8 Elevator Laboratory Assignment 6 F.9 Elevator Laboratory Assignment 7 990 991 992 993 993 995 996 997 997 998
xvi CONTENTS!! F. 10 Elevator Laboratory Assignment 8 F. 11 Elevator Laboratory Assignment 9 F. 12 Elevator Laboratory Assignment 10 F. 13 Elevator Laboratory Assignment 11 F. 14 Elevator Laboratory Assignment 12 F. 15 Recommended Modifications 999 999 1000 1000 1001 1001 Bibliography 1003 Index 1007