preface xxv before you begin xxxix - deitel & associates · x contents 2.6 memory concepts 52...

16
Preface xxv Before You Begin xxxix 1 Introduction to Computers, the Internet and the Web 1 1.1 Introduction 2 1.2 Computers: Hardware and Software 3 1.3 Computer Organization 4 1.4 Early Operating Systems 5 1.5 Personal, Distributed and Client/Server Computing 6 1.6 The Internet and the World Wide Web 6 1.7 Machine Languages, Assembly Languages and High-Level Languages 7 1.8 History of C and C++ 8 1.9 History of Java 8 1.10 Java Class Libraries 9 1.11 Fortran, COBOL, Pascal and Ada 10 1.12 BASIC, Visual Basic, Visual C++, C# and .NET 11 1.13 Typical Java Development Environment 11 1.14 Notes about Java and Java How to Program, Eighth Edition 14 1.15 Test-Driving a Java Application 15 1.16 Software Engineering Case Study: Introduction to Object Technology and the UML 20 1.17 Web 2.0 24 1.18 Software Technologies 25 1.19 Wrap-Up 26 1.20 Web Resources 26 2 Introduction to Java Applications 37 2.1 Introduction 38 2.2 Our First Program in Java: Printing a Line of Text 38 2.3 Modifying Our First Java Program 44 2.4 Displaying Text with printf 46 2.5 Another Application: Adding Integers 47 ©2010 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected under all copyright laws as they currently exist. No portion of this material may be reproduced in any form or by any means, without permission in writing from the publisher. For the exclusive use of adopters of the book, Java (TM) How to Program, 8th Edition, by Deitel and Deitel. ISBN 0-13-605306-8.

Upload: hatuyen

Post on 18-Jun-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

Preface xxv

Before You Begin xxxix

1 Introduction to Computers, the Internet and the Web 1

1.1 Introduction 21.2 Computers: Hardware and Software 31.3 Computer Organization 41.4 Early Operating Systems 51.5 Personal, Distributed and Client/Server Computing 61.6 The Internet and the World Wide Web 61.7 Machine Languages, Assembly Languages and High-Level Languages 71.8 History of C and C++ 81.9 History of Java 81.10 Java Class Libraries 91.11 Fortran, COBOL, Pascal and Ada 101.12 BASIC, Visual Basic, Visual C++, C# and .NET 111.13 Typical Java Development Environment 111.14 Notes about Java and Java How to Program, Eighth Edition 141.15 Test-Driving a Java Application 151.16 Software Engineering Case Study: Introduction to Object Technology

and the UML 201.17 Web 2.0 241.18 Software Technologies 251.19 Wrap-Up 261.20 Web Resources 26

2 Introduction to Java Applications 372.1 Introduction 382.2 Our First Program in Java: Printing a Line of Text 382.3 Modifying Our First Java Program 442.4 Displaying Text with printf 462.5 Another Application: Adding Integers 47

jhtp8TOC.fm Page ix Sunday, March 1, 2009 6:40 PM

©2010 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected under all copyright laws as they currently exist. No portion of this material may be reproduced in any form or by any means, without permission in writing from the publisher. For the exclusive use of adopters of the book, Java (TM) How to Program, 8th Edition, by Deitel and Deitel. ISBN 0-13-605306-8.

x Contents

2.6 Memory Concepts 522.7 Arithmetic 532.8 Decision Making: Equality and Relational Operators 562.9 Wrap-Up 60

3 Introduction to Classes and Objects 723.1 Introduction 733.2 Classes, Objects, Methods and Instance Variables 733.3 Declaring a Class with a Method and Instantiating an Object of a Class 753.4 Declaring a Method with a Parameter 783.5 Instance Variables, set Methods and get Methods 823.6 Primitive Types vs. Reference Types 873.7 Initializing Objects with Constructors 883.8 Floating-Point Numbers and Type double 913.9 (Optional) GUI and Graphics Case Study: Using Dialog Boxes 953.10 Wrap-Up 98

