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

AhmadLang / Java, How To Program, 2004

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

Table of Contents

Index

Java™ How to Program, Sixth Edition

By H. M. Deitel - Deitel & Associates, Inc., P. J. Deitel - Deitel & Associates, Inc.

Publisher: Prentice Hall

Pub Date: August 04, 2004

Print ISBN-10: 0-13-148398-6

eText ISBN-10: 0-13-128933-0

Print ISBN-13: 978-0-13-148398-9

eText ISBN-13: 978-0-13-128933-8

Pages: 1568

Extensively revised for the latest Java (J2SE 5.0) release; Deitel Java How to Program, 6/e now includes earlier coverage of objects; new and streamlined case studies; and OPTIONAL GUI and graphics sections. Now available in a briefer version (ch. 1-10) called Small Java. SafariX version available.

 

 

Java™ How to Program, Sixth Edition

 

 

By H. M. Deitel -

Deitel & Associates, Inc., P. J. Deitel - Deitel & Associates, Inc.

 

 

Publisher:

Prentice Hall

 

 

Pub Date:

August 04, 2004

 

 

Print ISBN-10:

0-13-148398-6

 

 

eText ISBN-10

:

0-13-128933-0

Table of

Print ISBN-13

:

978-0-13-148398-9

Contents

eText ISBN-13

:

978-0-13-128933-8

Index

Pages:

1568

 

 

Copyright

iv

Deitel® Books, Cyber Classrooms, Complete Training Courses and Web-Based

ii

Training Courses published by Prentice Hall

 

Preface

xxv

Features in Java How to Program, 6/e

xxvi

Teaching Approach

xxix

Tour of the Book

xxxiii

A Tour of the Optional Case Study on Object-Oriented Design with the UML

xliii

A Tour of the Optional GUI and Graphics Case Study

xlv

Software Included with Java How to Program, 6/e

xlvi

Teaching Resources for Java How to Program, 6/e

xlvii

Java in the Lab

xlvii

OneKey, CourseCompassSM, WebCT™ and by Blackboard™

xlix

Java 2 Multimedia Cyber Classroom, 6/e Through OneKey

l

PearsonChoices

l

Computer Science AP Courses

li

Deitel® Buzz Online Free E-mail Newsletter

li

Acknowledgments

li

About the Authors

lv

About Deitel & Associates, Inc.

lv

Before You Begin

lvii

Software and Other Resources on the CD That Accompanies Java How to

lvii

Program, Sixth Edition

 

Hardware and Software Requirements to Run JDK 5.0

lvii

Copying and Organizing Files

lviii

Copying the Book Examples from the CD

lviii

Changing the Read-Only Property of Files

lviii

Installing the J2SE Development Kit (JDK)

lxii

Setting the PATH Variable

lxiv

Chapter 1. Introduction to Computers, the Internet and the World Wide Web

1

Section 1.1. Introduction

2

Section 1.2. What Is a Computer?

4

Section 1.3. Computer Organization

4

Section 1.4. Early Operating Systems

5

Section 1.5. Personal, Distributed and Client/Server Computing

6

Section 1.6. The Internet and the World Wide Web

6

Section 1.7. Machine Languages, Assembly Languages and High-Level Languages 7

Section 1.8. History of C and C++

8

Section 1.9. History of Java

9

Section 1.10. Java Class Libraries

9

Section 1.11. FORTRAN, COBOL, Pascal and Ada

11

Section 1.12. BASIC, Visual Basic, Visual C++, C# and .NET

11

Section 1.13. Typical Java Development Environment

12

Section 1.14. Notes about Java and Java How to Program, Sixth Edition

15

Section 1.15. Test-Driving a Java Application

16

Section 1.16. Software Engineering Case Study: Introduction to Object Technology

21

and the UML (Required)

 

Section 1.17. Wrap-Up

26

Section 1.18. Web Resources

26

Summary

28

Terminology

30

Self-Review Exercises

32

Answers to Self-Review Exercises

32

Exercises

33

Chapter 2. Introduction to Java Applications

35

Section 2.1. Introduction

36

