O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learn Java the Easy Way

Book Description

"Java is the world’s most popular programming language, but it’s known for having a steep learning curve. Learn Java the Easy Way takes the chore out of learning Java with hands-on projects that will get you building real, functioning apps right away.

You’ll start by familiarizing yourself with JShell, Java’s interactive command line shell that allows programmers to run single lines of code and get immediate feedback. Then, you’ll create a guessing game, a secret message encoder, and a multitouch bubble-drawing app for both desktop and mobile devices using Eclipse, an industry-standard IDE, and Android Studio, the development environment for making Android apps.

As you build these apps, you’ll learn how to:

• Perform calculations, manipulate text strings, and generate random colors• Use conditions, loops, and methods to make your programs responsive and concise• Create functions to reuse code and save time• Build graphical user interface (GUI) elements, including buttons, menus, pop-ups, and sliders• Take advantage of Eclipse and Android Studio features to debug your code and find, fix, and prevent common mistakes

If you’ve been thinking about learning Java, Learn Java the Easy Way will bring you up to speed in no time."

Table of Contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Brief Contents
  5. Contents in Detail
  6. Acknowledgments
  7. Introduction
    1. Why Should I Learn to Code?
    2. Why Should I Learn Java?
    3. What’s in This Book
    4. What Tools Do I Need?
    5. Online Resources
    6. Start Now!
  8. 1: Getting Started
    1. Java on Windows, macOS, and Linux
    2. Installing Java 8 and 9 for Developers
    3. Installing the Eclipse IDE for Java Developers
    4. Setting Up Eclipse
      1. Installing the WindowBuilder Editor
      2. Customizing Eclipse’s Look and Feel
    5. Installing Android Studio for Mobile App Development
    6. Getting to Know Java with JShell
      1. Running JShell
      2. Working with Java Expressions in JShell
      3. Declaring Java Variables in JShell
      4. Printing Output in Java
      5. JShell Commands
    7. What You Learned
  9. 2: Build a Hi-Lo Guessing Game App!
    1. Planning the Game Step-by-Step
    2. Creating a New Java Project
    3. Creating the HiLo Class
      1. Generating a Random Number
      2. Getting User Input from the Keyboard
      3. Making the Program Print Output
    4. Loops: Ask, Check, Repeat
      1. if Statements: Testing for the Right Conditions
      2. Adding a Play Again Loop
    5. Testing the Game
    6. What You Learned
    7. Programming Challenges
      1. #1: Expanding Your Range
      2. #2: Counting Tries
      3. #3: Playing MadLibs
  10. 3: Creating a GUI for Our Guessing Game
    1. Practicing with JShell
      1. Creating a GUI in Four Lines of Code
      2. Creating an Interactive GUI in 10 Lines of Code!
    2. Setting Up the GUI App in Eclipse
    3. GUI Design with Eclipse’s WindowBuilder Editor
    4. Designing the User Interface
      1. Setting GUI Properties in the Properties Pane
      2. Customizing GUI Components in the Palette Pane
      3. Aligning GUI Elements
      4. Naming GUI Components for Coding
      5. Connecting the GUI to Your Java Code
    5. Adding a Method to Check the Player’s Guess
      1. Getting Text from a JTextField
      2. Converting Strings to Numbers
    6. Starting a New Game
    7. Listening for User Events: Click to Guess!
    8. Setting Up the GUI Window
    9. Time to Play!
    10. Adding a Play Again Feature
    11. Improving the UX
      1. Allowing Users to Press Enter to Guess
      2. Automatically Removing Old Guesses
    12. Handling Bad User Input
    13. What You Learned
    14. Programming Challenges
      1. #1: Showing Users How Many Tries They Took
      2. #2: Showing and Hiding a Play Again Button
      3. #3: Creating a GUI MadLib
  11. 4: Creating Your First Android App
    1. Starting a New Android Studio App Project
    2. Building the GUI Layout in Design View
    3. Naming GUI Components in Android Studio
    4. Connecting the GUI to Java in Android Studio
    5. Adding Methods to Check the Guess and Begin a New Game
    6. Handling Events in Android
    7. Running the App on the Android Emulator
    8. Running the App on a Real Android Device
      1. Preparing Your Device
      2. Connecting Your Device
      3. Running the App on Your Device
    9. Improving the UX
      1. Centering the User’s Guess in the Text Field
      2. Adding a Listener for the Enter Key
      3. Adding One More Finishing Touch
    10. What You Learned
    11. Programming Challenges
      1. #1: “Toast”-ing to the Number of Tries
      2. #2: Adding Visual Appeal
      3. #3: Creating a MadLibs Mobile App
  12. 5: Polishing Your App by Adding Menus and Preferences
    1. Adding an Options Menu in Android
      1. Adding Items to the Menu’s XML File
      2. Displaying the Options Menu
      3. Responding to User Selections
      4. Creating an Alert Dialog Pop-up for the About Screen
    2. Changing the Guessing Range
      1. Adding a Variable for the Range
      2. Using the range Variable
      3. Building the Dialog to Allow the User to Select the Range
    3. Storing User Preferences and Game Stats
      1. Storing and Retrieving the User’s Preferred Range
      2. Storing the Number of Games Won
    4. What You Learned
    5. Programming Challenges
      1. #1: You Win Some, You Lose Some
      2. #2: Ratio of Wins to Losses
  13. 6: Deciphering Secret Messages
    1. The Caesar Cipher
    2. Setting Up the Secret Messages App
      1. Creating the Secret Messages Project in Eclipse
      2. Beginning to Code SecretMessages.java
      3. Messing with Strings
    3. Characters and Values in Java
    4. Encoding Just the Letters
    5. Closing the Scanner
    6. Adding a Custom Key Value
    7. Encoding Digits
    8. Running Command Line Apps Without Eclipse
      1. Finding Your Workspace Folders
      2. Opening a Command Line Window
    9. What You Learned
    10. Programming Challenges
      1. #1: Looping the Loop
      2. #2: Reversing and Encoding
      3. #3: Safely Handling Keys with try and catch
  14. 7: Creating Advanced GUIs and Sharing Your App
    1. Setting Up the Secret Messages GUI App Project
    2. Designing the GUI and Naming Components
    3. Coding the Secret Messages GUI App
      1. Creating the encode() Method
      2. Writing the Event Handler for the Encode/Decode Button
      3. Handling Bad Input and User Errors
      4. Building the main() Method and Running the App
    4. Improving the GUI
      1. Setting Line Wrap and Word Wrap
      2. Handling Bad Input and User Errors: Part 2
      3. Adding a Slider to the Secret Messages GUI
    5. Code Cracking with the Slider
    6. Bonus: Sharing Your App as a Runnable JAR File
    7. What You Learned
    8. Programming Challenges
      1. #1: Movin’ On Up!
      2. #2: Scrolling Away!
      3. #3: Changing the Text to Change the Slider
  15. 8: Make Secret Messages a Phone App to Share with Friends!
    1. Setting Up the Mobile GUI
    2. Designing the Mobile GUI
    3. Wiring the GUI to the Java Code
      1. Connecting the Encode Button to the encode() Method
      2. Testing the App
      3. Working with the SeekBar
    4. Running the App on the Emulator and on an Android Device
    5. Bonus: Customizing the Floating Action Button
    6. Receiving Secret Messages from Other Apps
    7. What You Learned
    8. Programming Challenges
      1. #1: Creating a Move Up Button
      2. #2: Changing the SeekBar’s Progress
  16. 9: Paint Colorful Bubbles with Your Mouse!
    1. Creating the BubbleDraw Project Files
    2. Building the BubbleDraw Frame
    3. Creating a Class for Bubbles
      1. Defining a Bubble
      2. Designing a Bubble’s Methods
    4. Storing Bubbles in an ArrayList
      1. Adding a Constructor to the BubblePanel Class
      2. Adding a Method to Draw on the Screen
      3. Testing the BubblePanel Class
    5. Handling Mouse Events from the User
      1. Creating a Reusable Event Listener
      2. Handling Clicks and Drags
      3. Bonus: Handling MouseWheel Events
    6. What You Learned
    7. Programming Challenges
      1. #1: No Bubble Too Small
      2. #2: PixelDraw!
  17. 10: Adding Animation and Collision Detection with Timers
    1. Copying the BubbleDraw Java Project to Create BubbleDrawGUI
      1. Renaming the Main Class and Java File
      2. Adding Transparency
    2. Adding Animation: Bubbles Rising!
      1. Adding a Timer
      2. Setting the Timer
      3. Preparing the Animation
      4. Starting the Timer
    3. Forever Blowing Bubbles: Adding Random Speed and Direction
    4. Building a GUI for Our Animated Drawing App
      1. Setting Up the GUI Panel and Buttons
      2. Coding the Clear and Pause/Start Buttons
    5. Bouncing off the Walls with Collision Detection
      1. A Soft Bounce
      2. A Hard Bounce
    6. Adding a Slider to Control the Animation Speed
      1. Customizing the Slider
      2. Implementing the Slider Event Handler
    7. What You Learned
    8. Programming Challenges
      1. #1: No Bubble Left Behind
      2. #2: Flexi-Draw!
      3. #3: PixelDraw 2.0
  18. 11: Making BubbleDraw a Multitouch Android App
    1. Setting Up the BubbleDraw Project
    2. Creating the BubbleView Constructor
      1. Adding the Animation Variables
      2. Creating the BubbleView() Constructor
      3. Preparing the Layout to Use BubbleView
    3. Modifying the Bubble Class
    4. Drawing in Android with the onDraw() Method
    5. Testing BubbleDraw with 100 Bubbles
      1. Adding testBubbles()
      2. Fixing the OnTouchListener Error
      3. Running the BubbleDraw App
    6. Using Threaded Animation and Multitasking in Java
    7. Using Touch to Draw with Your Finger
      1. Using Multitouch to Draw with 10 Fingers at a Time!
      2. Testing Multitouch Events on an Android Device
    8. Changing the App Launcher Icon
      1. Creating a Custom App Icon
      2. Adding the Custom Icon to Your App
      3. Displaying Your New Icon
      4. Changing the App Name
    9. What You Learned
    10. Programming Challenges
      1. #1: Combining One-Finger and Multitouch Events, v1.0
      2. #2: Combining One-Finger and Multitouch Events, v2.0
  19. Appendix: Debugging and Avoiding Common Errors in Java
    1. Spelling and Case
      1. Correcting Typos in Eclipse
      2. Correcting Typos in Android Studio
      3. Avoiding Other Common Spelling Errors
    2. Comparison Trouble
    3. Grouping Symbols
      1. Quick Fixes in Eclipse
      2. Code Completion in Android Studio
    4. Summary
  20. Index
  21. Resources