4 Control Statements: Part I 1064.1 Introduction 1074.2 Algorithms 1074.3 Pseudocode 1084.4 Control Structures 1084.5 if Single-Selection Statement 1104.6 if…else Double-Selection Statement 1114.7 while Repetition Statement 1164.8 Formulating Algorithms: Counter-Controlled Repetition 1174.9 Formulating Algorithms: Sentinel-Controlled Repetition 1224.10 Formulating Algorithms: Nested Control Statements 1294.11 Compound Assignment Operators 1344.12 Increment and Decrement Operators 1354.13 Primitive Types 1384.14 (Optional) GUI and Graphics Case Study: Creating Simple Drawings 1384.15 Wrap-Up 142

5 Control Statements: Part 2 1565.1 Introduction 1575.2 Essentials of Counter-Controlled Repetition 1575.3 for Repetition Statement 1595.4 Examples Using the for Statement 1625.5 do…while Repetition Statement 1675.6 switch Multiple-Selection Statement 1695.7 break and continue Statements 1765.8 Logical Operators 1785.9 Structured Programming Summary 184

jhtp8TOC.fm Page x Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Contents xi

5.10 (Optional) GUI and Graphics Case Study: Drawing Rectangles and Ovals 1895.11 Wrap-Up 192

6 Methods: A Deeper Look 2026.1 Introduction 2036.2 Program Modules in Java 2036.3 static Methods, static Fields and Class Math 2056.4 Declaring Methods with Multiple Parameters 2076.5 Notes on Declaring and Using Methods 2106.6 Method-Call Stack and Activation Records 2116.7 Argument Promotion and Casting 2126.8 Java API Packages 2136.9 Case Study: Random-Number Generation 215

6.9.1 Generalized Scaling and Shifting of Random Numbers 2196.9.2 Random-Number Repeatability for Testing and Debugging 220

6.10 Case Study: A Game of Chance; Introducing Enumerations 2206.11 Scope of Declarations 2256.12 Method Overloading 2286.13 (Optional) GUI and Graphics Case Study: Colors and Filled Shapes 2316.14 Wrap-Up 234

7 Arrays and ArrayLists 2487.1 Introduction 2497.2 Arrays 2507.3 Declaring and Creating Arrays 2517.4 Examples Using Arrays 2527.5 Case Study: Card Shuffling and Dealing Simulation 2617.6 Enhanced for Statement 2647.7 Passing Arrays to Methods 2667.8 Case Study: Class GradeBook Using an Array to Store Grades 2697.9 Multidimensional Arrays 2747.10 Case Study: Class GradeBook Using a Two-Dimensional Array 2787.11 Variable-Length Argument Lists 2847.12 Using Command-Line Arguments 2857.13 Class Arrays 2877.14 Introduction to Collections and Class ArrayList 2897.15 (Optional) GUI and Graphics Case Study: Drawing Arcs 2927.16 Wrap-Up 295

8 Classes and Objects: A Deeper Look 3168.1 Introduction 3178.2 Time Class Case Study 3178.3 Controlling Access to Members 3218.4 Referring to the Current Object’s Members with the this Reference 322

jhtp8TOC.fm Page xi Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

xii Contents

8.5 Time Class Case Study: Overloaded Constructors 3258.6 Default and No-Argument Constructors 3308.7 Notes on Set and Get Methods 3318.8 Composition 3328.9 Enumerations 3358.10 Garbage Collection and Method finalize 3388.11 static Class Members 3388.12 static Import 3428.13 final Instance Variables 3438.14 Time Class Case Study: Creating Packages 3468.15 Package Access 3518.16 (Optional) GUI and Graphics Case Study: Using Objects with Graphics 3528.17 Wrap-Up 356

9 Object-Oriented Programming: Inheritance 3659.1 Introduction 3669.2 Superclasses and Subclasses 3679.3 protected Members 3699.4 Relationship between Superclasses and Subclasses 370

