实例介绍
【实例简介】
【实例截图】
【核心代码】
Contents Preface xiii 1 The way of the program 1 1.1 What is programming? . . . . . . . . . . . . . . . . . . . . . 1 1.2 What is computer science? . . . . . . . . . . . . . . . . . . . 2 1.3 Programming languages . . . . . . . . . . . . . . . . . . . . 3 1.4 The hello world program . . . . . . . . . . . . . . . . . . . . 4 1.5 Displaying strings . . . . . . . . . . . . . . . . . . . . . . . . 6 1.6 Escape sequences . . . . . . . . . . . . . . . . . . . . . . . . 7 1.7 Formatting code . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.8 Debugging code . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.9 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2 Variables and operators 15 2.1 Declaring variables . . . . . . . . . . . . . . . . . . . . . . . 15 2.2 Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.3 State diagrams . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.4 Printing variables . . . . . . . . . . . . . . . . . . . . . . . . 18 2.5 Arithmetic operators . . . . . . . . . . . . . . . . . . . . . . 19 vi CONTENTS 2.6 Floating-point numbers . . . . . . . . . . . . . . . . . . . . . 21 2.7 Rounding errors . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.8 Operators for strings . . . . . . . . . . . . . . . . . . . . . . 23 2.9 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.10 Types of errors . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3 Input and output 33 3.1 The System class . . . . . . . . . . . . . . . . . . . . . . . . 33 3.2 The Scanner class . . . . . . . . . . . . . . . . . . . . . . . . 34 3.3 Program structure . . . . . . . . . . . . . . . . . . . . . . . . 36 3.4 Inches to centimeters . . . . . . . . . . . . . . . . . . . . . . 37 3.5 Literals and constants . . . . . . . . . . . . . . . . . . . . . . 38 3.6 Formatting output . . . . . . . . . . . . . . . . . . . . . . . 38 3.7 Centimeters to inches . . . . . . . . . . . . . . . . . . . . . . 40 3.8 Modulus operator . . . . . . . . . . . . . . . . . . . . . . . . 41 3.9 Putting it all together . . . . . . . . . . . . . . . . . . . . . . 41 3.10 The Scanner bug . . . . . . . . . . . . . . . . . . . . . . . . 43 3.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 4 Void methods 49 4.1 Math methods . . . . . . . . . . . . . . . . . . . . . . . . . . 49 4.2 Composition revisited . . . . . . . . . . . . . . . . . . . . . . 50 4.3 Adding new methods . . . . . . . . . . . . . . . . . . . . . . 51 4.4 Flow of execution . . . . . . . . . . . . . . . . . . . . . . . . 54 CONTENTS vii 4.5 Parameters and arguments . . . . . . . . . . . . . . . . . . . 55 4.6 Multiple parameters . . . . . . . . . . . . . . . . . . . . . . . 56 4.7 Stack diagrams . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.8 Reading documentation . . . . . . . . . . . . . . . . . . . . . 59 4.9 Writing documentation . . . . . . . . . . . . . . . . . . . . . 61 4.10 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 4.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 5 Conditionals and logic 67 5.1 Relational operators . . . . . . . . . . . . . . . . . . . . . . . 67 5.2 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . 68 5.3 Conditional statements . . . . . . . . . . . . . . . . . . . . . 69 5.4 Chaining and nesting . . . . . . . . . . . . . . . . . . . . . . 71 5.5 Flag variables . . . . . . . . . . . . . . . . . . . . . . . . . . 72 5.6 The return statement . . . . . . . . . . . . . . . . . . . . . . 72 5.7 Validating input . . . . . . . . . . . . . . . . . . . . . . . . . 73 5.8 Recursive methods . . . . . . . . . . . . . . . . . . . . . . . 74 5.9 Recursive stack diagrams . . . . . . . . . . . . . . . . . . . . 76 5.10 Binary numbers . . . . . . . . . . . . . . . . . . . . . . . . . 77 5.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 6 Value methods 85 6.1 Return values . . . . . . . . . . . . . . . . . . . . . . . . . . 85 6.2 Writing methods . . . . . . . . . . . . . . . . . . . . . . . . 88 6.3 Method composition . . . . . . . . . . . . . . . . . . . . . . 90 6.4 Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 viii CONTENTS 6.5 Boolean methods . . . . . . . . . . . . . . . . . . . . . . . . 93 6.6 Javadoc tags . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 6.7 More recursion . . . . . . . . . . . . . . . . . . . . . . . . . . 95 6.8 Leap of faith . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 6.9 One more example . . . . . . . . . . . . . . . . . . . . . . . 98 6.10 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 6.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 7 Loops 105 7.1 The while statement . . . . . . . . . . . . . . . . . . . . . . 105 7.2 Generating tables . . . . . . . . . . . . . . . . . . . . . . . . 107 7.3 Encapsulation and generalization . . . . . . . . . . . . . . . 109 7.4 More generalization . . . . . . . . . . . . . . . . . . . . . . . 112 7.5 The for statement . . . . . . . . . . . . . . . . . . . . . . . . 114 7.6 The do-while loop . . . . . . . . . . . . . . . . . . . . . . . . 115 7.7 Break and continue . . . . . . . . . . . . . . . . . . . . . . . 116 7.8 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 7.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 8 Arrays 123 8.1 Creating arrays . . . . . . . . . . . . . . . . . . . . . . . . . 123 8.2 Accessing elements . . . . . . . . . . . . . . . . . . . . . . . 124 8.3 Displaying arrays . . . . . . . . . . . . . . . . . . . . . . . . 125 8.4 Copying arrays . . . . . . . . . . . . . . . . . . . . . . . . . 127 8.5 Array length . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 8.6 Array traversal . . . . . . . . . . . . . . . . . . . . . . . . . 128 8.7 Random numbers . . . . . . . . . . . . . . . . . . . . . . . . 129 CONTENTS ix 8.8 Traverse and count . . . . . . . . . . . . . . . . . . . . . . . 131 8.9 Building a histogram . . . . . . . . . . . . . . . . . . . . . . 131 8.10 The enhanced for loop . . . . . . . . . . . . . . . . . . . . . 133 8.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 8.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 9 Strings and things 139 9.1 Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 9.2 Strings are immutable . . . . . . . . . . . . . . . . . . . . . 141 9.3 String traversal . . . . . . . . . . . . . . . . . . . . . . . . . 141 9.4 Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 9.5 The indexOf method . . . . . . . . . . . . . . . . . . . . . . 144 9.6 String comparison . . . . . . . . . . . . . . . . . . . . . . . . 144 9.7 String formatting . . . . . . . . . . . . . . . . . . . . . . . . 145 9.8 Wrapper classes . . . . . . . . . . . . . . . . . . . . . . . . . 146 9.9 Command-line arguments . . . . . . . . . . . . . . . . . . . . 147 9.10 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 9.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 10 Objects 155 10.1 Point objects . . . . . . . . . . . . . . . . . . . . . . . . . . 155 10.2 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 10.3 Objects as parameters . . . . . . . . . . . . . . . . . . . . . 157 10.4 Objects as return types . . . . . . . . . . . . . . . . . . . . . 158 10.5 Mutable objects . . . . . . . . . . . . . . . . . . . . . . . . . 159 10.6 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 10.7 The null keyword . . . . . . . . . . . . . . . . . . . . . . . . 161 x CONTENTS 10.8 Garbage collection . . . . . . . . . . . . . . . . . . . . . . . 162 10.9 Class diagrams . . . . . . . . . . . . . . . . . . . . . . . . . 162 10.10 Java library source . . . . . . . . . . . . . . . . . . . . . . . 163 10.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 10.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 11 Classes 171 11.1 The Time class . . . . . . . . . . . . . . . . . . . . . . . . . 172 11.2 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 11.3 More constructors . . . . . . . . . . . . . . . . . . . . . . . . 174 11.4 Getters and setters . . . . . . . . . . . . . . . . . . . . . . . 176 11.5 Displaying objects . . . . . . . . . . . . . . . . . . . . . . . . 178 11.6 The toString method . . . . . . . . . . . . . . . . . . . . . . 179 11.7 The equals method . . . . . . . . . . . . . . . . . . . . . . . 180 11.8 Adding times . . . . . . . . . . . . . . . . . . . . . . . . . . 181 11.9 Pure methods and modifiers . . . . . . . . . . . . . . . . . . 183 11.10 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 11.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 12 Arrays of objects 189 12.1 Card objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 12.2 Card toString . . . . . . . . . . . . . . . . . . . . . . . . . . 191 12.3 Class variables . . . . . . . . . . . . . . . . . . . . . . . . . . 193 12.4 The compareTo method . . . . . . . . . . . . . . . . . . . . 194 12.5 Cards are immutable . . . . . . . . . . . . . . . . . . . . . . 195 12.6 Arrays of cards . . . . . . . . . . . . . . . . . . . . . . . . . 196 12.7 Sequential search . . . . . . . . . . . . . . . . . . . . . . . . 198 CONTENTS xi 12.8 Binary search . . . . . . . . . . . . . . . . . . . . . . . . . . 199 12.9 Tracing the code . . . . . . . . . . . . . . . . . . . . . . . . 201 12.10 Recursive version . . . . . . . . . . . . . . . . . . . . . . . . 202 12.11 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 12.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 13 Objects of arrays 205 13.1 The Deck class . . . . . . . . . . . . . . . . . . . . . . . . . 205 13.2 Shuffling decks . . . . . . . . . . . . . . . . . . . . . . . . . . 207 13.3 Selection sort . . . . . . . . . . . . . . . . . . . . . . . . . . 208 13.4 Merge sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 13.5 Subdecks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 13.6 Merging decks . . . . . . . . . . . . . . . . . . . . . . . . . . 210 13.7 Adding recursion . . . . . . . . . . . . . . . . . . . . . . . . 211 13.8 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 13.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 14 Objects of objects 215 14.1 Decks and hands . . . . . . . . . . . . . . . . . . . . . . . . 216 14.2 CardCollection . . . . . . . . . . . . . . . . . . . . . . . . . 216 14.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 14.4 Dealing cards . . . . . . . . . . . . . . . . . . . . . . . . . . 221 14.5 The Player class . . . . . . . . . . . . . . . . . . . . . . . . . 223 14.6 The Eights class . . . . . . . . . . . . . . . . . . . . . . . . . 226 14.7 Class relationships . . . . . . . . . . . . . . . . . . . . . . . 230 14.8 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 14.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 xii CONTENTS A Development tools 233 A.1 Installing DrJava . . . . . . . . . . . . . . . . . . . . . . . . 233 A.2 DrJava interactions . . . . . . . . . . . . . . . . . . . . . . . 235 A.3 Command-line interface . . . . . . . . . . . . . . . . . . . . . 236 A.4 Command-line testing . . . . . . . . . . . . . . . . . . . . . . 237 A.5 Running Checkstyle . . . . . . . . . . . . . . . . . . . . . . . 239 A.6 Tracing with a debugger . . . . . . . . . . . . . . . . . . . . 240 A.7 Testing with JUnit . . . . . . . . . . . . . . . . . . . . . . . 241 A.8 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 B Java 2D graphics 245 B.1 Creating graphics . . . . . . . . . . . . . . . . . . . . . . . . 245 B.2 Graphics methods . . . . . . . . . . . . . . . . . . . . . . . . 246 B.3 Example drawing . . . . . . . . . . . . . . . . . . . . . . . . 248 B.4 Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 B.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 C Debugging 253 C.1 Compile-time errors . . . . . . . . . . . . . . . . . . . . . . . 253 C.2 Run-time errors . . . . . . . . . . . . . . . . . . . . . . . . . 257 C.3 Logic errors . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Index 267
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论