Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

AhmadLang / Java, How To Program, 2004

.pdf
Скачиваний:
588
Добавлен:
31.05.2015
Размер:
51.82 Mб
Скачать

Exercises

1001

Special Section: Challenging Multimedia Projects

1002

Chapter 22. GUI Components: Part 2

1005

Section 22.1. Introduction

1006

Section 22.2. JSlider

1006

Section 22.3. Windows: Additional Notes

1010

Section 22.4. Using Menus with Frames

1011

Section 22.5. JPopupMenu

1019

Section 22.6. Pluggable Look-and-Feel

1022

Section 22.7. JDesktopPane and JInternalFrame

1026

Section 22.8. JTabbedPane

1030

Section 22.9. Layout Managers: BoxLayout and GridBagLayout

1032

Section 22.10. Wrap-Up

1046

Summary

1046

Terminology

1048

Self-Review Exercises

1049

Answers to Self-Review Exercises

1050

Exercises

1050

Chapter 23. Multithreading

1052

Section 23.1. Introduction

1053

Section 23.2. Thread States: Life Cycle of a Thread

1055

Section 23.3. Thread Priorities and Thread Scheduling

1056

Section 23.4. Creating and Executing Threads

1058

Section 23.5. Thread Synchronization

1062

Section 23.6. Producer/Consumer Relationship without Synchronization

1064

Section 23.7. Producer/Consumer Relationship with Synchronization

1071

Section 23.8. Producer/Consumer Relationship: Circular Buffer

1076

Section 23.9. Producer/Consumer Relationship: ArrayBlockingQueue

1084

Section 23.10. Multithreading with GUI

1087

Section 23.11. Other Classes and Interfaces in java.util.concurrent

1093

Section 23.12. Monitors and Monitor Locks

1093

Section 23.13. Wrap-Up

1100

Summary

1100

Terminology

1102

Self-Review Exercises

1103

Answers to Self-Review Exercises

1104

Exercises

1104

Chapter 24. Networking

1106

Section 24.1. Introduction

1107

Section 24.2. Manipulating URLs

1108

Section 24.3. Reading a File on a Web Server

1114

Section 24.4. Establishing a Simple Server Using Stream Sockets

1117

Section 24.5. Establishing a Simple Client Using Stream Sockets

1119

Section 24.6. Client/Server Interaction with Stream Socket Connections

1119

Section 24.7. Connectionless Client/Server Interaction with Datagrams

1132

Section 24.8. Client/Server Tic-Tac-Toe Using a Multithreaded Server

1139

Section 24.9. Security and the Network

1154

Section 24.10. Case Study: DeitelMessenger Server and Client

1155

Section 24.11. Wrap-Up

1181

Summary

1181

Terminology

1183

Self-Review Exercises

1184

Answers to Self-Review Exercises

1185

Exercises

1185

Chapter 25. Accessing Databases with JDBC

1189

Section 25.1. Introduction

1190

Section 25.2. Relational Databases

1191

Section 25.3. Relational Database Overview: The books Database

1192

Section 25.4. SQL

1196

Section 25.5. Instructions to install MySQL and MySQL Connector/J

1206

Section 25.6. Instructions on Setting MySQL User Account

1206

Section 25.7. Creating Database books in MySQL

1207

Section 25.8. Manipulating Databases with JDBC

1207

Section 25.9. Stored Procedures

1224

Section 25.10. RowSet Interface

1224

Section 25.11. Wrap-Up

1227

Section 25.12. Internet and Web Resources

1227

Recommended Readings

1228

Summary

1229

Terminology

1232

Self-Review Exercises

1234

Answers to Self-Review Exercises

1234

Exercises

1234

Chapter 26. Servlets

1236

Section 26.1. Introduction

1237

Section 26.2. Servlet Overview and Architecture

1239

Section 26.3. Setting Up the Apache Tomcat Server

1243

Section 26.4. Handling HTTP get Requests

1246

Section 26.5. Handling HTTP get Requests Containing Data

1255

Section 26.6. Handling HTTP post Requests

1258

Section 26.7. Redirecting Requests to Other Resources

1261

Section 26.8. Multitier Applications: Using JDBC from a Servlet