9.4.1 Creating and Using a CommissionEmployee Class 3709.4.2 Creating and Using a BasePlusCommissionEmployee Class 3759.4.3 Creating a CommissionEmployee–BasePlusCommissionEmployee

Inheritance Hierarchy 3809.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance

Hierarchy Using protected Instance Variables 3839.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance

Hierarchy Using private Instance Variables 3869.5 Constructors in Subclasses 3909.6 Software Engineering with Inheritance 3919.7 Object Class 3929.8 (Optional) GUI and Graphics Case Study: Displaying Text and

Images Using Labels 3949.9 Wrap-Up 396

10 Object-Oriented Programming: Polymorphism 40010.1 Introduction 40110.2 Polymorphism Examples 40310.3 Demonstrating Polymorphic Behavior 40410.4 Abstract Classes and Methods 40610.5 Case Study: Payroll System Using Polymorphism 408

10.5.1 Abstract Superclass Employee 40910.5.2 Concrete Subclass SalariedEmployee 41210.5.3 Concrete Subclass HourlyEmployee 41410.5.4 Concrete Subclass CommissionEmployee 41510.5.5 Indirect Concrete Subclass BasePlusCommissionEmployee 417

jhtp8TOC.fm Page xii Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Contents xiii

10.5.6 Polymorphic Processing, Operator instanceof and Downcasting 41810.5.7 Summary of the Allowed Assignments Between Superclass and

Subclass Variables 42310.6 final Methods and Classes 42310.7 Case Study: Creating and Using Interfaces 424

10.7.1 Developing a Payable Hierarchy 42610.7.2 Interface Payable 42710.7.3 Class Invoice 42710.7.4 Modifying Class Employee to Implement Interface Payable 43010.7.5 Modifying Class SalariedEmployee for Use in the Payable

Hierarchy 43110.7.6 Using Interface Payable to Process Invoices and Employees

Polymorphically 43310.7.7 Common Interfaces of the Java API 435

10.8 (Optional) GUI and Graphics Case Study: Drawing with Polymorphism 43610.9 Wrap-Up 438

11 Exception Handling 44311.1 Introduction 44411.2 Error-Handling Overview 44511.3 Example: Divide by Zero without Exception Handling 44511.4 Example: Handling ArithmeticExceptions and

InputMismatchExceptions 44811.5 When to Use Exception Handling 45311.6 Java Exception Hierarchy 45311.7 finally Block 45611.8 Stack Unwinding 46111.9 printStackTrace, getStackTrace and getMessage 46211.10 Chained Exceptions 46511.11 Declaring New Exception Types 46711.12 Preconditions and Postconditions 46811.13 Assertions 46811.14 Wrap-Up 470

12 ATM Case Study, Part 1: Object-Oriented Design with the UML 47612.1 Case Study Introduction 47712.2 Examining the Requirements Document 47712.3 Identifying the Classes in a Requirements Document 48512.4 Identifying Class Attributes 49112.5 Identifying Objects’ States and Activities 49612.6 Identifying Class Operations 50012.7 Indicating Collaboration Among Objects 50612.8 Wrap-Up 513

jhtp8TOC.fm Page xiii Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

xiv Contents

13 ATM Case Study Part 2: Implementing an Object-Oriented Design 51713.1 Introduction 51813.2 Starting to Program the Classes of the ATM System 51813.3 Incorporating Inheritance and Polymorphism into the ATM System 523

13.3.1 Implementing the ATM System Design (Incorporating Inheritance) 52813.4 ATM Case Study Implementation 529

13.4.1 Class ATM 53013.4.2 Class Screen 53513.4.3 Class Keypad 53613.4.4 Class CashDispenser 53713.4.5 Class DepositSlot 53813.4.6 Class Account 53913.4.7 Class BankDatabase 54113.4.8 Class Transaction 54313.4.9 Class BalanceInquiry 54513.4.10 Class Withdrawal 54613.4.11 Class Deposit 55013.4.12 Class ATMCaseStudy 553

