Tải bản đầy đủ (.pdf) (7 trang)

a0177 java how to program mor split 1 2952

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (269.62 KB, 7 trang )

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
Contents
• Index


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
Contents
ã Index
ã



Print ISBN-13: 978-0-13-148398-9
eText ISBN-13: 978-0-13-128933-8
Pages: 1568

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

iv
ii
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
Software and Other Resources on the CD That Accompanies Java How to
Program, Sixth Edition
Hardware and Software Requirements to Run JDK 5.0

lvii
lvii
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
Requirements Document
Section 2.10. Wrap-Up

60
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
Class
Section 3.4. Declaring a Method with a Parameter

84
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
Attributes
Section 4.16. Wrap-Up


162
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
Ovals
Section 5.11. (Optional) Software Engineering Case Study: Identifying Objects'
States and Activities
Section 5.12. Wrap-Up

213
216
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
Operations
Section 6.15. Wrap-Up


263
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
Objects
Section 7.15. Wrap-Up

328
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


37
38
39
40
41
42
43
44
45
46
47
48
49
50

51
52
53
54
55

S ho wCo lor s2J Frame.th is, "Choos e a c olor", color );
// s et de fau lt color, i f n o color is r eturne d
if ( c olo r = = n ull )
c ol or = C olo r.LIGHT_ GRA Y;
// c ha nge co nte nt pane' s b ackgrou nd co lor
colo rJ Pan el. set Backgrou nd( color );
} / / en d met hod ac tionPerf orm ed
} // e nd a no nym ous in ner clas s
); // e nd cal l to add Act ionListe ner
ad d ( co lo rJPa ne l, Bor der Layout.C ENT ER ); / / add color JPanel
ad d ( ch an geCo lo rJB utt on, BorderL ayo ut.SOUT H ); // add butto n
se t Size ( 400, 1 30 ); // set fram e s ize
se t Visi bl e( t ru e ) ; / / d isplay f ram e
} // e nd S ho wCol or 2JF ram e c onstruct or
} // end clas s Show Co lor s2J Fra me

Figure 12.8. Choosing colors with J C o l o r C h o o s e r .
(This item is displayed on pages 604 - 605 in the print version)

1
2
3
4
5

6
7
8
9
10
11
12
13

// F ig. 1 2.8: S howC ol ors 2.j ava
// C hoos i ng c ol ors wi th JCo lor Chooser.
im po rt j a vax. sw ing. JF ram e;
pu bl ic c l ass Sh owCo lo rs2
{
/ / ex e cute a ppli ca tio n
p ubli c sta ti c vo id ma in( St ring arg s[] )
{
Sh o wCol or s2JF ra me app lic ation = new ShowCo lors2 JFrame ();
ap p lica ti on.s et Def aul tCl oseOpera tio n( JFra me.EX IT_ON_ CLOSE );
} // e nd m ai n
} // end clas s Show Co lor s2

[View full size image]


[Page 604]
Class JColorChooser provides a static convenience method s h o w D i a l o g that creates a
JColorChooser object, attaches it to a dialog box and displays the dialog. Lines 3637 of Fig. 12.7 invoke
this method to display the color chooser dialog. Method showDialog returns the selected Color object,
or null if the user presses Cancel or closes the dialog without pressing OK. The method takes three

argumentsa reference to its parent Component , a String to display in the title bar of the dialog and the
initial selected Color for the dialog. The parent component is a reference to the window from which the
dialog is displayed (in this case the JFrame , with the reference name frame ). The dialog will be centered
on the parent. If the parent is null , the dialog is centered on the screen. While the color chooser dialog
is on the screen, the user cannot interact with the parent component. This type of dialog is called a
m o d a l d i a l o g (discussed in Chapter 22, GUI Components: Part 2).

[Page 605]
After the user selects a color, lines 4041 determine whether color is null , and, if so, set color to
Color.LIGHT_GRAY . Line 44 invokes method setBackground to change the background color of the
JPanel . Method setBackground is one of the many Component methods that can be used on most GUI
components. Note that the user can continue to use the Change Color button to change the background
color of the application. Figure 12.8 contains method main , which executes the program.
The second screen capture of Fig. 12.8 demonstrates the default JColorChooser dialog that allows the
user to select a color from a variety of color swatches. Note that there are actually three tabs across
the top of the dialogSwatches, HSB and RGB. These represent three different ways to select a color. The
HSB tab allows you to select a color based on hue, saturation and brightnessvalues that are used to
define the amount of light in a color. We do not discuss HSB values. For more information on hue,
saturation and brightness, visit whatis.techtarget.com/definition/0,,sid9_gci212262,00.html. The RGB tab
allows you to select a color by using sliders to select the red, green and blue components. The HSB and
RGB tabs are shown in Fig. 12.9.

[Page 606]



×