Example: biology

Think Java: How to Think Like a Computer Scientist

Think java How to Think Like a Computer Scientist Version Think java How to Think Like a Computer Scientist Version Allen B. Downey and Chris Mayfield Green Tea Press Needham, Massachusetts Copyright 2016 Allen B. Downey and Chris Mayfield. Green Tea Press 9 Washburn Ave Needham, MA 02492. Permission is granted to copy, distribute, and/or modify this work under the terms of the Creative Commons Attribution-NonCommercial-ShareAlike Unported License, which is available at licenses/by-nc- The original form of this book is LATEX source code. Compiling this code has the effect of generating a device-independent representation of the book, which can be converted to other formats and printed.

But you can’t write Java programs (even hello world) without encountering object-oriented features. In some cases we explain a feature brie y when it rst appears, and then explain it more deeply later on. This book is well suited to prepare students for the AP Computer Science A exam, which includes object-oriented design and implementation ...

Tags:

  Programs, Think, Java, Think java, Java programs

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Think Java: How to Think Like a Computer Scientist

1 Think java How to Think Like a Computer Scientist Version Think java How to Think Like a Computer Scientist Version Allen B. Downey and Chris Mayfield Green Tea Press Needham, Massachusetts Copyright 2016 Allen B. Downey and Chris Mayfield. Green Tea Press 9 Washburn Ave Needham, MA 02492. Permission is granted to copy, distribute, and/or modify this work under the terms of the Creative Commons Attribution-NonCommercial-ShareAlike Unported License, which is available at licenses/by-nc- The original form of this book is LATEX source code. Compiling this code has the effect of generating a device-independent representation of the book, which can be converted to other formats and printed.

2 The LATEX source for this book is available from Contents Preface xiii 1 The way of the program 1. What is programming? .. 1. What is Computer science? .. 2. Programming languages .. 3. The hello world program .. 4. Displaying strings .. 6. Escape sequences .. 7. Formatting code .. 8. Debugging code .. 9. Vocabulary .. 10. Exercises .. 12. 2 Variables and operators 15. Declaring variables .. 15. Assignment .. 16. State diagrams .. 17. Printing variables .. 18. Arithmetic operators .. 19. vi CONTENTS. Floating-point numbers .. 21. Rounding errors .. 22. Operators for strings.

3 23. Composition .. 24. Types of errors .. 25. Vocabulary .. 28. Exercises .. 30. 3 Input and output 33. The System class .. 33. The Scanner class .. 34. Program structure .. 36. Inches to centimeters .. 37. Literals and constants .. 38. Formatting output .. 38. Centimeters to inches .. 40. Modulus operator .. 41. Putting it all together .. 41. The Scanner bug .. 43. Vocabulary .. 44. Exercises .. 45. 4 Void methods 49. Math methods .. 49. Composition revisited .. 50. Adding new methods .. 51. Flow of execution .. 54. CONTENTS vii Parameters and arguments .. 55. Multiple parameters.

4 56. Stack diagrams .. 57. Reading documentation .. 59. Writing documentation .. 61. Vocabulary .. 63. Exercises .. 63. 5 Conditionals and logic 67. Relational operators .. 67. Logical operators .. 68. Conditional statements .. 69. Chaining and nesting .. 71. Flag variables .. 72. The return statement .. 72. Validating input .. 73. Recursive methods .. 74. Recursive stack diagrams .. 76. Binary numbers .. 77. Vocabulary .. 79. Exercises .. 80. 6 Value methods 85. Return values .. 85. Writing methods .. 88. Method composition .. 90. Overloading .. 92. viii CONTENTS. Boolean methods.

5 93. Javadoc tags .. 94. More recursion .. 95. Leap of faith .. 97. One more example .. 98. Vocabulary .. 98. Exercises .. 99. 7 Loops 105. The while statement .. 105. Generating tables .. 107. Encapsulation and generalization .. 109. More generalization .. 112. The for statement .. 114. The do-while loop .. 115. Break and continue .. 116. Vocabulary .. 118. Exercises .. 118. 8 Arrays 123. Creating arrays .. 123. Accessing elements .. 124. Displaying arrays .. 125. Copying arrays .. 127. Array length .. 128. Array traversal .. 128. Random numbers .. 129. CONTENTS ix Traverse and count.

6 131. Building a histogram .. 131. The enhanced for loop .. 133. Vocabulary .. 134. Exercises .. 134. 9 Strings and things 139. Characters .. 139. Strings are immutable .. 141. String traversal .. 141. Substrings .. 143. The indexOf method .. 144. String comparison .. 144. String formatting .. 145. Wrapper classes .. 146. Command-line arguments .. 147. Vocabulary .. 148. Exercises .. 149. 10 Objects 155. Point objects .. 155. Attributes .. 156. Objects as parameters .. 157. Objects as return types .. 158. Mutable objects .. 159. Aliasing .. 160. The null keyword .. 161.

7 X CONTENTS. Garbage collection .. 162. Class diagrams .. 162. java library source .. 163. Vocabulary .. 164. Exercises .. 165. 11 Classes 171. The Time class .. 172. Constructors .. 173. More constructors .. 174. Getters and setters .. 176. Displaying objects .. 178. The toString method .. 179. The equals method .. 180. Adding times .. 181. Pure methods and modifiers .. 183. Vocabulary .. 184. Exercises .. 186. 12 Arrays of objects 189. Card objects .. 189. Card toString .. 191. Class variables .. 193. The compareTo method .. 194. Cards are immutable .. 195. Arrays of cards.

8 196. Sequential search .. 198. CONTENTS xi Binary search .. 199. Tracing the code .. 201. Recursive version .. 202. Vocabulary .. 202. Exercises .. 203. 13 Objects of arrays 205. The Deck class .. 205. Shuffling decks .. 207. Selection sort .. 208. Merge sort .. 208. Subdecks .. 209. Merging decks .. 210. Adding recursion .. 211. Vocabulary .. 212. Exercises .. 213. 14 Objects of objects 215. Decks and hands .. 216. CardCollection .. 216. Inheritance .. 219. Dealing cards .. 221. The Player class .. 223. The Eights class .. 226. Class relationships .. 230. Vocabulary.

9 231. Exercises .. 231. xii CONTENTS. A Development tools 233. Installing DrJava .. 233. DrJava interactions .. 235. Command-line interface .. 236. Command-line testing .. 237. Running Checkstyle .. 239. Tracing with a debugger .. 240. Testing with JUnit .. 241. Vocabulary .. 243. B java 2D graphics 245. Creating graphics .. 245. Graphics methods .. 246. Example drawing .. 248. Vocabulary .. 250. Exercises .. 250. C Debugging 253. Compile-time errors .. 253. Run-time errors .. 257. Logic errors .. 261. Index 267. Preface Think java is an introduction to Computer science and programming intended for readers with little or no experience.

10 We start with the most basic concepts and are careful to define all terms when they are first used. The book presents each new idea in a logical progression. Larger topics, like recursion and object- oriented programming, are divided into smaller examples and introduced over the course of several chapters. This book is intentionally concise. Each chapter is 12 14 pages and covers the material for one week of a college course. It is not meant to be a comprehensive presentation of java , but rather, an initial exposure to programming constructs and techniques. We begin with small problems and basic algorithms and work up to object-oriented design.


Related search queries