Think Java

Book description

Currently used at many colleges, universities, and high schools, this hands-on introduction to computer science is ideal for people with little or no programming experience. The goal of this concise book is not just to teach you Java, but to help you think like a computer scientist. You’ll learn how to program—a useful skill by itself—but you’ll also discover how to use programming as a means to an end.

Authors Allen Downey and Chris Mayfield start with the most basic concepts and gradually move into topics that are more complex, such as recursion and object-oriented programming. Each brief chapter covers the material for one week of a college course and includes exercises to help you practice what you’ve learned.

  • Learn one concept at a time: tackle complex topics in a series of small steps with examples
  • Understand how to formulate problems, think creatively about solutions, and write programs clearly and accurately
  • Determine which development techniques work best for you, and practice the important skill of debugging
  • Learn relationships among input and output, decisions and loops, classes and methods, strings and arrays
  • Work on exercises involving word games, graphics, puzzles, and playing cards

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. The Philosophy Behind the Book
    2. Object-Oriented Programming
    3. Appendixes
    4. Using the Code Examples
    5. Conventions Used in This Book
    6. O’Reilly Safari
    7. How to Contact Us
    8. Acknowledgments
  2. 1. The Way of the Program
    1. What Is Programming?
    2. What Is Computer Science?
    3. Programming Languages
    4. The Hello World Program
    5. Displaying Strings
    6. Escape Sequences
    7. Formatting Code
    8. Debugging Code
    9. Vocabulary
    10. Exercises
  3. 2. Variables and Operators
    1. Declaring Variables
    2. Assignment
    3. State Diagrams
    4. Printing Variables
    5. Arithmetic Operators
    6. Floating-Point Numbers
    7. Rounding Errors
    8. Operators for Strings
    9. Composition
    10. Types of Errors
    11. Vocabulary
    12. Exercises
  4. 3. Input and Output
    1. The System Class
    2. The Scanner Class
    3. Program Structure
    4. Inches to Centimeters
    5. Literals and Constants
    6. Formatting Output
    7. Centimeters to Inches
    8. Modulus Operator
    9. Putting It All Together
    10. The Scanner Bug
    11. Vocabulary
    12. Exercises
  5. 4. Void Methods
    1. Math Methods
    2. Composition Revisited
    3. Adding New Methods
    4. Flow of Execution
    5. Parameters and Arguments
    6. Multiple Parameters
    7. Stack Diagrams
    8. Reading Documentation
    9. Writing Documentation
    10. Vocabulary
    11. Exercises
  6. 5. Conditionals and Logic
    1. Relational Operators
    2. Logical Operators
    3. Conditional Statements
    4. Chaining and Nesting
    5. Flag Variables
    6. The return Statement
    7. Validating Input
    8. Recursive Methods
    9. Recursive Stack Diagrams
    10. Binary Numbers
    11. Vocabulary
    12. Exercises
  7. 6. Value Methods
    1. Return Values
    2. Writing Methods
    3. Method Composition
    4. Overloading
    5. Boolean Methods
    6. Javadoc Tags
    7. More Recursion
    8. Leap of Faith
    9. One More Example
    10. Vocabulary
    11. Exercises
  8. 7. Loops
    1. The while Statement
    2. Generating Tables
    3. Encapsulation and Generalization
    4. More Generalization
    5. The for Statement
    6. The do-while Loop
    7. break and continue
    8. Vocabulary
    9. Exercises
  9. 8. Arrays
    1. Creating Arrays
    2. Accessing Elements
    3. Displaying Arrays
    4. Copying Arrays
    5. Array Length
    6. Array Traversal
    7. Random Numbers
    8. Traverse and Count
    9. Building a Histogram
    10. The Enhanced for Loop
    11. Vocabulary
    12. Exercises
  10. 9. Strings and Things
    1. Characters
    2. Strings Are Immutable
    3. String Traversal
    4. Substrings
    5. The indexOf Method
    6. String Comparison
    7. String Formatting
    8. Wrapper Classes
    9. Command-Line Arguments
    10. Vocabulary
    11. Exercises
  11. 10. Objects
    1. Point Objects
    2. Attributes
    3. Objects as Parameters
    4. Objects as Return Types
    5. Mutable Objects
    6. Aliasing
    7. The null Keyword
    8. Garbage Collection
    9. Class Diagrams
    10. Java Library Source
    11. Vocabulary
    12. Exercises
  12. 11. Classes
    1. The Time Class
    2. Constructors
    3. More Constructors
    4. Getters and Setters
    5. Displaying Objects
    6. The toString Method
    7. The equals Method
    8. Adding Times
    9. Pure Methods and Modifiers
    10. Vocabulary
    11. Exercises
  13. 12. Arrays of Objects
    1. Card Objects
    2. Card toString
    3. Class Variables
    4. The compareTo Method
    5. Cards Are Immutable
    6. Arrays of Cards
    7. Sequential Search
    8. Binary Search
    9. Tracing the Code
    10. Recursive Version
    11. Vocabulary
    12. Exercises
  14. 13. Objects of Arrays
    1. The Deck Class
    2. Shuffling Decks
    3. Selection Sort
    4. Merge Sort
    5. Subdecks
    6. Merging Decks
    7. Adding Recursion
    8. Vocabulary
    9. Exercises
  15. 14. Objects of Objects
    1. Decks and Hands
    2. CardCollection
    3. Inheritance
    4. Dealing Cards
    5. The Player Class
    6. The Eights Class
    7. Class Relationships
    8. Vocabulary
    9. Exercises
  16. A. Development Tools
    1. Installing DrJava
    2. DrJava Interactions
    3. Command-Line Interface
    4. Command-Line Testing
    5. Running Checkstyle
    6. Tracing with a Debugger
    7. Testing with JUnit
    8. Vocabulary
  17. B. Java 2D Graphics
    1. Creating Graphics
    2. Graphics Methods
    3. Example Drawing
    4. Vocabulary
    5. Exercises
  18. C. Debugging
    1. Compile-Time Errors
      1. The compiler is spewing error messages.
      2. I’m getting a weird compiler message, and it won’t go away.
      3. I can’t get my program to compile no matter what I do.
      4. I did what the compiler told me to do, but it still doesn’t work.
    2. Run-Time Errors
      1. My program hangs.
      2. When I run the program I get an exception.
      3. I added so many print statements I get inundated with output.
    3. Logic Errors
      1. My program doesn’t work.
      2. I’ve got a big hairy expression and it doesn’t do what I expect.
      3. My method doesn’t return what I expect.
      4. My print statement isn’t doing anything.
      5. I’m really, really stuck and I need help.
      6. No, I really need help.
      7. I found the bug!
  19. Index

Product information

  • Title: Think Java
  • Author(s): Allen B. Downey, Chris Mayfield
  • Release date: May 2016
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491929568