没有合适的资源?快使用搜索试试~ 我知道了~
首页java notes 第7版 Introduction to Programming Using Java
java notes 第7版 Introduction to Programming Using Java
需积分: 10 8 下载量 100 浏览量
更新于2023-03-16
评论
收藏 5.58MB PDF 举报
英文原版 Introduction to Programming Using Java Version 7.0, August 2014 David J. Eck Hobart and William Smith Colleges
资源详情
资源评论
资源推荐
Introduction to Programming Using Java
Version 7.0, August 2014
David J. Eck
Hobart and William Smith Colleges
This is a PDF version of a free, on-line book that is available
at http://math.hws.edu/javanotes/. The web site includes
source code for all example programs, answers to quizzes,
and discussions and solutions for exercises.
ii
c
1996–2014, David J. Eck
David J. Eck (eck@hws.edu)
Department of Mathemati cs 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 purp oses
provided they are distrib u ted under the same license as the original. More
specifically: This work is licensed under the Creative Commons Attributi on-
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 xiii
1 The Mental Landscape 1
1.1 Machine Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Asynchronous Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 The Java Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Building Blocks of Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5 Object-oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.6 The Modern User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.7 The Internet and Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Quiz on Chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2 Names and Things 19
2.1 The Basic Java Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2 Variables and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2.2 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.2.3 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.2.4 Strings and String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.5 Variables in Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.3 Objects and Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3.1 Built-in Subroutines and Functions . . . . . . . . . . . . . . . . . . . . . . 30
2.3.2 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.3.3 Operations on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.3.4 Introduction to Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.4 Text Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.4.1 Basic Output and Formatted Outupt . . . . . . . . . . . . . . . . . . . . . 38
2.4.2 A First Text Input Example . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.4.3 Basic TextIO Input Functions . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.4.4 Introduction to File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.4.5 Other TextIO Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.4.6 Using Scanner for Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.5 Details of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.5.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.5.2 Increment and Decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.5.3 Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.5.4 Boolean Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.5.5 Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
iii
iv CONTENTS
2.5.6 Assignment Operators and Type Conversion . . . . . . . . . . . . . . . . 51
2.5.7 Precedence Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.6 Programming Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.6.1 Java Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.6.2 Command Line Environment . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.6.3 Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.6.4 NetBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.6.5 BlueJ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.6.6 The Problem of Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Exercises for Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Quiz on Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3 Control 67
3.1 Blocks, Loops, and B ran ches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.1.1 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.1.2 The Basic While Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
3.1.3 The Basic If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.1.4 Definite Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
3.2 Algorithm Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.2.1 Pseudocode and Stepwise Refinement . . . . . . . . . . . . . . . . . . . . 74
3.2.2 The 3N+1 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
3.2.3 Coding, Testing, Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 80
3.3 while and do..while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.3.1 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.3.2 The do..while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.3.3 break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.4 The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.4.1 For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.4.2 Example: Counting Divisors . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.4.3 Nested for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.5 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.5.1 The Dangling else Problem . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.5.2 Multiway Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.5.3 If Statement Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.5.4 The Empty Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
3.6 The switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
3.6.1 The Basic switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 104
3.6.2 Menus and switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.6.3 Enums in switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . 108
3.6.4 Definite Assignment and switch Statements . . . . . . . . . . . . . . . . . 108
3.7 Exceptions and try..catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
3.7.1 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
3.7.2 try..catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
3.7.3 Exceptions in TextIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
3.8 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
3.8.1 Creating and Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 114
3.8.2 Arrays and For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
CONTENTS v
3.8.3 Random Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
3.8.4 Partially Full Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.8.5 Two-dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
3.9 GUI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
3.9.1 Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
3.9.2 Drawing in a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
3.9.3 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Exercises for Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Quiz on Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
4 Subroutines 135
4.1 Black Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
4.2 Static Subroutines and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
4.2.1 Subroutine Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
4.2.2 Calling Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
4.2.3 Subroutines in Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
4.2.4 Member Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
4.3 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
4.3.1 Using Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
4.3.2 Formal and Actual Parameters . . . . . . . . . . . . . . . . . . . . . . . . 147
4.3.3 Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
4.3.4 Subroutine Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
4.3.5 Array Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
4.3.6 Command-line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
4.3.7 Throwing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
4.3.8 Global and Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 154
4.4 Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
4.4.1 The return statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
4.4.2 Function Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
4.4.3 3N+1 Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
4.5 APIs, Packages, and Javadoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
4.5.1 Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
4.5.2 Java’s Standard Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
4.5.3 Using Classes from Packages . . . . . . . . . . . . . . . . . . . . . . . . . 163
4.5.4 Javadoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
4.5.5 Static Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
4.6 More on Program Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
4.6.1 Preconditions and Postconditions . . . . . . . . . . . . . . . . . . . . . . . 168
4.6.2 A Design Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
4.6.3 The Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
4.7 The Truth About Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
4.7.1 Initialization in Declarations . . . . . . . . . . . . . . . . . . . . . . . . . 175
4.7.2 Named Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
4.7.3 Naming and Scope Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Exercises for Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Quiz on Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
剩余761页未读,继续阅读
rockbee2009
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- stc12c5a60s2 例程
- Android通过全局变量传递数据
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0