1265

Section 26.9. Welcome Files

1270

Section 26.10. Wrap-Up

1274

Section 26.11. Internet and Web Resources

1274

Summary

1275

Terminology

1277

Self-Review Exercises

1277

Answers to Self-Review Exercises

1278

Exercises

1278

Chapter 27. JavaServer Pages (JSP)

1280

Section 27.1. Introduction

1281

Section 27.2. JavaServer Pages Overview

1282

Section 27.3. First JSP Example

1283

Section 27.4. Implicit Objects

1285

Section 27.5. Scripting

1286

Section 27.6. Standard Actions

1291

Section 27.7. Directives

1305

Section 27.8. Case Study: Guest Book

1309

Section 27.9. Wrap-Up

1321

Section 27.10. Internet and Web Resources

1321

Summary

1321

Terminology

1323

Self-Review Exercises

1324

Answers to Self-Review Exercises

1325

Exercises

1325

Chapter 28. Formatted Output

1326

Section 28.1. Introduction

1327

Section 28.2. Streams

1327

Section 28.3. Formatting Output with printf

1328

Section 28.4. Printing Integers

1328

Section 28.5. Printing Floating-Point Numbers

1329

Section 28.6. Printing Strings and Characters

1330

Section 28.7. Printing Dates and Times

1331

Section 28.8. Other Conversion Characters

1335

Section 28.9. Printing with Field Widths and Precisions

1335

Section 28.10. Using Flags in the printf Format String

1337

Section 28.11. Printing with Argument Indices

1342

Section 28.12. Printing Literals and Escape Sequences

1342

Section 28.13. Formatting Output with Class Formatter

1342

Section 28.14. Wrap-Up

1344

Summary

1344

Terminology

1346

Self-Review Exercises

1346

Answers to Self-Review Exercises

1347

Exercises

1348

Chapter 29. Strings, Characters and Regular Expressions

1350

Section 29.1. Introduction

1351

Section 29.2. Fundamentals of Characters and Strings

1352

Section 29.3. Class String

1352

Section 29.4. Class StringBuffer

1364

Section 29.5. Class Character

1372

Section 29.6. Class StringTokenizer

1376

Section 29.7. Regular Expressions, Class Pattern and Class Matcher

1378

Section 29.8. Wrap-Up

1387

Summary

1387

Terminology

1391

Self-Review Exercises

1391

Answers to Self-Review Exercises

1392

Exercises

1392

Special Section: Advanced String-Manipulation Exercises

1393

Special Section: Challenging String-Manipulation Projects

1396

Appendix A. Operator Precedence Chart

1398

Section A.1. Operator Precedence

1398

Appendix B. ASCII Character Set

1400

Appendix C. Keywords and Reserved Words

1401

Appendix D. Primitive Types

1402

Appendix E. (On CD) Number Systems Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation Section G.1. Introduction

Section G.2. Navigating the Java API

Appendix H. (On CD) Creating Documentation with javadoc Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements Appendix L. (On CD) UML 2: Additional Diagram Types Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger End User License Agreements

Prentice Hall License Agreement and Limited Warranty Apache Tomcat License Agreement

JCreator™ License Agreement jEdit License Agreement jGRASP™ License Agreement NetBeans™ License Agreement MySQL® Open Source License

Inside Back Cover

License Agreement and Limited Warranty Using the CD-ROM

Contents of the CD-ROM

Software and Hardware System Requirements Index

1403

1416

1427

1427

1428

1437

1449

1464

1489

1492

1494

1515 EULA-1 EULA-1 EULA-2 EULA-4 EULA-7 EULA-11 EULA-12 EULA-17

InsideBackCover InsideBackCover InsideBackCover InsideBackCover InsideBackCover

[Page iv]

Copyright

Library of Congress Cataloging-in-Publication Data

On file

Vice President and Editorial Director, ECS: Marcia J. Horton

Senior Acquisitions Editor: Kate Hargett

Associate Editor: Jennifer Cappello

Assistant Editor: Sarah Parker

Editorial Assistant: Michael Giacobbe

Vice President and Director of Production and Manufacturing, ESM: David W. Riccardi

Executive Managing Editor: Vince O'Brien

