Think Java, 2nd Edition

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

The updated second edition of Think Java also features new chapters on polymorphism and data processing, as well as content covering changes through Java 12.

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. The Philosophy Behind the Book
    2. Object-Oriented Programming
    3. Changes to the Second Edition
    4. About the Appendixes
    5. Using the Code Examples
    6. Conventions Used in This Book
    7. O’Reilly Online Learning
    8. How to Contact Us
    9. Acknowledgments
  2. 1. Computer Programming
    1. What Is a Computer?
    2. What Is Programming?
    3. The Hello World Program
    4. Compiling Java Programs
    5. Displaying Two Messages
    6. Formatting Source Code
    7. Using Escape Sequences
    8. What Is Computer Science?
    9. Debugging Programs
    10. Vocabulary
    11. Exercises
  3. 2. Variables and Operators
    1. Declaring Variables
    2. Assigning Variables
    3. Memory Diagrams
    4. Printing Variables
    5. Arithmetic Operators
    6. Floating-Point Numbers
    7. Rounding Errors
    8. Operators for Strings
    9. Compiler Error Messages
    10. Other Types of Errors
    11. Vocabulary
    12. Exercises
  4. 3. Input and Output
    1. The System Class
    2. The Scanner Class
    3. Language Elements
    4. Literals and Constants
    5. Formatting Output
    6. Reading Error Messages
    7. Type Cast Operators
    8. Remainder Operator
    9. Putting It All Together
    10. The Scanner Bug
    11. Vocabulary
    12. Exercises
  5. 4. Methods and Testing
    1. Defining New Methods
    2. Flow of Execution
    3. Parameters and Arguments
    4. Multiple Parameters
    5. Stack Diagrams
    6. Math Methods
    7. Composition
    8. Return Values
    9. Incremental Development
    10. Vocabulary
    11. Exercises
  6. 5. Conditionals and Logic
    1. Relational Operators
    2. The if-else Statement
    3. Chaining and Nesting
    4. The switch Statement
    5. Logical Operators
    6. De Morgan’s Laws
    7. Boolean Variables
    8. Boolean Methods
    9. Validating Input
    10. Example Program
    11. Vocabulary
    12. Exercises
  7. 6. Loops and Strings
    1. The while Statement
    2. Increment and Decrement
    3. The for Statement
    4. Nested Loops
    5. Characters
    6. Which Loop to Use
    7. String Iteration
    8. The indexOf Method
    9. Substrings
    10. String Comparison
    11. String Formatting
    12. Vocabulary
    13. Exercises
  8. 7. Arrays and References
    1. Creating Arrays
    2. Accessing Elements
    3. Displaying Arrays
    4. Copying Arrays
    5. Traversing Arrays
    6. Generating Random Numbers
    7. Building a Histogram
    8. The Enhanced for Loop
    9. Counting Characters
    10. Vocabulary
    11. Exercises
  9. 8. Recursive Methods
    1. Recursive Void Methods
    2. Recursive Stack Diagrams
    3. Value-Returning Methods
    4. The Leap of Faith
    5. Counting Up Recursively
    6. Binary Number System
    7. Recursive Binary Method
    8. CodingBat Problems
    9. Vocabulary
    10. Exercises
  10. 9. Immutable Objects
    1. Primitives Versus Objects
    2. The null Keyword
    3. Strings Are Immutable
    4. Wrapper Classes
    5. Command-Line Arguments
    6. Argument Validation
    7. BigInteger Arithmetic
    8. Incremental Design
    9. More Generalization
    10. Vocabulary
    11. Exercises
  11. 10. Mutable Objects
    1. Point Objects
    2. Objects as Parameters
    3. Objects as Return Values
    4. Rectangles Are Mutable
    5. Aliasing Revisited
    6. Java Library Source
    7. Class Diagrams
    8. Scope Revisited
    9. Garbage Collection
    10. Mutable Versus Immutable
    11. StringBuilder Objects
    12. Vocabulary
    13. Exercises
  12. 11. Designing Classes
    1. The Time Class
    2. Constructors
    3. Value Constructors
    4. Getters and Setters
    5. Displaying Objects
    6. The toString Method
    7. The equals Method
    8. Adding Times
    9. Vocabulary
    10. 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. Vocabulary
    11. Exercises
  14. 13. Objects of Arrays
    1. Decks of Cards
    2. Shuffling Decks
    3. Selection Sort
    4. Merge Sort
    5. Subdecks
    6. Merging Decks
    7. Adding Recursion
    8. Static Context
    9. Piles of Cards
    10. Playing War
    11. Vocabulary
    12. Exercises
  15. 14. Extending Classes
    1. CardCollection
    2. Inheritance
    3. Dealing Cards
    4. The Player Class
    5. The Eights Class
    6. Class Relationships
    7. Vocabulary
    8. Exercises
  16. 15. Arrays of Arrays
    1. Conway’s Game of Life
    2. The Cell Class
    3. Two-Dimensional Arrays
    4. The GridCanvas Class
    5. Other Grid Methods
    6. Starting the Game
    7. The Simulation Loop
    8. Exception Handling
    9. Counting Neighbors
    10. Updating the Grid
    11. Vocabulary
    12. Exercises
  17. 16. Reusing Classes
    1. Langton’s Ant
    2. Refactoring
    3. Abstract Classes
    4. UML Diagram
    5. Vocabulary
    6. Exercises
  18. 17. Advanced Topics
    1. Polygon Objects
    2. Adding Color
    3. Regular Polygons
    4. More Constructors
    5. An Initial Drawing
    6. Blinking Polygons
    7. Interfaces
    8. Event Listeners
    9. Timers
    10. Vocabulary
    11. Exercises
  19. A. 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
  20. B. Javadoc
    1. Reading Documentation
    2. Writing Documentation
    3. Javadoc Tags
    4. Example Source File
    5. Vocabulary
  21. C. Graphics
    1. Creating Graphics
    2. Graphics Methods
    3. Example Drawing
    4. Vocabulary
    5. Exercises
  22. D. 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!
  23. Index

Product information

  • Title: Think Java, 2nd Edition
  • Author(s): Allen B. Downey, Chris Mayfield
  • Release date: November 2019
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492072454