Section 2.2. First Program in Java: Printing a Line of Text

36

Section 2.3. Modifying Our First Java Program

43

Section 2.4. Displaying Text with printf

45

Section 2.5. Another Java Application: Adding Integers

47

Section 2.6. Memory Concepts

51

Section 2.7. Arithmetic

52

Section 2.8. Decision Making: Equality and Relational Operators

56

Section 2.9. (Optional) Software Engineering Case Study: Examining the

60

Requirements Document

 

Section 2.10. Wrap-Up

69

Summary

70

Terminology

73

Self-Review Exercises

74

Answers to Self-Review Exercises

75

Exercises

77

Chapter 3. Introduction to Classes and Objects

81

Section 3.1. Introduction

82

Section 3.2. Classes, Objects, Methods and Instance Variables

82

Section 3.3. Declaring a Class with a Method and Instantiating an Object of a

84

Class

 

Section 3.4. Declaring a Method with a Parameter

88

Section 3.5. Instance Variables, set Methods and get Methods

91

Section 3.6. Primitive Types vs. Reference Types

96

Section 3.7. Initializing Objects with Constructors

97

Section 3.8. Floating-Point Numbers and Type double

100

Section 3.9. (Optional) GUI and Graphics Case Study: Using Dialog Boxes

104

Section 3.10. (Optional) Software Engineering Case Study: Identifying the Classes

107

in a Requirements Document

 

Section 3.11. Wrap-Up

114

Summary

115

Terminology

119

Self-Review Exercises

119

Answers to Self-Review Exercises

120

Exercises

121

Chapter 4. Control Statements: Part I

123

Section 4.1. Introduction

124

Section 4.2. Algorithms

124

Section 4.3. Pseudocode

125

Section 4.4. Control Structures

125

Section 4.5. if Single-Selection Statement

128

Section 4.6. if...else Double-Selection Statement

129

Section 4.7. while Repetition Statement

134

Section 4.8. Formulating Algorithms: Counter-Controlled Repetition

135

Section 4.9. Formulating Algorithms: Sentinel-Controlled Repetition

140

Section 4.10. Formulating Algorithms: Nested Control Statements

148

Section 4.11. Compound Assignment Operators

153

Section 4.12. Increment and Decrement Operators

154

Section 4.13. Primitive Types

156

Section 4.14. (Optional) GUI and Graphics Case Study: Creating Simple Drawings

157

Section 4.15. (Optional) Software Engineering Case Study: Identifying Class

162

Attributes

 

Section 4.16. Wrap-Up

166

Summary

167

Terminology

168

Self-Review Exercises

169

Answers to Self-Review Exercises

171

Exercises

172

Chapter 5. Control Statements: Part 2

179

Section 5.1. Introduction

180

Section 5.2. Essentials of Counter-Controlled Repetition

180

Section 5.3. for Repetition Statement

182

Section 5.4. Examples Using the for Statement

186

Section 5.5. do...while Repetition Statement

191

Section 5.6. switch Multiple-Selection Statement

192

Section 5.7. break and continue Statements

200

Section 5.8. Logical Operators

202

Section 5.9. Structured Programming Summary

208

Section 5.10. (Optional) GUI and Graphics Case Study: Drawing Rectangles and

213

Ovals

 

Section 5.11. (Optional) Software Engineering Case Study: Identifying Objects'

216

States and Activities

 

Section 5.12. Wrap-Up

220

Summary

221

Terminology

223

Self-Review Exercises

224

Answers to Self-Review Exercises

225

Exercises

226

Chapter 6. Methods: A Deeper Look

230

Section

6.1. Introduction

231

Section

6.2. Program Modules in Java

232

Section

6.3. static Methods, static Fields and Class Math

233

Section

6.4. Declaring Methods with Multiple Parameters

236

Section

6.5. Notes on Declaring and Using Methods

240

Section

6.6. Method Call Stack and Activation Records

241

Section

6.7. Argument Promotion and Casting

241

Section

6.8. Java API Packages

243

Section

6.9. Case Study: Random-Number Generation

245

Section

