IntroductiontoProgrammingUsingJava - Hobart and William Smith ...

Introduction to Programming Using Java

Version 8.1.3, August 2021

(Minor update of Version 8.0, December 2018;

very minor update of Version 8.1, July 2019)

David J. Eck

Hobart and William Smith Colleges

This is a PDF version of a free, on-line book that is available

at . The web site includes

source code for all example programs, answers to quizzes,

and discussions and solutions for exercises.

ii

c 1996¨C2021, 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 AttributionNonCommercial-ShareAlike 4.0 License. To view a copy of this license, visit

. Other uses require

permission from the author.

The web site for this book is:



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 . . . . . . . . .

xiii

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1

1

3

7

9

11

13

15

18

2 Names and Things

2.1 The Basic Java Application . . . . . . . . . .

2.2 Variables and Types . . . . . . . . . . . . . .

2.2.1 Variables . . . . . . . . . . . . . . . .

2.2.2 Types . . . . . . . . . . . . . . . . . .

2.2.3 Literals . . . . . . . . . . . . . . . . .

2.2.4 Strings and String Literals . . . . . . .

2.2.5 Variables in Programs . . . . . . . . .

2.3 Objects and Subroutines . . . . . . . . . . . .

2.3.1 Built-in Subroutines and Functions . .

2.3.2 Classes and Objects . . . . . . . . . .

2.3.3 Operations on Strings . . . . . . . . .

2.3.4 Introduction to Enums . . . . . . . . .

2.3.5 Text Blocks: Multiline Strings . . . .

2.4 Text Input and Output . . . . . . . . . . . .

2.4.1 Basic Output and Formatted Output .

2.4.2 A First Text Input Example . . . . . .

2.4.3 Basic TextIO Input Functions . . . . .

2.4.4 Introduction to File I/O . . . . . . . .

2.4.5 Other TextIO Features . . . . . . . . .

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 . . . . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

19

19

23

24

25

26

27

28

29

30

33

34

36

38

38

38

40

42

44

46

47

48

49

50

51

51

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

iii

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

iv

CONTENTS

2.5.5 Conditional Operator . . . . . . . . . . . . .

2.5.6 Assignment Operators and Type Conversion

2.5.7 Precedence Rules . . . . . . . . . . . . . . . .

2.6 Programming Environments . . . . . . . . . . . . . .

2.6.1 Getting JDK and JavaFX . . . . . . . . . . .

2.6.2 Command Line Environment . . . . . . . . .

2.6.3 JavaFX on the Command Line . . . . . . . .

2.6.4 Eclipse IDE . . . . . . . . . . . . . . . . . . .

2.6.5 Using JavaFX in Eclipse . . . . . . . . . . . .

2.6.6 BlueJ . . . . . . . . . . . . . . . . . . . . . .

2.6.7 The Problem of Packages . . . . . . . . . . .

2.6.8 About jshell . . . . . . . . . . . . . . . . . . .

Exercises for Chapter 2 . . . . . . . . . . . . . . . . . . .

Quiz on Chapter 2 . . . . . . . . . . . . . . . . . . . . . .

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.1.4 Definite Assignment . . . . . . . . . . . . .

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.5 The if Statement . . . . . . . . . . . . . . . . . . .

3.5.1 The Dangling else Problem . . . . . . . . .

3.5.2 Multiway Branching . . . . . . . . . . . . .

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 and switch Statements

3.6.5 A New switch Statement Syntax . . . . . .

3.7 Exceptions and try..catch . . . . . . . . . . . . . .

3.7.1 Exceptions . . . . . . . . . . . . . . . . . .

3.7.2 try..catch . . . . . . . . . . . . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

52

52

54

55

55

57

59

61

65

67

68

69

70

72

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

73

73

73

74

77

79

80

80

83

86

88

88

90

92

94

94

97

99

102

103

103

105

109

110

110

112

114

114

115

116

116

117

CONTENTS

3.7.3 Exceptions in TextIO . . .

3.8 Introduction to Arrays . . . . . . .

3.8.1 Creating and Using Arrays

3.8.2 Arrays and For Loops . . .

3.8.3 Random Access . . . . . . .

3.8.4 Partially Full Arrays . . . .

3.8.5 Two-dimensional Arrays . .

3.9 GUI Programming . . . . . . . . .

3.9.1 Drawing Shapes . . . . . .

3.9.2 Drawing in a Program . . .

3.9.3 Animation . . . . . . . . . .

Exercises for Chapter 3 . . . . . . . . .

Quiz on Chapter 3 . . . . . . . . . . . .

v

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

119

121

121

123

124

126

127

129

129

133

134

137

141

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 Array Parameters . . . . . . .

4.3.6 Command-line Arguments . . .

4.3.7 Throwing Exceptions . . . . . .

4.3.8 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 Lambda Expressions . . . . . . . . . .

4.5.1 First-class Functions . . . . . .

4.5.2 Functional Interfaces . . . . . .

4.5.3 Lambda Expressions . . . . . .

4.5.4 Method References . . . . . . .

4.6 APIs, Packages, Modules, and Javadoc

4.6.1 Toolboxes . . . . . . . . . . . .

4.6.2 Java¡¯s Standard Packages . . .

4.6.3 Using Classes from Packages .

4.6.4 About Modules . . . . . . . . .

4.6.5 Javadoc . . . . . . . . . . . . .

4.6.6 Static Import . . . . . . . . . .

4.7 More on Program Design . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

143

143

145

146

147

148

151

154

154

155

156

157

159

160

161

162

163

163

164

167

168

169

170

170

173

173

174

175

176

178

179

181

182

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download