Managing Editor: Tom Manshreck

Production Editor: John F. Lovell

Production Editor, Media: Bob Engelhardt

Production Assistant: Asha Rohra

Director of Creative Services: Paul Belfanti

A/V Production Editor: Xiaohong Zhu

Art Studio: Artworks, York, PA

Art Director: Geoffrey Cassar

Cover Design: Harvey M. Deitel, Shawn Murphy, Geoffrey Cassar

Interior Design: Harvey M. Deitel, Geoffrey Cassar

Manufacturing Manager: Trudy Pisciotti

Manufacturing Buyer: Lisa McDowell

Marketing Manager: Pamela Hersperger

Marketing Assistant: Barrie Reinhold

© 2005 by Pearson Education, Inc.

Upper Saddle River, New Jersey 07458

The authors and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of the theories and programs to determine their

effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in this book. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks and registered trademarks. Where those designations appear in this book, and Prentice Hall and the authors were aware of a trademark claim, the designations have been printed in initial caps or all caps. All product names mentioned remain trademarks or registered trademarks of their respective owners.

All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher.

Printed in the United States of America

10 9 8 7 6 5 4 3 2 1

Pearson Education Ltd., London

Pearson Education Australia Pty. Ltd., Sydney

Pearson Education Singapore, Pte. Ltd.

Pearson Education North Asia Ltd., Hong Kong

Pearson Education Canada, Inc., Toronto

Pearson Educacion de Mexico, S.A. de C.V.

Pearson EducationJapan, Tokyo

Pearson Education Malaysia, Pte. Ltd.

Pearson Education, Inc., Upper Saddle River, New Jersey

[Page vi]

Trademarks

Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Pearson Education is independent of Sun Microsystems, Inc.

Microsoft, Microsoft® Internet Explorer and the Windows logo are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.

Netscape browser window © 2004 Netscape Communications Corporation. Used with permission. Netscape Communications has not authorized, sponsored, endorsed, or approved this publication and is not responsible for its content.

MySQL is a registered trademark of MySQL AB in the United States, the European Union and other countries.

Copyright® 19992002 The Apache Software Foundation. All Rights Reserved.

[Page vii]

Dedication

In memory of Edsger W. Dijkstra:

We continue to learn from your writings every day.

Harvey M. Deitel and Paul J. Deitel

[Page ii]

Deitel® Books, Cyber Classrooms, Complete Training Courses and Web-Based Training Courses published by Prentice Hall

How To Program Series

Advanced Java™ 2 Platform How to Program

C How to Program, 4/E

C++ How to Program, 4/E

C# How to Program

e-Business and e-Commerce How to Program Internet and World Wide Web How to Program, 3/E Java How to Program, 6/E

Small Java™ How to Program, 6/E

Perl How to Program

Python How to Program

Visual C++® .NET How to Program

Visual Basic® 6 How to Program

Visual Basic® .NET How to Program, 2/E

Wireless Internet & Mobile Business How to Program

XML How to Program

Simply Series

Simply C++: An Application-Driven Tutorial Approach

Simply C#: An Application-Driven Tutorial Approach

Simply Java™ Programming: An Application-Driven Tutorial Approach

Simply Visual Basic® .NET: An Application Driven Tutorial Approach (Visual Studio .NET 2002 Edition)

Simply Visual Basic® .NET: An Application Driven Tutorial Approach (Visual Studio .NET 2003 Edition)

.NET How to Program Series

C# How to Program

Visual Basic® .NET How to Program, 2/E

Visual C++® .NET How to Program

For Managers Series

e-Business and e-Commerce for Managers

Visual Studio® Series

C# How to Program

Visual Basic® .NET How to Program, 2/E

Getting Started with Microsoft® Visual C++® 6 with an Introduction to MFC

Visual Basic® 6 How to Program

Also Available

e-books

Premium CourseCompass, WebCT and Blackboard Multimedia Cyber Classroom versions Pearson Choices: SafariX

[Page iii]

Multimedia Cyber Classroom and Web-Based Training Series

C++ Multimedia Cyber Classroom, 4/E

C# Multimedia Cyber Classroom