13.5 Wrap-Up 553

14 GUI Components: Part 1 55514.1 Introduction 55614.2 Java’s New Nimbus Look-and-Feel 55714.3 Simple GUI-Based Input/Output with JOptionPane 55814.4 Overview of Swing Components 56114.5 Displaying Text and Images in a Window 56314.6 Text Fields and an Introduction to Event Handling with Nested Classes 56814.7 Common GUI Event Types and Listener Interfaces 57414.8 How Event Handling Works 57614.9 JButton 57814.10 Buttons That Maintain State 581

14.10.1 JCheckBox 58114.10.2 JRadioButton 584

14.11 JComboBox and Using an Anonymous Inner Class for Event Handling 58714.12 JList 59114.13 Multiple-Selection Lists 59314.14 Mouse Event Handling 59614.15 Adapter Classes 60014.16 JPanel Subclass for Drawing with the Mouse 60414.17 Key Event Handling 60714.18 Introduction to Layout Managers 610

14.18.1 FlowLayout 61214.18.2 BorderLayout 61514.18.3 GridLayout 618

jhtp8TOC.fm Page xiv Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Contents xv

14.19 Using Panels to Manage More Complex Layouts 62014.20 JTextArea 62214.21 Wrap-Up 624

15 Graphics and Java 2D™ 64015.1 Introduction 64115.2 Graphics Contexts and Graphics Objects 64315.3 Color Control 64415.4 Manipulating Fonts 65115.5 Drawing Lines, Rectangles and Ovals 65615.6 Drawing Arcs 66015.7 Drawing Polygons and Polylines 66215.8 Java 2D API 66515.9 Wrap-Up 672

16 Strings, Characters and Regular Expressions 68116.1 Introduction 68216.2 Fundamentals of Characters and Strings 68216.3 Class String 683

16.3.1 String Constructors 68316.3.2 String Methods length, charAt and getChars 68416.3.3 Comparing Strings 68516.3.4 Locating Characters and Substrings in Strings 69016.3.5 Extracting Substrings from Strings 69216.3.6 Concatenating Strings 69216.3.7 Miscellaneous String Methods 69316.3.8 String Method valueOf 694

16.4 Class StringBuilder 69616.4.1 StringBuilder Constructors 69616.4.2 StringBuilder Methods length, capacity, setLength and

ensureCapacity 69716.4.3 StringBuilder Methods charAt, setCharAt, getChars and

reverse 69816.4.4 StringBuilder append Methods 70016.4.5 StringBuilder Insertion and Deletion Methods 701

16.5 Class Character 70316.6 Tokenizing Strings 70716.7 Regular Expressions, Class Pattern and Class Matcher 70816.8 Wrap-Up 717

17 Files, Streams and Object Serialization 72817.1 Introduction 72917.2 Data Hierarchy 730

jhtp8TOC.fm Page xv Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

xvi Contents

17.3 Files and Streams 73217.4 Class File 73317.5 Sequential-Access Text Files 738

17.5.1 Creating a Sequential-Access Text File 73817.5.2 Reading Data from a Sequential-Access Text File 74517.5.3 Case Study: A Credit-Inquiry Program 74717.5.4 Updating Sequential-Access Files 752

17.6 Object Serialization 75317.6.1 Creating a Sequential-Access File Using Object Serialization 75417.6.2 Reading and Deserializing Data from a Sequential-Access File 760

17.7 Additional java.io Classes 76217.7.1 Interfaces and Classes for Byte-Based Input and Output 76217.7.2 Interfaces and Classes for Character-Based Input and Output 764

17.8 Opening Files with JFileChooser 765

17.9 Wrap-Up 768