6.10. Case Study: A Game of Chance (Introducing Enumerations)

250

Section

6.11. Scope of Declarations

255

Section

6.12. Method Overloading

257

Section

6.13. (Optional) GUI and Graphics Case Study: Colors and Filled Shapes

261

Section

6.14. (Optional) Software Engineering Case Study: Identifying Class

263

Operations

 

Section

6.15. Wrap-Up

271

Summary

271

Terminology

274

Self-Review Exercises

275

Answers to Self-Review Exercises

277

Exercises

279

Chapter 7. Arrays

285

Section

7.1. Introduction

286

Section

7.2. Arrays

286

Section

7.3. Declaring and Creating Arrays

288

Section

7.4. Examples Using Arrays

289

Section

7.5. Case Study: Card Shuffling and Dealing Simulation

298

Section

7.6. Enhanced for Statement

301

Section

7.7. Passing Arrays to Methods

303

Section

7.8. Case Study: Class GradeBook Using an Array to Store Grades

307

Section

7.9. Multidimensional Arrays

311

Section

7.10. Case Study: Class GradeBook Using a Two-Dimensional Array

316

Section

7.11. Variable-Length Argument Lists

322

Section

7.12. Using Command-Line Arguments

323

Section

7.13. (Optional) GUI and Graphics Case Study: Drawing Arcs

325

Section

7.14. (Optional) Software Engineering Case Study: Collaboration Among

328

Objects

 

 

 

Section

7.15. Wrap-Up

336

Summary

337

Terminology

339

Self-Review Exercises

339

Answers to Self-Review Exercises

340

Exercises

341

Special Section: Building Your Own Computer

350

Chapter 8. Classes and Objects: A Deeper Look

357

Section 8.1. Introduction

358

Section 8.2. Time Class Case Study

359

Section 8.3. Controlling Access to Members

362

Section 8.4. Referring to the Current Object's Members with the this Reference

363

Section 8.5. Time Class Case Study: Overloaded Constructors

366

455
455
456
457
457
458
459
460
462
463
466
468
483
484
496
498
406
407
410
410
411
411
415
416
417
419
420
444
450
451
371
372
373
376
379
380
385
386
388
389
390
396
396

Section 8.6. Default and No-Argument Constructors Section 8.7. Notes on Set and Get Methods Section 8.8. Composition

Section 8.9. Enumerations

Section 8.10. Garbage Collection and Method finalize Section 8.11. static Class Members

Section 8.12. static Import

Section 8.13. final Instance Variables Section 8.14. Software Reusability

Section 8.15. Data Abstraction and Encapsulation Section 8.16. Time Class Case Study: Creating Packages Section 8.17. Package Access

Section 8.18. (Optional) GUI and Graphics Case Study: Using Objects with Graphics

Section 8.19. (Optional) Software Engineering Case Study: Starting to Program the 401 Classes of the ATM System

Section 8.20. Wrap-Up Summary

Terminology Self-Review Exercises

Answers to Self-Review Exercises Exercises

Chapter 9. Object-Oriented Programming: Inheritance Section 9.1. Introduction

Section 9.2. Superclasses and Subclasses Section 9.3. protected Members

Section 9.4. Relationship between Superclasses and Subclasses Section 9.5. Constructors in Subclasses

Section 9.6. Software Engineering with Inheritance Section 9.7. Object Class

Section 9.8. (Optional) GUI and Graphics Case Study: Displaying Text and Images 453 Using Labels

Section 9.9. Wrap-Up Summary Terminology Self-Review Exercises

Answers to Self-Review Exercises Exercises

Chapter 10. Object-Oriented Programming: Polymorphism Section 10.1. Introduction

Section 10.2. Polymorphism Examples

Section 10.3. Demonstrating Polymorphic Behavior Section 10.4. Abstract Classes and Methods

Section 10.5. Case Study: Payroll System Using Polymorphism Section 10.6. final Methods and Classes

Section 10.7. Case Study: Creating and Using Interfaces

Section 10.8. (Optional) GUI and Graphics Case Study: Drawing with Polymorphism

