Introduction to Programming using Java 2011.pdf

(6136 KB) Pobierz
Introduction to Programming Using Java
Version 6.0, June 2011
(Version 6.0.2, with minor corrections, May 2013)
David J. Eck
Hobart and William Smith Colleges
This is a PDF version of an on-line book that is available at
http://math.hws.edu/javanotes/.
The PDF does not include
source code files, solutions to exercises, or answers to quizzes, but
it does have external links to these resources, shown in blue.
In addition, each section has a link to the on-line version.
The PDF also has internal links, shown in red. These links can
be used in
Acrobat Reader
and some other PDF reader programs.
ii
c 1996–2013, David J. Eck
David J. Eck (eck@hws.edu)
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
Geneva, NY 14456
This book can be distributed in unmodified form for non-commercial purposes.
Modified versions can be made and distributed for non-commercial purposes
provided they are distributed under the same license as the original. More
specifically: This work is licensed under the Creative Commons Attribution-
NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit
http://creativecommons.org/licenses/by-nc-sa/3.0/. Other uses require
permission from the author.
The web site for this book is:
http://math.hws.edu/javanotes
Contents
Preface
1 The Mental Landscape
1.1 Machine Language
. . . . . .
1.2 Asynchronous Events
. . . . .
1.3 The Java Virtual Machine
. .
1.4 Building Blocks of Programs
1.5 Object-oriented Programming
1.6 The Modern User Interface
.
1.7 The Internet and Beyond
. .
Quiz on Chapter 1
. . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.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
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
x
1
1
3
6
8
10
13
15
18
19
19
22
23
24
27
28
29
33
35
36
37
38
39
42
43
45
46
47
47
48
49
50
50
51
52
2 Names and Things
2.1 The Basic Java Application
. . . . . . . . . .
2.2 Variables and Types
. . . . . . . . . . . . . .
2.2.1 Variables
. . . . . . . . . . . . . . . .
2.2.2 Types and Literals
. . . . . . . . . . .
2.2.3 Variables in Programs
. . . . . . . . .
2.3 Objects and Subroutines
. . . . . . . . . . . .
2.3.1 Built-in Subroutines and Functions
. .
2.3.2 Operations on Strings
. . . . . . . . .
2.3.3 Introduction to Enums
. . . . . . . . .
2.4 Text Input and Output
. . . . . . . . . . . .
2.4.1 A First Text Input Example
. . . . . .
2.4.2 Text Output
. . . . . . . . . . . . . .
2.4.3 TextIO Input Functions
. . . . . . . .
2.4.4 Formatted Output
. . . . . . . . . . .
2.4.5 Introduction to File I/O
. . . . . . . .
2.4.6 Using Scanner for Input
. . . . . . . .
2.5 Details of Expressions
. . . . . . . . . . . . .
2.5.1 Arithmetic Operators
. . . . . . . . .
2.5.2 Increment and Decrement
. . . . . . .
2.5.3 Relational Operators
. . . . . . . . . .
2.5.4 Boolean Operators
. . . . . . . . . . .
2.5.5 Conditional Operator
. . . . . . . . .
2.5.6 Assignment Operators and Type-Casts
2.5.7 Type Conversion of Strings
. . . . . .
2.5.8 Precedence Rules
. . . . . . . . . . . .
i
CONTENTS
2.6
Programming Environments
. . . . .
2.6.1 Java Development Kit
. . . .
2.6.2 Command Line Environment
2.6.3 IDEs and Eclipse
. . . . . . .
2.6.4 The Problem of Packages
. .
Exercises for Chapter 2
. . . . . . . . . .
Quiz on Chapter 2
. . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.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
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
ii
52
53
53
56
58
60
62
63
63
63
64
66
68
68
71
74
76
76
78
80
82
82
85
87
90
91
91
92
93
97
98
98
100
101
102
103
103
104
106
107
114
117
3 Control
3.1 Blocks, Loops, and Branches
. . . . . . . . .
3.1.1 Blocks
. . . . . . . . . . . . . . . . . .
3.1.2 The Basic While Loop
. . . . . . . . .
3.1.3 The Basic If Statement
. . . . . . . .
3.2 Algorithm Development
. . . . . . . . . . . .
3.2.1 Pseudocode and Stepwise Refinement
3.2.2 The 3N+1 Problem
. . . . . . . . . .
3.2.3 Coding, Testing, Debugging
. . . . . .
3.3 while and do..while
. . . . . . . . . . . . . . .
3.3.1 The while Statement
. . . . . . . . . .
3.3.2 The do..while Statement
. . . . . . . .
3.3.3 break and continue
. . . . . . . . . . .
3.4 The for Statement
. . . . . . . . . . . . . . .
3.4.1 For Loops
. . . . . . . . . . . . . . . .
3.4.2 Example: Counting Divisors
. . . . . .
3.4.3 Nested for Loops
. . . . . . . . . . . .
3.4.4 Enums and for-each Loops
. . . . . . .
3.5 The if Statement
. . . . . . . . . . . . . . . .
3.5.1 The Dangling else Problem
. . . . . .
3.5.2 The if...else if Construction
. . . . . .
3.5.3 If Statement Examples
. . . . . . . . .
3.5.4 The Empty Statement
. . . . . . . . .
3.6 The switch Statement
. . . . . . . . . . . . .
3.6.1 The Basic switch Statement
. . . . . .
3.6.2 Menus and switch Statements
. . . . .
3.6.3 Enums in switch Statements
. . . . .
3.6.4 Definite Assignment
. . . . . . . . . .
3.7 Exceptions and try..catch
. . . . . . . . . . .
3.7.1 Exceptions
. . . . . . . . . . . . . . .
3.7.2 try..catch
. . . . . . . . . . . . . . . .
3.7.3 Exceptions in TextIO
. . . . . . . . .
3.8 GUI Programming
. . . . . . . . . . . . . . .
Exercises for Chapter 3
. . . . . . . . . . . . . . .
Quiz on Chapter 3
. . . . . . . . . . . . . . . . . .
CONTENTS
4 Subroutines
4.1 Black Boxes
. . . . . . . . . . . . . . . .
4.2 Static Subroutines and Variables
. . . .
4.2.1 Subroutine Definitions
. . . . . .
4.2.2 Calling Subroutines
. . . . . . .
4.2.3 Subroutines in Programs
. . . . .
4.2.4 Member Variables
. . . . . . . .
4.3 Parameters
. . . . . . . . . . . . . . . .
4.3.1 Using Parameters
. . . . . . . . .
4.3.2 Formal and Actual Parameters
.
4.3.3 Overloading
. . . . . . . . . . . .
4.3.4 Subroutine Examples
. . . . . . .
4.3.5 Throwing Exceptions
. . . . . . .
4.3.6 Global and Local Variables
. . .
4.4 Return Values
. . . . . . . . . . . . . . .
4.4.1 The return statement
. . . . . .
4.4.2 Function Examples
. . . . . . . .
4.4.3 3N+1 Revisited
. . . . . . . . . .
4.5 APIs, Packages, and Javadoc
. . . . . .
4.5.1 Toolboxes
. . . . . . . . . . . . .
4.5.2 Java’s Standard Packages
. . . .
4.5.3 Using Classes from Packages
. .
4.5.4 Javadoc
. . . . . . . . . . . . . .
4.6 More on Program Design
. . . . . . . .
4.6.1 Preconditions and Postconditions
4.6.2 A Design Example
. . . . . . . .
4.6.3 The Program
. . . . . . . . . . .
4.7 The Truth About Declarations
. . . . .
4.7.1 Initialization in Declarations
. .
4.7.2 Named Constants
. . . . . . . .
4.7.3 Naming and Scope Rules
. . . .
Exercises for Chapter 4
. . . . . . . . . . . .
Quiz on Chapter 4
. . . . . . . . . . . . . . .
5 Objects and Classes
5.1 Objects and Instance Methods
. . . . .
5.1.1 Objects, Classes, and Instances
.
5.1.2 Fundamentals of Objects
. . . .
5.1.3 Getters and Setters
. . . . . . . .
5.2 Constructors and Object Initialization
.
5.2.1 Initializing Instance Variables
. .
5.2.2 Constructors
. . . . . . . . . . .
5.2.3 Garbage Collection
. . . . . . . .
5.3 Programming with Objects
. . . . . . .
5.3.1 Some Built-in Classes
. . . . . .
5.3.2 Wrapper Classes and Autoboxing
5.3.3 The class “Object”
. . . . . . . .
iii
119
. 119
. 121
. 121
. 123
. 124
. 127
. 129
. 129
. 130
. 132
. 133
. 135
. 135
. 136
. 136
. 137
. 140
. 142
. 142
. 143
. 144
. 146
. 148
. 149
. 149
. 154
. 156
. 156
. 157
. 160
. 163
. 167
168
. 168
. 169
. 170
. 175
. 176
. 176
. 177
. 182
. 183
. 183
. 184
. 186
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.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