18 Recursion 77818.1 Introduction 77918.2 Recursion Concepts 78018.3 Example Using Recursion: Factorials 78118.4 Example Using Recursion: Fibonacci Series 78418.5 Recursion and the Method-Call Stack 78718.6 Recursion vs. Iteration 78918.7 Towers of Hanoi 79018.8 Fractals 79218.9 Recursive Backtracking 80318.10 Wrap-Up 803

19 Searching, Sorting and Big O 81119.1 Introduction 81219.2 Searching Algorithms 813

19.2.1 Linear Search 81319.2.2 Binary Search 817

19.3 Sorting Algorithms 82219.3.1 Selection Sort 82219.3.2 Insertion Sort 82619.3.3 Merge Sort 830

19.4 Wrap-Up 836

20 Generic Collections 84120.1 Introduction 84220.2 Collections Overview 84220.3 Type-Wrapper Classes for Primitive Types 843

jhtp8TOC.fm Page xvi Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Contents xvii

20.4 Autoboxing and Auto-Unboxing 84420.5 Interface Collection and Class Collections 84420.6 Lists 845

20.6.1 ArrayList and Iterator 84620.6.2 LinkedList 848

20.7 Collections Methods 85320.7.1 Method sort 85420.7.2 Method shuffle 85720.7.3 Methods reverse, fill, copy, max and min 85920.7.4 Method binarySearch 86120.7.5 Methods addAll, frequency and disjoint 863

20.8 Stack Class of Package java.util 86420.9 Class PriorityQueue and Interface Queue 86720.10 Sets 86820.11 Maps 87120.12 Properties Class 87420.13 Synchronized Collections 87720.14 Unmodifiable Collections 87820.15 Abstract Implementations 87920.16 Wrap-Up 879

21 Generic Classes and Methods 88621.1 Introduction 88721.2 Motivation for Generic Methods 88721.3 Generic Methods: Implementation and Compile-Time Translation 89021.4 Additional Compile-Time Translation Issues: Methods That Use a Type

Parameter as the Return Type 89321.5 Overloading Generic Methods 89621.6 Generic Classes 89621.7 Raw Types 90421.8 Wildcards in Methods That Accept Type Parameters 90821.9 Generics and Inheritance: Notes 91221.10 Wrap-Up 913

22 Custom Generic Data Structures 91722.1 Introduction 91822.2 Self-Referential Classes 91822.3 Dynamic Memory Allocation 91922.4 Linked Lists 92022.5 Stacks 93022.6 Queues 93422.7 Trees 93722.8 Wrap-Up 943

jhtp8TOC.fm Page xvii Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

xviii Contents

23 Applets and Java Web Start 95423.1 Introduction 95523.2 Sample Applets Provided with the JDK 95623.3 Simple Java Applet: Drawing a String 960

23.3.1 Executing WelcomeApplet in the appletviewer 96223.3.2 Executing an Applet in a Web Browser 964

23.4 Applet Life-Cycle Methods 96423.5 Initializing an Instance Variable with Method init 96523.6 Sandbox Security Model 96723.7 Java Web Start and the Java Network Launch Protocol (JNLP) 968

23.7.1 Packaging the DrawTest Applet for Use with Java Web Start 96923.7.2 JNLP Document for the DrawTest Applet 970

23.8 Wrap-Up 974

24 Multimedia: Applets and Applications 98024.1 Introduction 98124.2 Loading, Displaying and Scaling Images 98224.3 Animating a Series of Images 98824.4 Image Maps 99424.5 Loading and Playing Audio Clips 99724.6 Playing Video and Other Media with Java Media Framework 100024.7 Wrap-Up 100524.8 Web Resources 1005

25 GUI Components: Part 2 101325.1 Introduction 101425.2 JSlider 101425.3 Windows: Additional Notes 101825.4 Using Menus with Frames 101925.5 JPopupMenu 102725.6 Pluggable Look-and-Feel 103025.7 JDesktopPane and JInternalFrame 103425.8 JTabbedPane 103825.9 Layout Managers: BoxLayout and GridBagLayout 104025.10 Wrap-Up 1052

