Java by Dissection (2nd ed.) [Pohl & McDowell 2006-08-02].pdf

(6695 KB) Pobierz
00JavaEd2.book
Java by Dissection
2nd Edition
Ira Pohl
Charlie McDowell
University of California, Santa Cruz
653114401.001.png
ii
Copyright (c) 2006 by Ira Pohl and Charlie McDowell
ISBN 978-1-4116-5238-5
Updated: August 2, 2006
T ABLE OF C ONTENTS
Table of Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
Chapter 1
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
1.1 Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Algorithms—Being Precise . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Implementing Our Algorithm in Java . . . . . . . . . . . . . . . 5
1.4 Why Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Network Computing and the Web . . . . . . . . . . . . . . . . . 7
1.6 Human–Computer Interaction and the GUI . . . . . . . . . . 8
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Chapter 2
Program Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.1 “Hello, world!” in Java . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Compiling and Running Your Java Program . . . . . . . . 14
2.3 Lexical Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.1 White Space . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.2 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . 17
653114401.002.png
iv
Table of Contents
2.3.3 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
2.3.4 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
2.3.5 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
2.3.6 Operators and Punctuation . . . . . . . . . . . . . . .19
2.4 Data Types and Variable Declarations . . . . . . . . . . . . .19
2.4.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
2.4.2 Variable Initialization . . . . . . . . . . . . . . . . . . .20
2.5 An Example: String Concatenation . . . . . . . . . . . . . . . .21
2.5.1 Strings Versus Identifiers Versus Variables . . .22
2.6 User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23
2.7 Calling Predefined Methods . . . . . . . . . . . . . . . . . . . . .24
2.8 print() , println() , and printf() . . . . . . . . . . . . .25
2.8.1 Formatting output with printf() . . . . . . . . .26
2.9 Number Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
2.9.1 The Integer Types . . . . . . . . . . . . . . . . . . . . . .27
2.9.2 The Floating Point Types . . . . . . . . . . . . . . . .28
2.9.3 The char Type . . . . . . . . . . . . . . . . . . . . . . . .28
2.9.4 Numbers Versus Strings . . . . . . . . . . . . . . . . .30
2.10 Arithmetic Expressions . . . . . . . . . . . . . . . . . . . . . . . .30
2.10.1 An Integer Arithmetic Example: Change . . . . .31
2.10.2 Type Conversion . . . . . . . . . . . . . . . . . . . . . . .32
2.11 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . .34
2.12 The Increment and Decrement Operators . . . . . . . . . . .36
2.13 Precedence and Associativity of Operators . . . . . . . . . .37
2.14 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Applet Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Chapter 3
Statements and Control Flow . . . . . . . . . . . . . . . . . . . . . 47
3.1 Expression, block, and empty Statements . . . . . . . . . .47
3.1.1 Empty Statement . . . . . . . . . . . . . . . . . . . . . .48
3.2 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . .49
3.2.1 Relational and Equality Operators . . . . . . . . . .49
3.2.2 Logical operators . . . . . . . . . . . . . . . . . . . . . .49
3.3 The if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51
3.3.1 Problem Solving with the if statement . . . . . .52
3.4 The if-else statement . . . . . . . . . . . . . . . . . . . . . . .55
3.4.1 Nested if-else Statements . . . . . . . . . . . . . .57
3.4.2 if-else - if-else - if . . . . . . . . . . . . . . . . . . .58
3.4.3 The Dangling else Problem . . . . . . . . . . . . . .59
3.5 The while statement . . . . . . . . . . . . . . . . . . . . . . . . . .60
3.5.1 Problem Solving with Loops . . . . . . . . . . . . . .61
3.6 The do Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . .64
3.7 The for statement . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
Table of Contents v
3.7.1 Local Variables in the for Statement . . . . . . .67
3.8 The break and continue statements . . . . . . . . . . . . .68
3.9 The switch statement . . . . . . . . . . . . . . . . . . . . . . . . .69
3.10 Using the laws of Boolean Algebra . . . . . . . . . . . . . . . .71
3.11 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Applet Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Chapter 4
Methods: Functional Abstraction . . . . . . . . . . . . . . . . . . 81
4.1 Method Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
4.2 Static Method Definitions . . . . . . . . . . . . . . . . . . . . . . .83
4.3 The return statement . . . . . . . . . . . . . . . . . . . . . . . . .84
4.4 Scope of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . .86
4.5 Top-Down Design . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
4.6 Problem Solving: Random Numbers . . . . . . . . . . . . . . .91
4.7 A Simulation: Probability Calculations . . . . . . . . . . . . .93
4.8 Invocation and Call-By-Value . . . . . . . . . . . . . . . . . . . .96
4.9 Problem Solving: A Computer Game . . . . . . . . . . . . . . .97
4.9.1 Twenty-One Pickup: Requirements Analysis . .98
4.9.2 Twenty-One Pickup: Design . . . . . . . . . . . . . .98
4.9.3 Twenty-one Pickup: Implementation . . . . . . .100
4.9.4 Twenty-one Pickup: Testing . . . . . . . . . . . . .105
4.10 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106
4.11 Problem Solving: Mathematical Functions . . . . . . . . . .108
4.12 Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . .109
4.13 Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . .111
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Applet Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Chapter 5
Arrays And Containers . . . . . . . . . . . . . . . . . . . . . . . . . .121
5.1 One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . .121
5.1.1 Indexing an Array Element . . . . . . . . . . . . . .122
5.1.2 Array Initialization . . . . . . . . . . . . . . . . . . . .122
5.1.3 Array Member Length . . . . . . . . . . . . . . . . . .124
5.2 The for Iterator statement . . . . . . . . . . . . . . . . . . . . .124
5.3 Passing Arrays to Methods . . . . . . . . . . . . . . . . . . . . .125
5.4 Array Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . .127
5.5 Finding the Minimum and Maximum of an Array . . . .128
5.6 A Simple Sorting Method . . . . . . . . . . . . . . . . . . . . . .130
5.7 Searching an Ordered Array . . . . . . . . . . . . . . . . . . . .132
Zgłoś jeśli naruszono regulamin