e-Business and e-Commerce Multimedia Cyber Classroom Internet and World Wide Web Multimedia Cyber Classroom, 2/E Java™ 2 Multimedia Cyber Classroom, 5/E

Perl Multimedia Cyber Classroom

Python Multimedia Cyber Classroom

Visual Basic® 6 Multimedia Cyber Classroom

Visual Basic® .NET Multimedia Cyber Classroom, 2/E

Wireless Internet & Mobile Business Programming Multimedia Cyber Classroom XML Multimedia Cyber Classroom

The Complete Training Course Series

The Complete C++ Training Course, 4/E

The Complete C# Training Course

The Complete e-Business and e-Commerce Programming Training Course

The Complete Internet and World Wide Web Programming Training Course, 2/E

The Complete Java™ 2 Training Course, 5/E

The Complete Perl Training Course

The Complete Python Training Course

The Complete Visual Basic® 6 Training Course

The Complete Visual Basic® .NET Training Course, 2/E

The Complete Wireless Internet & Mobile Business Programming Training Course

The Complete XML Programming Training Course

To follow the Deitel publishing program, please register at: www.deitel.com/newsletter/subscribe.html

for the free DEITEL® BUZZ ONLINE e-mail newsletter.

To communicate with the authors, send e-mail to: deitel@deitel.com

For information on corporate on-site seminars offered by Deitel & Associates, Inc. worldwide, visit: www.deitel.com or write to deitel@deitel.com

For continuing updates on Prentice Hall/Deitel publications visit: www.deitel.com,

www.prenhall.com/deitel or www.InformIT.com/deitel

[Page xxv]

Preface

"Live in fragments no longer, only connect."

Edgar Morgan Foster

Welcome to Java and Java How to Program, Sixth Edition! At Deitel & Associates, we write computer science textbooks and professional books. This book was a joy to create. To start, we put the fifth edition of Java How to Program under the microscope:

We audited the presentation against the most recent ACM/IEEE curriculum recommendations and the Computer Science Advanced Placement Examination.

All of the chapters have been significantly updated and upgraded.

We changed to an early classes and objects pedagogy. Now students build their first reusable classes starting in Chapter 3.

All of the GUI and graphics in the early chapters has been replaced by carefully paced optional

sections in Chapters 310 with two special exercise sections in Chapters 11 and 12. Instructors have a broad choice of the amount of GUI and graphics to coverfrom none, to a 10-section introductory sequence, to a deep treatment in Chapters 11, 12 and 22.

We updated our object-oriented presentation to use the latest version of the UML (Unified

Modeling Language)UML™ 2.0the industry-standard graphical language for modeling objectoriented systems.

We replaced the optional elevator simulator case study from the previous edition with a new

optional OOD/UML automated teller machine (ATM) case study in Chapters 18 and 10. The new case study is much simpler and more attractive for first and second programming courses.

Several multi-section object-oriented programming case studies have been added.

We incorporated key new features of Sun Microsystems' latest release of Javathe Java 2 Platform, Standard Edition version 5.0 (J2SE 5.0).

The design of the book has been completely revised. This new design uses color, fonts and various design elements to enhance a student's learning experience.

All of this has been carefully scrutinized by a team of 37 distinguished academic and industry reviewers.

We believe that this book and its support materials have everything instructors and students need for an informative, interesting, challenging and entertaining Java educational experience. In this Preface, we overview various conventions used in the book, such as syntax coloring the code examples, "code washing" and code highlighting. We discuss the software bundled with the book as well as the comprehensive suite of educational materials that help instructors maximize their students' learning

experience, including the Instructor's Resource CD, PowerPoint® Slide lecture notes, lab manual, companion Web site, course management systems, SafariX (Pearson Education's WebBook publications) and more.

[Page xxvi]

As you read this book, if you have questions, send an e-mail to deitel@deitel.com; we will respond promptly. Please visit our Web site, www.deitel.com, regularly and be sure to sign up for the free

Deitel® Buzz Online e-mail newsletter at www.deitel.com/newsletter/subscribe.html. We use the Web site and the newsletter to keep our readers and industry clients informed of the latest news on Deitel publications and services. Please check the Web site occasionally for errata, updates regarding the Java software, free downloads and other resources.