26 Multithreading 105826.1 Introduction 105926.2 Thread States: Life Cycle of a Thread 106126.3 Thread Priorities and Thread Scheduling 106326.4 Creating and Executing Threads 1065

26.4.1 Runnables and the Thread Class 106526.4.2 Thread Management with the Executor Framework 1068

jhtp8TOC.fm Page xviii Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Contents xix

26.5 Thread Synchronization 106926.5.1 Unsynchronized Data Sharing 107026.5.2 Synchronized Data Sharing—Making Operations Atomic 1074

26.6 Producer/Consumer Relationship without Synchronization 107826.7 Producer/Consumer Relationship: ArrayBlockingQueue 108526.8 Producer/Consumer Relationship with Synchronization 108826.9 Producer/Consumer Relationship: Bounded Buffers 109426.10 Producer/Consumer Relationship: The Lock and Condition Interfaces 110126.11 Multithreading with GUI 1108

26.11.1 Performing Computations in a Worker Thread 110926.11.2 Processing Intermediate Results with SwingWorker 1115

26.12 Interfaces Callable and Future 112226.13 Wrap-Up 1122

27 Networking 113127.1 Introduction 113227.2 Manipulating URLs 113327.3 Reading a File on a Web Server 113827.4 Establishing a Simple Server Using Stream Sockets 114127.5 Establishing a Simple Client Using Stream Sockets 114327.6 Client/Server Interaction with Stream Socket Connections 114427.7 Connectionless Client/Server Interaction with Datagrams 115627.8 Client/Server Tic-Tac-Toe Using a Multithreaded Server 116327.9 [Web Bonus] Case Study: DeitelMessenger Server and Client 117827.10 Wrap-Up 1178

28 Accessing Databases with JDBC 118428.1 Introduction 118528.2 Relational Databases 118628.3 Relational Database Overview: The books Database 118728.4 SQL 1190

28.4.1 Basic SELECT Query 119128.4.2 WHERE Clause 119228.4.3 ORDER BY Clause 119328.4.4 Merging Data from Multiple Tables: INNER JOIN 119528.4.5 INSERT Statement 119628.4.6 UPDATE Statement 119728.4.7 DELETE Statement 1198

28.5 Instructions for Installing MySQL and MySQL Connector/J 119928.6 Instructions for Setting Up a MySQL User Account 120028.7 Creating Database books in MySQL 120028.8 Manipulating Databases with JDBC 1201

28.8.1 Connecting to and Querying a Database 120128.8.2 Querying the books Database 1206

jhtp8TOC.fm Page xix Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

xx Contents

28.9 RowSet Interface 121828.10 Java DB/Apache Derby 122028.11 PreparedStatements 122228.12 Stored Procedures 123728.13 Transaction Processing 123728.14 Wrap-Up 123828.15 Web Resources 1238

29 JavaServer™ Faces Web Applications 124729.1 Introduction 124829.2 Simple HTTP Transactions 124929.3 Multitier Application Architecture 125229.4 Java Web Technologies 1253

29.4.1 Servlets 125329.4.2 JavaServer Pages 125429.4.3 JavaServer Faces 125529.4.4 Web Technologies in NetBeans 1256

29.5 Creating and Running a Simple Application in NetBeans 125629.5.1 Examining a JSP Document 125729.5.2 Examining a Page Bean File 125929.5.3 Event-Processing Life Cycle 126129.5.4 Building a Web Application in NetBeans 1262

29.6 JSF Components 126929.6.1 Text and Graphics Components 126929.6.2 Validation Using Validator Components and Custom Validators 1273

29.7 Session Tracking 128129.7.1 Cookies 128329.7.2 Session Tracking with Session Beans 1294

29.8 Wrap-Up 1303

30 Ajax-Enabled JavaServer™ Faces Web Applications 1313