Section 10.9. (Optional) Software Engineering Case Study: Incorporating Inheritance into the ATM System

Section 10.10. Wrap-Up

506

Summary

506

Terminology

507

Self-Review Exercises

507

Answers to Self-Review Exercises

508

Exercises

508

Chapter 11. GUI Components: Part 1

510

Section 11.1. Introduction

511

Section 11.2. Simple GUI-Based Input/Output with JOptionPane

512

Section 11.3. Overview of Swing Components

515

Section 11.4. Displaying Text and Images in a Window

518

Section 11.5. Text Fields and an Introduction to Event Handling with Nested

522

Classes

 

Section 11.6. Common GUI Event Types and Listener Interfaces

529

Section 11.7. How Event Handling Works

530

Section 11.8. JButton

533

Section 11.9. Buttons that Maintain State

536

Section 11.10. JComboBox and Using an Anonymous Inner Class for Event

543

Handling

 

Section 11.11. JList

546

Section 11.12. Multiple-Selection Lists

549

Section 11.13. Mouse Event Handling

551

Section 11.14. Adapter Classes

556

Section 11.15. JPanel Subclass for Drawing with the Mouse

559

Section 11.16. Key-Event Handling

563

Section 11.17. Layout Managers

567

Section 11.18. Using Panels to Manage More Complex Layouts

576

Section 11.19. JTextArea

578

Section 11.20. Wrap-Up

581

Summary

581

Terminology

587

Self-Review Exercises

589

Answers to Self-Review Exercises

590

Exercises

590

Chapter 12. Graphics and Java 2D™

595

Section 12.1. Introduction

596

Section 12.2. Graphics Contexts and Graphics Objects

598

Section 12.3. Color Control

599

Section 12.4. Font Control

606

Section 12.5. Drawing Lines, Rectangles and Ovals

611

Section 12.6. Drawing Arcs

616

Section 12.7. Drawing Polygons and Polylines

618

Section 12.8. Java 2D API

622

Section 12.9. Wrap-Up

629

Summary

629

Terminology

631

Self-Review Exercises

632

Answers to Self-Review Exercises

633

Exercises

633

Chapter 13. Exception Handling

638

Section 13.1. Introduction

639

Section 13.2. Exception-Handling Overview

640

Section 13.3. Example: Divide By Zero Without Exception Handling

641

Section 13.4. Example: Handling ArithmeticExceptions and

643

InputMismatchExceptions

 

Section 13.5. When to Use Exception Handling

648

Section 13.6. Java Exception Hierarchy

649

Section 13.7. finally block

652

Section 13.8. Stack Unwinding

656

Section 13.9. printStackTrace, getStackTrace and getMessage

658

Section 13.10. Chained Exceptions

660

Section 13.11. Declaring New Exception Types

662

Section 13.12. Preconditions and Postconditions

663

Section 13.13. Assertions

664

Section 13.14. Wrap-Up

666

Summary

666

Terminology

668

Self-Review Exercises

669

Answers to Self-Review Exercises

670

Exercises

670

Chapter 14. Files and Streams

672

Section 14.1. Introduction

673

Section 14.2. Data Hierarchy

674

Section 14.3. Files and Streams

675

Section 14.4. Class File

678

Section 14.5. Sequential-Access Text Files

682

Section 14.6. Object Serialization

697

Section 14.7. Random-Access Files

706

Section 14.8. Additional java.io Classes

727

Section 14.9. Opening Files with JFileChooser

729

Section 14.10. Wrap-Up

733

Summary

733

Terminology

735

Self-Review Exercises

737

Answers to Self-Review Exercises

738

Exercises

739

Chapter 15. Recursion

744

Section 15.1. Introduction

745

Section 15.2. Recursion Concepts

745

Section 15.3. Example Using Recursion: Factorials

747

Section 15.4. Example Using Recursion: Fibonacci Series

750

Section 15.5. Recursion and the Method Call Stack

753

Section 15.6. Recursion vs. Iteration

754

Section 15.7. String Permutations

756

Section 15.8. Towers of Hanoi

761

Section 15.9. Fractals

