java : how to program - 7e

1705

Upload: chih-pei-wen

Post on 15-Jul-2015

342 views

Category:

Education


4 download

TRANSCRIPT

  • Table of ContentsCopyright..................................................................................................... 1Deitel Series Page..................................................................................... 5Preface........................................................................................................ 8Before You Begin....................................................................................... 21Chapter 1. Introduction to Computers, the Internet and the Web.............. 28

    Section 1.1. Introduction............................................................................................................................................................... 29Section 1.2. What Is a Computer?................................................................................................................................................. 31Section 1.3. Computer Organization............................................................................................................................................. 31Section 1.4. Early Operating Systems........................................................................................................................................... 32Section 1.5. Personal, Distributed and Client/Server Computing............................................................................................... 33Section 1.6. The Internet and the World Wide Web..................................................................................................................... 33Section 1.7. Machine Languages, Assembly Languages and High-Level Languages................................................................... 34Section 1.8. History of C and C++................................................................................................................................................. 35Section 1.9. History of Java........................................................................................................................................................... 36Section 1.10. Java Class Libraries................................................................................................................................................. 36Section 1.11. Fortran, COBOL, Pascal and Ada............................................................................................................................. 37Section 1.12. BASIC, Visual Basic, Visual C++, C# and .NET...................................................................................................... 38Section 1.13. Typical Java Development Environment................................................................................................................ 38Section 1.14. Notes about Java and Java How to Program, 7/e................................................................................................... 42Section 1.15. Test-Driving a Java Application.............................................................................................................................. 43Section 1.16. Software Engineering Case Study: Introduction to Object Technology and the UML........................................... 47Section 1.17. Web 2.0..................................................................................................................................................................... 52Section 1.18. Software Technologies............................................................................................................................................. 53Section 1.19. Wrap-Up................................................................................................................................................................... 54Section 1.20. Web Resources........................................................................................................................................................ 55Summary....................................................................................................................................................................................... 57Terminology.................................................................................................................................................................................. 60Self-Review Exercises.................................................................................................................................................................... 61Answers to Self-Review Exercises................................................................................................................................................ 62Exercises........................................................................................................................................................................................ 62

    Chapter 2. Introduction to Java Applications............................................ 64Section 2.1. Introduction............................................................................................................................................................... 65Section 2.2. A First Program in Java: Printing a Line of Text..................................................................................................... 65Section 2.3. Modifying Our First Java Program........................................................................................................................... 72Section 2.4. Displaying Text with printf....................................................................................................................................... 75Section 2.5. Another Java Application: Adding Integers............................................................................................................. 76Section 2.6. Memory Concepts...................................................................................................................................................... 81Section 2.7. Arithmetic................................................................................................................................................................. 82Section 2.8. Decision Making: Equality and Relational Operators............................................................................................. 85Section 2.9. (Optional) Software Engineering Case Study: Examining the Requirements Document...................................... 90Section 2.10. Wrap-Up................................................................................................................................................................ 100Summary..................................................................................................................................................................................... 100Terminology................................................................................................................................................................................ 103Self-Review Exercises.................................................................................................................................................................. 104Answers to Self-Review Exercises............................................................................................................................................... 105Exercises...................................................................................................................................................................................... 107

    Chapter 3. Introduction to Classes and Objects......................................... 111Section 3.1. Introduction............................................................................................................................................................. 112Section 3.2. Classes, Objects, Methods and Instance Variables................................................................................................. 112Section 3.3. Declaring a Class with a Method and Instantiating an Object of a Class............................................................... 114Section 3.4. Declaring a Method with a Parameter.................................................................................................................... 118Section 3.5. Instance Variables, set Methods and get Methods.................................................................................................. 121Section 3.6. Primitive Types vs. Reference Types....................................................................................................................... 126Section 3.7. Initializing Objects with Constructors..................................................................................................................... 127Section 3.8. Floating-Point Numbers and Type double............................................................................................................. 130

    Java How to Program, Seventh Edition

    Java How to Program, Seventh EditionJava How to Program, Seventh Edition By H. M. Deitel - Deitel & Associates, Inc., P. J.Deitel - Deitel & Associates, Inc. ISBN: 9780136085676 Publisher: Prentice Hall

    Prepared for Peter Disney, Safari ID: [email protected] by Peter Disney

    Print Publication Date: 2006/12/27 User number: 1839029 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.

  • Section 3.9. (Optional) GUI and Graphics Case Study: Using Dialog Boxes............................................................................. 135Section 3.10. (Optional) Software Engineering Case Study: Identifying the Classes in a Requirements Document............... 138Section 3.11. Wrap-Up................................................................................................................................................................. 146Summary..................................................................................................................................................................................... 146Terminology................................................................................................................................................................................. 149Self-Review Exercises.................................................................................................................................................................. 150Answers to Self-Review Exercises............................................................................................................................................... 151Exercises...................................................................................................................................................................................... 152

    Chapter 4. Control Statements: Part 1...................................................... 154Section 4.1. Introduction............................................................................................................................................................. 155Section 4.2. Algorithms............................................................................................................................................................... 155Section 4.3. Pseudocode.............................................................................................................................................................. 156Section 4.4. Control Structures................................................................................................................................................... 156Section 4.5. if Single-Selection Statement.................................................................................................................................. 159Section 4.6. if...else Double-Selection Statement....................................................................................................................... 160Section 4.7. while Repetition Statement..................................................................................................................................... 165Section 4.8. Formulating Algorithms: Counter-Controlled Repetition..................................................................................... 166Section 4.9. Formulating Algorithms: Sentinel-Controlled Repetition...................................................................................... 171Section 4.10. Formulating Algorithms: Nested Control Statements.......................................................................................... 179Section 4.11. Compound Assignment Operators........................................................................................................................ 184Section 4.12. Increment and Decrement Operators................................................................................................................... 185Section 4.13. Primitive Types...................................................................................................................................................... 188Section 4.14. (Optional) GUI and Graphics Case Study: Creating Simple Drawings................................................................ 188Section 4.15. (Optional) Software Engineering Case Study: Identifying Class Attributes......................................................... 193Section 4.16. Wrap-Up................................................................................................................................................................. 197Summary..................................................................................................................................................................................... 198Terminology................................................................................................................................................................................ 203Self-Review Exercises................................................................................................................................................................. 204Answers to Self-Review Exercises.............................................................................................................................................. 206Exercises...................................................................................................................................................................................... 207

    Chapter 5. Control Statements: Part 2...................................................... 213Section 5.1. Introduction............................................................................................................................................................. 214Section 5.2. Essentials of Counter-Controlled Repetition.......................................................................................................... 214Section 5.3. for Repetition Statement......................................................................................................................................... 216Section 5.4. Examples Using the for Statement......................................................................................................................... 220Section 5.5. do...while Repetition Statement............................................................................................................................. 225Section 5.6. switch Multiple-Selection Statement...................................................................................................................... 226Section 5.7. break and continue Statements.............................................................................................................................. 234Section 5.8. Logical Operators.................................................................................................................................................... 236Section 5.9. Structured Programming Summary....................................................................................................................... 242Section 5.10. (Optional) GUI and Graphics Case Study: Drawing Rectangles and Ovals......................................................... 247Section 5.11. (Optional) Software Engineering Case Study: Identifying Objects States and Activities................................... 250Section 5.12. Wrap-Up................................................................................................................................................................ 254Summary..................................................................................................................................................................................... 255Terminology................................................................................................................................................................................ 259Self-Review Exercises................................................................................................................................................................. 260Answers to Self-Review Exercises............................................................................................................................................... 261Exercises...................................................................................................................................................................................... 262

    Chapter 6. Methods: A Deeper Look........................................................ 266Section 6.1. Introduction............................................................................................................................................................. 267Section 6.2. Program Modules in Java....................................................................................................................................... 268Section 6.3. static Methods, static Fields and Class Math......................................................................................................... 269Section 6.4. Declaring Methods with Multiple Parameters....................................................................................................... 272Section 6.5. Notes on Declaring and Using Methods................................................................................................................. 276Section 6.6. Method-Call Stack and Activation Records............................................................................................................ 277Section 6.7. Argument Promotion and Casting.......................................................................................................................... 278Section 6.8. Java API Packages................................................................................................................................................... 279Section 6.9. Case Study: Random-Number Generation............................................................................................................. 281Section 6.10. Case Study: A Game of Chance (Introducing Enumerations).............................................................................. 287

    Java How to Program, Seventh Edition

    Java How to Program, Seventh EditionJava How to Program, Seventh Edition By H. M. Deitel - Deitel & Associates, Inc., P. J.Deitel - Deitel & Associates, Inc. ISBN: 9780136085676 Publisher: Prentice Hall

    Prepared for Peter Disney, Safari ID: [email protected] by Peter Disney

    Print Publication Date: 2006/12/27 User number: 1839029 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.

  • Section 6.11. Scope of Declarations............................................................................................................................................. 291Section 6.12. Method Overloading............................................................................................................................................. 294Section 6.13. (Optional) GUI and Graphics Case Study: Colors and Filled Shapes.................................................................. 297Section 6.14. (Optional) Software Engineering Case Study: Identifying Class Operations...................................................... 300Section 6.15. Wrap-Up................................................................................................................................................................ 307Summary..................................................................................................................................................................................... 308Terminology................................................................................................................................................................................. 311Self-Review Exercises.................................................................................................................................................................. 312Answers to Self-Review Exercises............................................................................................................................................... 314Exercises...................................................................................................................................................................................... 316

    Chapter 7. Arrays.................................................................................... 322Section 7.1. Introduction............................................................................................................................................................. 323Section 7.2. Arrays...................................................................................................................................................................... 323Section 7.3. Declaring and Creating Arrays................................................................................................................................ 325Section 7.4. Examples Using Arrays........................................................................................................................................... 326Section 7.5. Case Study: Card Shuffling and Dealing Simulation.............................................................................................. 335Section 7.6. Enhanced for Statement......................................................................................................................................... 339Section 7.7. Passing Arrays to Methods..................................................................................................................................... 340Section 7.8. Case Study: Class GradeBook Using an Array to Store Grades............................................................................. 344Section 7.9. Multidimensional Arrays........................................................................................................................................ 349Section 7.10. Case Study: Class GradeBook Using a Two-Dimensional Array.......................................................................... 353Section 7.11. Variable-Length Argument Lists........................................................................................................................... 359Section 7.12. Using Command-Line Arguments........................................................................................................................ 360Section 7.13. (Optional) GUI and Graphics Case Study: Drawing Arcs..................................................................................... 362Section 7.14. (Optional) Software Engineering Case Study: Collaboration Among Objects..................................................... 365Section 7.15. Wrap-Up................................................................................................................................................................. 373Summary..................................................................................................................................................................................... 374Terminology................................................................................................................................................................................ 376Self-Review Exercises.................................................................................................................................................................. 377Answers to Self-Review Exercises.............................................................................................................................................. 378Exercises...................................................................................................................................................................................... 378Special Section: Building Your Own Computer......................................................................................................................... 388

    Chapter 8. Classes and Objects: A Deeper Look....................................... 395Section 8.1. Introduction............................................................................................................................................................ 396Section 8.2. Time Class Case Study............................................................................................................................................ 397Section 8.3. Controlling Access to Members.............................................................................................................................. 401Section 8.4. Referring to the Current Objects Members with the this Reference.................................................................... 402Section 8.5. Time Class Case Study: Overloaded Constructors................................................................................................. 404Section 8.6. Default and No-Argument Constructors................................................................................................................ 409Section 8.7. Notes on Set and Get Methods................................................................................................................................ 410Section 8.8. Composition............................................................................................................................................................. 411Section 8.9. Enumerations.......................................................................................................................................................... 414Section 8.10. Garbage Collection and Method finalize............................................................................................................... 417Section 8.11. static Class Members............................................................................................................................................. 418Section 8.12. static Import.......................................................................................................................................................... 423Section 8.13. final Instance Variables......................................................................................................................................... 424Section 8.14. Software Reusability............................................................................................................................................. 426Section 8.15. Data Abstraction and Encapsulation.................................................................................................................... 427Section 8.16. Time Class Case Study: Creating Packages.......................................................................................................... 429Section 8.17. Package Access...................................................................................................................................................... 434Section 8.18. (Optional) GUI and Graphics Case Study: Using Objects with Graphics............................................................ 436Section 8.19. (Optional) Software Engineering Case Study: Starting to Program the Classes of the ATM System................. 439Section 8.20. Wrap-Up............................................................................................................................................................... 445Summary..................................................................................................................................................................................... 445Terminology................................................................................................................................................................................ 448Self-Review Exercise................................................................................................................................................................... 449Answers to Self-Review Exercise................................................................................................................................................ 450Exercises...................................................................................................................................................................................... 450

    Chapter 9. Object-Oriented Programming: Inheritance.......................... 454

    Java How to Program, Seventh Edition

    Java How to Program, Seventh EditionJava How to Program, Seventh Edition By H. M. Deitel - Deitel & Associates, Inc., P. J.Deitel - Deitel & Associates, Inc. ISBN: 9780136085676 Publisher: Prentice Hall

    Prepared for Peter Disney, Safari ID: [email protected] by Peter Disney

    Print Publication Date: 2006/12/27 User number: 1839029 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.

  • Section 9.1. Introduction............................................................................................................................................................. 455Section 9.2. Superclasses and Subclasses................................................................................................................................... 456Section 9.3. protected Members................................................................................................................................................. 458Section 9.4. Relationship between Superclasses and Subclasses.............................................................................................. 459Section 9.5. Constructors in Subclasses..................................................................................................................................... 483Section 9.6. Software Engineering with Inheritance................................................................................................................. 489Section 9.7. Object Class............................................................................................................................................................. 490Section 9.8. (Optional) GUI and Graphics Case Study: Displaying Text and Images Using Labels......................................... 492Section 9.9. Wrap-Up................................................................................................................................................................. 494Summary..................................................................................................................................................................................... 495Terminology................................................................................................................................................................................ 496Self-Review Exercises................................................................................................................................................................. 496Answers to Self-Review Exercises............................................................................................................................................... 497Exercises...................................................................................................................................................................................... 497

    Chapter 10. Object-Oriented Programming: Polymorphism.................... 498Section 10.1. Introduction........................................................................................................................................................... 499Section 10.2. Polymorphism Examples...................................................................................................................................... 501Section 10.3. Demonstrating Polymorphic Behavior................................................................................................................. 502Section 10.4. Abstract Classes and Methods.............................................................................................................................. 505Section 10.5. Case Study: Payroll System Using Polymorphism............................................................................................... 507Section 10.6. final Methods and Classes..................................................................................................................................... 522Section 10.7. Case Study: Creating and Using Interfaces........................................................................................................... 523Section 10.8. (Optional) GUI and Graphics Case Study: Drawing with Polymorphism........................................................... 535Section 10.9. (Optional) Software Engineering Case Study: Incorporating Inheritance into the ATM System....................... 537Section 10.10. Wrap-Up.............................................................................................................................................................. 545Summary..................................................................................................................................................................................... 545Terminology................................................................................................................................................................................ 547Self-Review Exercises................................................................................................................................................................. 548Answers to Self-Review Exercises.............................................................................................................................................. 548Exercises...................................................................................................................................................................................... 548

    Chapter 11. GUI Components: Part 1........................................................ 550Section 11.1. Introduction............................................................................................................................................................ 551Section 11.2. Simple GUI-Based Input/Output with JOptionPane............................................................................................ 552Section 11.3. Overview of Swing Components............................................................................................................................ 555Section 11.4. Displaying Text and Images in a Window............................................................................................................. 558Section 11.5. Text Fields and an Introduction to Event Handling with Nested Classes............................................................ 563Section 11.6. Common GUI Event Types and Listener Interfaces............................................................................................. 569Section 11.7. How Event Handling Works.................................................................................................................................. 572Section 11.8. JButton................................................................................................................................................................... 573Section 11.9. Buttons That Maintain State.................................................................................................................................. 577Section 11.10. JComboBox and Using an Anonymous Inner Class for Event Handling........................................................... 583Section 11.11. JList....................................................................................................................................................................... 587Section 11.12. Multiple-Selection Lists....................................................................................................................................... 589Section 11.13. Mouse Event Handling......................................................................................................................................... 592Section 11.14. Adapter Classes..................................................................................................................................................... 597Section 11.15. JPanel Subclass for Drawing with the Mouse..................................................................................................... 600Section 11.16. Key-Event Handling............................................................................................................................................. 604Section 11.17. Layout Managers.................................................................................................................................................. 607Section 11.18. Using Panels to Manage More Complex Layouts................................................................................................. 617Section 11.19. JTextArea.............................................................................................................................................................. 618Section 11.20. Wrap-Up............................................................................................................................................................... 621Summary..................................................................................................................................................................................... 622Terminology................................................................................................................................................................................ 628Self-Review Exercises................................................................................................................................................................. 630Answers to Self-Review Exercises............................................................................................................................................... 631Exercises...................................................................................................................................................................................... 631

    Chapter 12. Graphics and Java 2D........................................................ 636Section 12.1. Introduction........................................................................................................................................................... 637Section 12.2. Graphics Contexts and Graphics Objects............................................................................................................. 639

    Java How to Program, Seventh Edition

    Java How to Program, Seventh EditionJava How to Program, Seventh Edition By H. M. Deitel - Deitel & Associates, Inc., P. J.Deitel - Deitel & Associates, Inc. ISBN: 9780136085676 Publisher: Prentice Hall

    Prepared for Peter Disney, Safari ID: [email protected] by Peter Disney

    Print Publication Date: 2006/12/27 User number: 1839029 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.

  • Section 12.3. Color Control......................................................................................................................................................... 640Section 12.4. Font Control.......................................................................................................................................................... 647Section 12.5. Drawing Lines, Rectangles and Ovals................................................................................................................... 653Section 12.6. Drawing Arcs......................................................................................................................................................... 657Section 12.7. Drawing Polygons and Polylines........................................................................................................................... 660Section 12.8. Java 2D API........................................................................................................................................................... 663Section 12.9. Wrap-Up................................................................................................................................................................ 670Summary..................................................................................................................................................................................... 670Terminology................................................................................................................................................................................ 672Self-Review Exercises.................................................................................................................................................................. 673Answers to Self-Review Exercises............................................................................................................................................... 674Exercises...................................................................................................................................................................................... 674

    Chapter 13. Exception Handling.............................................................. 679Section 13.1. Introduction........................................................................................................................................................... 680Section 13.2. Exception-Handling Overview.............................................................................................................................. 681Section 13.3. Example: Divide by Zero without Exception Handling........................................................................................ 682Section 13.4. Example: Handling ArithmeticExceptions and InputMismatchExceptions....................................................... 684Section 13.5. When to Use Exception Handling......................................................................................................................... 689Section 13.6. Java Exception Hierarchy..................................................................................................................................... 690Section 13.7. finally Block........................................................................................................................................................... 693Section 13.8. Stack Unwinding................................................................................................................................................... 697Section 13.9. printStackTrace, getStackTrace and getMessage................................................................................................. 699Section 13.10. Chained Exceptions.............................................................................................................................................. 701Section 13.11. Declaring New Exception Types.......................................................................................................................... 703Section 13.12. Preconditions and Postconditions....................................................................................................................... 704Section 13.13. Assertions............................................................................................................................................................. 705Section 13.14. Wrap-Up.............................................................................................................................................................. 706Summary..................................................................................................................................................................................... 707Terminology................................................................................................................................................................................. 710Self-Review Exercises.................................................................................................................................................................. 710Answers to Self-Review Exercises................................................................................................................................................ 711Exercises...................................................................................................................................................................................... 712

    Chapter 14. Files and Streams.................................................................. 713Section 14.1. Introduction............................................................................................................................................................ 714Section 14.2. Data Hierarchy....................................................................................................................................................... 715Section 14.3. Files and Streams................................................................................................................................................... 717Section 14.4. Class File................................................................................................................................................................ 719Section 14.5. Sequential-Access Text Files................................................................................................................................. 723Section 14.6. Object Serialization............................................................................................................................................... 739Section 14.7. Additional java.io Classes...................................................................................................................................... 748Section 14.8. Opening Files with JFileChooser.......................................................................................................................... 750Section 14.9. Wrap-Up................................................................................................................................................................ 754Summary..................................................................................................................................................................................... 754Terminology................................................................................................................................................................................ 756Self-Review Exercises.................................................................................................................................................................. 757Answers to Self-Review Exercises............................................................................................................................................... 759Exercises...................................................................................................................................................................................... 760

    Chapter 15. Recursion............................................................................. 764Section 15.1. Introduction........................................................................................................................................................... 765Section 15.2. Recursion Concepts............................................................................................................................................... 766Section 15.3. Example Using Recursion: Factorials................................................................................................................... 767Section 15.4. Example Using Recursion: Fibonacci Series......................................................................................................... 770Section 15.5. Recursion and the Method-Call Stack................................................................................................................... 773Section 15.6. Recursion vs. Iteration........................................................................................................................................... 774Section 15.7. Towers of Hanoi..................................................................................................................................................... 776Section 15.8. Fractals................................................................................................................................................................... 779Section 15.9. Recursive Backtracking......................................................................................................................................... 790Section 15.10. Wrap-Up.............................................................................................................................................................. 790Section 15.11. Internet and Web Resources................................................................................................................................ 791

    Java How to Program, Seventh Edition

    Java How to Program, Seventh EditionJava How to Program, Seventh Edition By H. M. Deitel - Deitel & Associates, Inc., P. J.Deitel - Deitel & Associates, Inc. ISBN: 9780136085676 Publisher: Prentice Hall

    Prepared for Peter Disney, Safari ID: [email protected] by Peter Disney

    Print Publication Date: 2006/12/27 User number: 1839029 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.

  • Summary..................................................................................................................................................................................... 792Terminology................................................................................................................................................................................ 794Self-Review Exercises.................................................................................................................................................................. 794Answers to Self-Review Exercises............................................................................................................................................... 795Exercises...................................................................................................................................................................................... 795

    Chapter 16. Searching and Sorting.......................................................... 800Section 16.1. Introduction........................................................................................................................................................... 801Section 16.2. Searching Algorithms........................................................................................................................................... 802Section 16.3. Sorting Algorithms................................................................................................................................................. 811Section 16.4. Invariants.............................................................................................................................................................. 826Section 16.5. Wrap-Up................................................................................................................................................................ 827Summary..................................................................................................................................................................................... 828Terminology................................................................................................................................................................................ 829Self-Review Exercises................................................................................................................................................................. 830Answers to Self-Review Exercises.............................................................................................................................................. 830Exercises..................................................................................................................................................................................... 830

    Chapter 17. Data Structures..................................................................... 833Section 17.1. Introduction........................................................................................................................................................... 834Section 17.2. Type-Wrapper Classes for Primitive Types........................................................................................................... 835Section 17.3. Autoboxing and Auto-Unboxing........................................................................................................................... 835Section 17.4. Self-Referential Classes......................................................................................................................................... 836Section 17.5. Dynamic Memory Allocation................................................................................................................................. 837Section 17.6. Linked Lists............................................................................................................................................................ 837Section 17.7. Stacks..................................................................................................................................................................... 848Section 17.8. Queues................................................................................................................................................................... 852Section 17.9. Trees....................................................................................................................................................................... 854Section 17.10. Wrap-Up............................................................................................................................................................... 861Summary..................................................................................................................................................................................... 861Terminology................................................................................................................................................................................ 863Self-Review Exercises................................................................................................................................................................. 864Answers to Self-Review Exercises.............................................................................................................................................. 865Exercises..................................................................................................................................................................................... 866

    Chapter 18. Generics............................................................................... 886Section 18.1. Introduction........................................................................................................................................................... 887Section 18.2. Motivation for Generic Methods.......................................................................................................................... 888Section 18.3. Generic Methods: Implementation and Compile-Time Translation................................................................... 890Section 18.4. Additional Compile-Time Translation Issues: Methods That Use a Type Parameter as the Return Type......... 893Section 18.5. Overloading Generic Methods.............................................................................................................................. 896Section 18.6. Generic Classes...................................................................................................................................................... 897Section 18.7. Raw Types.............................................................................................................................................................. 907Section 18.8. Wildcards in Methods That Accept Type Parameters........................................................................................... 911Section 18.9. Generics and Inheritance: Notes........................................................................................................................... 915Section 18.10. Wrap-Up.............................................................................................................................................................. 916Section 18.11. Internet and Web Resources................................................................................................................................ 916Summary..................................................................................................................................................................................... 916Terminology................................................................................................................................................................................. 919Self-Review Exercises.................................................................................................................................................................. 919Answers to Self-Review Exercises............................................................................................................................................... 919Exercises..................................................................................................................................................................................... 920

    Chapter 19. Collections............................................................................ 921Section 19.1. Introduction........................................................................................................................................................... 922Section 19.2. Collections Overview............................................................................................................................................. 923Section 19.3. Class Arrays........................................................................................................................................................... 924Section 19.4. Interface Collection and Class Collections............................................................................................................ 927Section 19.5. Lists....................................................................................................................................................................... 928Section 19.6. Collections Algorithms.......................................................................................................................................... 939Section 19.7. Stack Class of Package java.util............................................................................................................................. 952Section 19.8. Class PriorityQueue and Interface Queue............................................................................................................. 955Section 19.9. Sets......................................................................................................................................................................... 956

    Java How to Program, Seventh Edition

    Java How to Program, Seventh EditionJava How to Program, Seventh Edition By H. M. Deitel - Deitel & Associates, Inc., P. J.Deitel - Deitel & Associates, Inc. ISBN: 9780136085676 Publisher: Prentice Hall

    Prepared for Peter Disney, Safari ID: [email protected] by Peter Disney

    Print Publication Date: 2006/12/27 User number: 1839029 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.

  • Section 19.10. Maps..................................................................................................................................................................... 959Section 19.11. Properties Class.................................................................................................................................................... 964Section 19.12. Synchronized Collections..................................................................................................................................... 967Section 19.13. Unmodifiable Collections.................................................................................................................................... 967Section 19.14. Abstract Implementations................................................................................................................................... 968Section 19.15. Wrap-Up.............................................................................................................................................................. 968Summary..................................................................................................................................................................................... 969Terminology................................................................................................................................................................................ 972Self-Review Exercises.................................................................................................................................................................. 973Answers to Self-Review Exercises............................................................................................................................................... 973Exercises...................................................................................................................................................................................... 974

    Chapter 20. Introduction to Java Applets................................................ 976Section 20.1. Introduction........................................................................................................................................................... 977Section 20.2. Sample Applets Provided with the JDK............................................................................................................... 977Section 20.3. Simple Java Applet: Drawing a String................................................................................................................. 982Section 20.4. Applet Life-Cycle Methods................................................................................................................................... 987Section 20.5. Initializing an Instance Variable with Method init.............................................................................................. 988Section 20.6. Sandbox Security Model....................................................................................................................................... 990Section 20.7. Internet and Web Resources................................................................................................................................ 990Section 20.8. Wrap-Up................................................................................................................................................................ 991Summary..................................................................................................................................................................................... 991Terminology................................................................................................................................................................................ 993Self-Review Exercise................................................................................................................................................................... 993Answers to Self-Review Exercise................................................................................................................................................ 993Exercises...................................................................................................................................................................................... 994

    Chapter 21. Multimedia: Applets and Applications.................................. 995Section 21.1. Introduction........................................................................................................................................................... 996Section 21.2. Loading, Displaying and Scaling Images.............................................................................................................. 997Section 21.3. Animating a Series of Images............................................................................................................................. 1000Section 21.4. Image Maps......................................................................................................................................................... 1005Section 21.5. Loading and Playing Audio Clips........................................................................................................................ 1008Section 21.6. Playing Video and Other Media with Java Media Framework............................................................................ 1011Section 21.7. Wrap-Up............................................................................................................................................................... 1016Section 21.8. Web Resources..................................................................................................................................................... 1016Summary.................................................................................................................................................................................... 1017Terminology............................................................................................................................................................................... 1018Self-Review Exercises................................................................................................................................................................ 1019Answers to Self-Review Exercises............................................................................................................................................. 1019Exercises.................................................................................................................................................................................... 1020Special Section: Challenging Multimedia Projects................................................................................................................... 1020

    Chapter 22. GUI Components: Part 2..................................................... 1023Section 22.1. Introduction......................................................................................................................................................... 1024Section 22.2. JSlider................................................................................................................................................................. 1024Section 22.3. Windows: Additional Notes................................................................................................................................ 1028Section 22.4. Using Menus with Frames.................................................................................................................................. 1029Section 22.5. JPopupMenu....................................................................................................................................................... 1037Section 22.6. Pluggable Look-and-Feel.................................................................................................................................... 1040Section 22.7. JDesktopPane and JInternalFrame.................................................................................................................... 1044Section 22.8. JTabbedPane....................................................................................................................................................... 1048Section 22.9. Layout Managers: BoxLayout and GridBagLayout............................................................................................ 1050Section 22.10. Wrap-Up............................................................................................................................................................ 1063Summary................................................................................................................................................................................... 1064Terminology.............................................................................................................................................................................. 1065Self-Review Exercises................................................................................................................................................................ 1067Answers to Self-Review Exercises............................................................................................................................................. 1067Exercises.................................................................................................................................................................................... 1068

    Chapter 23. Multithreading................................................................... 1070Section 23.1. Introduction......................................................................................................................................................... 1071Section 23.2. Thread States: Life Cycle of a Thread................................................................................................................. 1073

    Java How to Program, Seventh Edition

    Java How to Program, Seventh EditionJava How to Program, Seventh Edition By H. M. Deitel - Deitel & Associates, Inc., P. J.Deitel - Deitel & Associates, Inc. ISBN: 9780136085676 Publisher: Prentice Hall

    Prepared for Peter Disney, Safari ID: [email protected] by Peter Disney

    Print Publication Date: 2006/12/27 User number: 1839029 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.

  • Section 23.3. Thread Priorities and Thread Scheduling........................................................................................................... 1075Section 23.4. Creating and Executing Threads......................................................................................................................... 1077Section 23.5. Thread Synchronization...................................................................................................................................... 1081Section 23.6. Producer/Consumer Relationship without Synchronization............................................................................ 1090Section 23.7. Producer/Consumer Relationship: ArrayBlockingQueue.................................................................................. 1097Section 23.8. Producer/Consumer Relationship with Synchronization.................................................................................. 1100Section 23.9. Producer/Consumer Relationship: Bounded Buffers........................................................................................ 1106Section 23.10. Producer/Consumer Relationship: The Lock and Condition Interfaces.......................................................... 1114Section 23.11. Multithreading with GUI.................................................................................................................................... 1120Section 23.12. Other Classes and Interfaces in java.util.concurrent........................................................................................ 1135Section 23.13. Wrap-Up............................................................................................................................................................. 1135Summary.................................................................................................................................................................................... 1136Terminology............................................................................................................................................................................... 1142Self-Review Exercises................................................................................................................................................................ 1143Answers to Self-Review Exercises............................................................................................................................................. 1144Exercises.................................................................................................................................................................................... 1144

    Chapter 24. Networking......................................................................... 1146Section 24.1. Introduction......................................................................................................................................................... 1147Section 24.2. Manipulating URLs............................................................................................................................................. 1148Section 24.3. Reading a File on a Web Server........................................................................................................................... 1153Section 24.4. Establishing a Simple Server Using Stream Sockets........................................................................................... 1157Section 24.5. Establishing a Simple Client Using Stream Sockets........................................................................................... 1158Section 24.6. Client/Server Interaction with Stream Socket Connections............................................................................... 1159Section 24.7. Connectionless Client/Server Interaction with Datagrams................................................................................. 1171Section 24.8. Client/Server Tic-Tac-Toe Using a Multithreaded Server.................................................................................. 1178Section 24.9. Security and the Network.................................................................................................................................... 1193Section 24.10. [Web Bonus] Case Study: DeitelMessenger Server and Client......................................................................... 1194Section 24.11. Wrap-Up............................................................................................................................................................. 1194Summary.................................................................................................................................................................................... 1194Terminology............................................................................................................................................................................... 1196Self-Review Exercises................................................................................................................................................................ 1197Answers to Self-Review Exercises............................................................................................................................................. 1197Exercises.................................................................................................................................................................................... 1198

    Chapter 25. Accessing Databases with JDBC.......................................... 1201Section 25.1. Introduction......................................................................................................................................................... 1202Section 25.2. Relational Databases........................................................................................................................................... 1203Section 25.3. Relational Database Overview: The books Database......................................................................................... 1204Section 25.4. SQL...................................................................................................................................................................... 1207Section 25.5. Instructions for installing MySQL and MySQL Connector/J............................................................................. 1216Section 25.6. Instructions for Setting Up a MySQL User Account........................................................................................... 1217Section 25.7. Creating Database books in MySQL.................................................................................................................... 1218Section 25.8. Manipulating Databases with JDBC................................................................................................................... 1219Section 25.9. RowSet Interface................................................................................................................................................. 1236Section 25.10. Java DB/Apache Derby..................................................................................................................................... 1239Section 25.11. PreparedStatements........................................................................................................................................... 1254Section 25.12. Stored Procedures.............................................................................................................................................. 1256Section 25.13. Transaction Processing...................................................................................................................................... 1256Section 25.14. Wrap-Up............................................................................................................................................................. 1257Section 25.15. Web Resources and Recommended Readings.................................................................................................. 1257Summary.................................................................................................................................................................................... 1259Terminology.............................................................................................................................................................................. 1264Self-Review Exercise................................................................................................................................................................. 1265Answers to Self-Review Exercise.............................................................................................................................................. 1265Exercises.................................................................................................................................................................................... 1265

    Chapter 26. Web Applications: Part 1..................................................... 1268Section 26.1. Introduction......................................................................................................................................................... 1269Section 26.2. Simple HTTP Transactions................................................................................................................................. 1270Section 26.3. Multitier Application Architecture...................................................................................................................... 1272Section 26.4. Java Web Technologies....................................................................................................................................... 1273

    Java How to Program, Seventh Edition

    Java How to Program, Seventh EditionJava How to Program, Seventh Edition By H. M. Deitel - Deitel & Associates, Inc., P. J.Deitel - Deitel & Associates, Inc. ISBN: 9780136085676 Publisher: Prentice Hall

    Prepared for Peter Disney, Safari ID: [email protected] by Peter Disney

    Print Publication Date: 2006/12/27 User number: 1839029 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other userequires prior written consent from the copyright owner. Unauthorized use, reproduction and/or distribution are strictly prohibited and violate applicable laws. All rights reserved.

  • Section 26.5. Creating and Running a Simple Application in Java Studio Creator 2.............................................................. 1276Section 26.6. JSF Components................................................................................................................................................. 1293Section 26.7. Session Tracking................................................................................................................................................. 1308Section 26.8. Wrap-Up.............................................................................................................................................................. 1336Section 26.9. Web Resources.................................................................................................................................................... 1337Summary................................................................................................................................................................................... 1338Terminology.............................................................................................................................................................................. 1346Self-Review Exercises................................................................................................................................................................ 1347Answers to Self-Review Exercises............................................................................................................................................. 1347Exercises.................................................................................................................................................................................... 1348

    Chapter 27. Web Applications: Part 2.................................................... 1349Section 27.1. Introduction......................................................................................................................................................... 1350Section 27.2. Accessing Databases in Web Applications.......................................................................................................... 1351Section 27.3. Ajax-Enabled JSF Components.......................................................................................................................... 1363Section 27.4. AutoComplete Text Field and Virtual Forms...................................................................................................... 1365Section 27.5. Google Maps Map Viewer Component................................................................................................................ 1374Section 27.6. Wrap-Up.............................................................................................................................................................. 1386Section 27.7. Web Resources.................................................................................................................................................... 1386Summary.................................................................................................................................................................................... 1387Terminol