30.1 Introduction 131430.2 Accessing Databases in Web Applications 1314

30.2.1 Building a Web Application That Displays Data from a Database 131530.2.2 Modifying the Page Bean File for the AddressBook Application 1324

30.3 Ajax-Enabled JSF Components 132730.4 Creating an Autocomplete Text Field and Using Virtual Forms 1329

30.4.1 Configuring Virtual Forms 132930.4.2 JSP File with Virtual Forms and an Autocomplete Text Field 133130.4.3 Providing Suggestions for an Autocomplete Text Field 133530.4.4 Displaying the Contact’s Information 1337

30.5 Wrap-Up 1339

jhtp8TOC.fm Page xx Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Contents xxi

31 Web Services 134331.1 Introduction 134431.2 Web Service Basics 134631.3 Simple Object Access Protocol (SOAP) 134631.4 Representational State Transfer (REST) 134731.5 JavaScript Object Notation (JSON) 134731.6 Publishing and Consuming SOAP-Based Web Services 1347

31.6.1 Creating a Web Application Project and Adding a Web Service Class in NetBeans 1348

31.6.2 Defining the WelcomeSOAP Web Service in NetBeans 134831.6.3 Publishing the WelcomeSOAP Web Service from NetBeans 135031.6.4 Testing the WelcomeSOAP Web Service with GlassFish

Application Server’s Tester Web Page 135131.6.5 Describing a Web Service with the Web Service Description

Language (WSDL) 135331.6.6 Creating a Client to Consume the WelcomeSOAP Web Service 135331.6.7 Consuming the WelcomeSOAP Web Service 1356

31.7 Publishing and Consuming REST-Based XML Web Services 135831.7.1 Creating a REST-Based XML Web Service 135831.7.2 Consuming a REST-Based XML Web Service 1362

31.8 Publishing and Consuming REST-Based JSON Web Services 136331.8.1 Creating a REST-Based JSON Web Service 136431.8.2 Consuming a REST-Based JSON Web Service 1365

31.9 Session Tracking in a SOAP-Based Web Service 136731.9.1 Creating a Blackjack Web Service 136831.9.2 Consuming the Blackjack Web Service 1372

31.10 Consuming a Database-Driven SOAP-Based Web Service 138331.10.1 Creating the Reservation Database 138331.10.2 Creating a Web Application to Interact with the Reservation

Service 138631.11 Equation Generator: Returning User-Defined Types 1391

31.11.1 Creating the REST-Based XML EquationGenerator Web Service 1394

31.11.2 Consuming the REST-Based XML EquationGeneratorWeb Service 1395

31.11.3 Creating the REST-Based JSON EquationGenerator Web Service 1398

31.11.4 Consuming the REST-Based JSON EquationGenerator Web Service 1399

31.12 Wrap-Up 1402

A Operator Precedence Chart 1411

B ASCII Character Set 1413

jhtp8TOC.fm Page xxi Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

xxii Contents

C Keywords and Reserved Words 1414

D Primitive Types 1415

E Using the Java API Documentation 1416E.1 Introduction 1416E.2 Navigating the Java API 1417

F Using the Debugger 1425F.1 Introduction 1426F.2 Breakpoints and the run, stop, cont and print Commands 1426F.3 The print and set Commands 1430F.4 Controlling Execution Using the step, step up and next Commands 1432F.5 The watch Command 1435F.6 The clear Command 1438F.7 Wrap-Up 1440

G Formatted Output 1442G.1 Introduction 1443G.2 Streams 1443G.3 Formatting Output with printf 1443G.4 Printing Integers 1444G.5 Printing Floating-Point Numbers 1445G.6 Printing Strings and Characters 1447G.7 Printing Dates and Times 1448G.8 Other Conversion Characters 1450G.9 Printing with Field Widths and Precisions 1452G.10 Using Flags in the printf Format String 1454G.11 Printing with Argument Indices 1457G.12 Printing Literals and Escape Sequences 1458G.13 Formatting Output with Class Formatter 1458G.14 Wrap-Up 1460