763

Section 15.10. Recursive Backtracking

774

Section 15.11. Wrap-Up

775

Section 15.12. Internet and Web Resources

775

Summary

776

Terminology

778

Self-Review Exercises

779

Answers to Self-Review Exercises

780

Exercises

780

Chapter 16. Searching and Sorting

785

Section 16.1. Introduction

786

Section 16.2. Searching Algorithms

786

Section 16.3. Sorting Algorithms

796

Section 16.4. Invariants

810

Section 16.5. Wrap-up

812

Summary

812

Terminology

814

Self-Review Exercises

814

Answers to Self-Review Exercises

814

Exercises

814

Chapter 17. Data Structures

817

Section 17.1. Introduction

818

Section 17.2. Type-Wrapper Classes for Primitive Types

819

Section 17.3. Autoboxing and Auto-Unboxing

819

Section 17.4. Self-Referential Classes

820

Section 17.5. Dynamic Memory Allocation

821

Section 17.6. Linked Lists

821

Section 17.7. Stacks

832

Section 17.8. Queues

836

Section 17.9. Trees

838

Section 17.10. Wrap-Up

845

Summary

846

Terminology

847

Self-Review Exercises

848

Answers to Self-Review Exercises

849

Exercises

850

Special Section: Building Your Own Compiler

855

Chapter 18. Generics

869

Section 18.1. Introduction

870

Section 18.2. Motivation for Generic Methods

871

Section 18.3. Generic Methods: Implementation and Compile-Time Translation

873

Section 18.4. Additional Compile-Time Translation Issues: Methods That Use a

876

Type Parameter as the Return Type

 

Section 18.5. Overloading Generic Methods

879

Section 18.6. Generic Classes

879

Section 18.7. Raw Types

890

Section 18.8. Wildcards in Methods That Accept Type Parameters

891

Section 18.9. Generics and Inheritance: Notes

898

Section 18.10. Wrap-Up

898

Section 18.11. Internet and Web Resources

899

Summary

899

Terminology

901

Self-Review Exercises

901

Answers to Self-Review Exercises

902

Exercises

902

Chapter 19. Collections

904

Section 19.1. Introduction

905

Section 19.2. Collections Overview

906

Section 19.3. Class Arrays

907

Section 19.4. Interface Collection and Class Collections

910

Section 19.5. Lists

911

Section 19.6. Collections Algorithms

922

Section 19.7. Stack Class of Package java.util

935

Section 19.8. Class PriorityQueue and Interface Queue

938

Section 19.9. Sets

939

Section 19.10. Maps

942

Section 19.11. Properties Class

946

Section 19.12. Synchronized Collections

949

Section 19.13. Unmodifiable Collections

950

Section 19.14. Abstract Implementations

950

Section 19.15. Wrap-Up

951

Summary

951

Terminology

954

Self-Review Exercises

955

Answers to Self-Review Exercises

955

Exercises

956

Chapter 20. Introduction to Java Applets

958

Section 20.1. Introduction

959

Section 20.2. Sample Applets Provided with the JDK

959

Section 20.3. Simple Java Applet: Drawing a String

964

Section 20.4. Applet Life-Cycle Methods

968

Section 20.5. Initializing an Instance Variable with Method init

970

Section 20.6. Sandbox Security Model

972

Section 20.7. Internet and Web Resources

972

Section 20.8. Wrap-Up

973

Summary

973

Terminology

975

Self-Review Exercises

975

Answers to Self-Review Exercises

975

Exercises

975

Chapter 21. Multimedia: Applets and Applications

977

Section 21.1. Introduction

978

Section 21.2. Loading, Displaying and Scaling Images

979

Section 21.3. Animating a Series of Images

982

Section 21.4. Image Maps

987

Section 21.5. Loading and Playing Audio Clips

987

Section 21.6. Playing Video and Other Media with Java Media Framework

993

Section 21.7. Wrap-Up

997

Section 21.8. Internet and Web Resources

998

Summary

999

Terminology

1000

Self-Review Exercises

1001

Answers to Self-Review Exercises

1001