Appendices on the Web 1465Appendices H–Q are PDF documents posted online at www.deitel.com/books/jhtp8/.

H Number Systems IH.1 Introduction IIH.2 Abbreviating Binary Numbers as Octal and Hexadecimal Numbers VH.3 Converting Octal and Hexadecimal Numbers to Binary Numbers VIH.4 Converting from Binary, Octal or Hexadecimal to Decimal VI

jhtp8TOC.fm Page xxii Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

Contents xxiii

H.5 Converting from Decimal to Binary, Octal or Hexadecimal VIIH.6 Negative Binary Numbers: Two’s Complement Notation IX

I GroupLayout XIVI.1 Introduction XIVI.2 GroupLayout Basics XIVI.3 Building a ColorChooser XVI.4 GroupLayout Web Resources XXV

J Java Desktop Integration Components (JDIC) XXVIJ.1 Introduction XXVIJ.2 Splash Screens XXVIJ.3 Desktop Class XXVIIIJ.4 Tray Icons XXXJ.5 JDIC Incubator Projects XXXIJ.6 JDIC Demos XXXI

K Mashups XXXIIK.1 Introduction XXXIIK.2 Popular Mashups XXXIIK.3 APIs Commonly Used in Mashups XXXIIIK.4 Deitel Mashups Research Center XXXIVK.5 Deitel RSS Resource Center XXXVK.6 Mashup Performance and Reliability Issues XXXV

L Unicode® XXXVIL.1 Introduction XXXVIL.2 Unicode Transformation Formats XXXVIIL.3 Characters and Glyphs XXXVIIIL.4 Advantages/Disadvantages of Unicode XXXIXL.5 Using Unicode XXXIXL.6 Character Ranges XLI

M Creating Documentation with javadoc XLIIIM.1 Introduction XLIIIM.2 Documentation Comments XLIIIM.3 Documenting Java Source Code XLIVM.4 javadoc LIM.5 Files Produced by javadoc LII

jhtp8TOC.fm Page xxiii Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.

xxiv Contents

N Bit Manipulation LVN.1 Introduction LVN.2 Bit Manipulation and the Bitwise Operators LVN.3 BitSet Class LXV

O Labeled break and continue Statements LXIXO.1 Introduction LXIXO.2 Labeled break Statement LXIXO.3 Labeled continue Statement LXX

P UML 2: Additional Diagram Types LXXIIP.1 Introduction LXXIIP.2 Additional Diagram Types LXXII

Q Design Patterns LXXIVQ.1 Introduction LXXIVQ.2 Creational, Structural and Behavioral Design Patterns LXXV

Q.2.1 Creational Design Patterns LXXVIQ.2.2 Structural Design Patterns LXXVIIIQ.2.3 Behavioral Design Patterns LXXIXQ.2.4 Conclusion LXXX

Q.3 Design Patterns in Packages java.awt and javax.swing LXXXQ.3.1 Creational Design Patterns LXXXQ.3.2 Structural Design Patterns LXXXIQ.3.3 Behavioral Design Patterns LXXXIIIQ.3.4 Conclusion LXXXVI

Q.4 Concurrency Design Patterns LXXXVIQ.5 Design Patterns Used in Packages java.io and java.net LXXXVIII

Q.5.1 Creational Design Patterns LXXXVIIIQ.5.2 Structural Design Patterns LXXXVIIIQ.5.3 Architectural Patterns LXXXIXQ.5.4 Conclusion XCI

Q.6 Design Patterns Used in Package java.util XCIIQ.6.1 Creational Design Patterns XCIIQ.6.2 Behavioral Design Patterns XCII

Q.7 Wrap-Up XCIII

Index 1466

jhtp8TOC.fm Page xxiv Sunday, March 1, 2009 6:40 PM

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.