CONTENTS IN DETAIL

ACKNOWLEDGMENTS

INTRODUCTION

Why Should I Learn to Code?

Why Should I Learn Java?

What’s in This Book

What Tools Do I Need?

Online Resources

Start Now!

1
GETTING STARTED

Java on Windows, macOS, and Linux

Installing Java 8 and 9 for Developers

Installing the Eclipse IDE for Java Developers

Setting Up Eclipse

Installing the WindowBuilder Editor

Customizing Eclipse’s Look and Feel

Installing Android Studio for Mobile App Development

Getting to Know Java with JShell

Running JShell

Working with Java Expressions in JShell

Declaring Java Variables in JShell

Printing Output in Java

JShell Commands

What You Learned

2
BUILD A HI-LO GUESSING GAME APP!

Planning the Game Step-by-Step

Creating a New Java Project

Creating the HiLo Class

Generating a Random Number

Getting User Input from the Keyboard

Making the Program Print Output

Loops: Ask, Check, Repeat

if Statements: Testing for the Right Conditions

Adding a Play Again Loop

Testing the Game

What You Learned

Programming Challenges

#1: Expanding Your Range

#2: Counting Tries

#3: Playing MadLibs

3
CREATING A GUI FOR OUR GUESSING GAME

Practicing with JShell

Creating a GUI in Four Lines of Code

Creating an Interactive GUI in 10 Lines of Code!

Setting Up the GUI App in Eclipse

GUI Design with Eclipse’s WindowBuilder Editor

Designing the User Interface

Setting GUI Properties in the Properties Pane

Customizing GUI Components in the Palette Pane

Aligning GUI Elements

Naming GUI Components for Coding

Connecting the GUI to Your Java Code

Adding a Method to Check the Player’s Guess

Getting Text from a JTextField

Converting Strings to Numbers

Starting a New Game

Listening for User Events: Click to Guess!

Setting Up the GUI Window

Time to Play!

Adding a Play Again Feature

Improving the UX

Allowing Users to Press Enter to Guess

Automatically Removing Old Guesses

Handling Bad User Input

What You Learned

Programming Challenges

#1: Showing Users How Many Tries They Took

#2: Showing and Hiding a Play Again Button

#3: Creating a GUI MadLib

4
CREATING YOUR FIRST ANDROID APP

Starting a New Android Studio App Project

Building the GUI Layout in Design View

Naming GUI Components in Android Studio

Connecting the GUI to Java in Android Studio

Adding Methods to Check the Guess and Begin a New Game

Handling Events in Android

Running the App on the Android Emulator

Running the App on a Real Android Device

Preparing Your Device

Connecting Your Device

Running the App on Your Device

Improving the UX

Centering the User’s Guess in the Text Field

Adding a Listener for the Enter Key

Adding One More Finishing Touch

What You Learned

Programming Challenges

#1: “Toast”-ing to the Number of Tries

#2: Adding Visual Appeal

#3: Creating a MadLibs Mobile App

5
POLISHING YOUR APP BY ADDING MENUS AND PREFERENCES

Adding an Options Menu in Android

Adding Items to the Menu’s XML File

Displaying the Options Menu

Responding to User Selections

Creating an Alert Dialog Pop-up for the About Screen

Changing the Guessing Range

Adding a Variable for the Range

Using the range Variable

Building the Dialog to Allow the User to Select the Range

Storing User Preferences and Game Stats

Storing and Retrieving the User’s Preferred Range

Storing the Number of Games Won

What You Learned

Programming Challenges

#1: You Win Some, You Lose Some

#2: Ratio of Wins to Losses

6
DECIPHERING SECRET MESSAGES

The Caesar Cipher

Setting Up the Secret Messages App

Creating the Secret Messages Project in Eclipse

Beginning to Code SecretMessages.java

Messing with Strings

Characters and Values in Java

Encoding Just the Letters

Closing the Scanner

Adding a Custom Key Value

Encoding Digits

Running Command Line Apps Without Eclipse

Finding Your Workspace Folders

Opening a Command Line Window

What You Learned

Programming Challenges

#1: Looping the Loop

#2: Reversing and Encoding

#3: Safely Handling Keys with try and catch

7
CREATING ADVANCED GUIs AND SHARING YOUR APP

Setting Up the Secret Messages GUI App Project

Designing the GUI and Naming Components

Coding the Secret Messages GUI App

Creating the encode() Method

Writing the Event Handler for the Encode/Decode Button

Handling Bad Input and User Errors

Building the main() Method and Running the App

Improving the GUI

Setting Line Wrap and Word Wrap

Handling Bad Input and User Errors: Part 2

Adding a Slider to the Secret Messages GUI

Code Cracking with the Slider

Bonus: Sharing Your App as a Runnable JAR File

What You Learned

Programming Challenges

#1: Movin’ On Up!

#2: Scrolling Away!

#3: Changing the Text to Change the Slider

8
MAKE SECRET MESSAGES A PHONE APP TO SHARE WITH FRIENDS!

Setting Up the Mobile GUI

Designing the Mobile GUI

Wiring the GUI to the Java Code

Connecting the Encode Button to the encode() Method

Testing the App

Working with the SeekBar

Running the App on the Emulator and on an Android Device

Bonus: Customizing the Floating Action Button

Receiving Secret Messages from Other Apps

What You Learned

Programming Challenges

#1: Creating a Move Up Button

#2: Changing the SeekBar’s Progress

9
PAINT COLORFUL BUBBLES WITH YOUR MOUSE!

Creating the BubbleDraw Project Files

Building the BubbleDraw Frame

Creating a Class for Bubbles

Defining a Bubble

Designing a Bubble’s Methods

Storing Bubbles in an ArrayList

Adding a Constructor to the BubblePanel Class

Adding a Method to Draw on the Screen

Testing the BubblePanel Class

Handling Mouse Events from the User

Creating a Reusable Event Listener

Handling Clicks and Drags

Bonus: Handling MouseWheel Events

What You Learned

Programming Challenges

#1: No Bubble Too Small

#2: PixelDraw!

10
ADDING ANIMATION AND COLLISION DETECTION WITH TIMERS

Copying the BubbleDraw Java Project to Create BubbleDrawGUI

Renaming the Main Class and Java File

Adding Transparency

Adding Animation: Bubbles Rising!

Adding a Timer

Setting the Timer

Preparing the Animation

Starting the Timer

Forever Blowing Bubbles: Adding Random Speed and Direction

Building a GUI for Our Animated Drawing App

Setting Up the GUI Panel and Buttons

Coding the Clear and Pause/Start Buttons

Bouncing off the Walls with Collision Detection

A Soft Bounce

A Hard Bounce

Adding a Slider to Control the Animation Speed

Customizing the Slider

Implementing the Slider Event Handler

What You Learned

Programming Challenges

#1: No Bubble Left Behind

#2: Flexi-Draw!

#3: PixelDraw 2.0

11
MAKING BUBBLEDRAW A MULTITOUCH ANDROID APP

Setting Up the BubbleDraw Project

Creating the BubbleView Constructor

Adding the Animation Variables

Creating the BubbleView() Constructor

Preparing the Layout to Use BubbleView

Modifying the Bubble Class

Drawing in Android with the onDraw() Method

Testing BubbleDraw with 100 Bubbles

Adding testBubbles()

Fixing the OnTouchListener Error

Running the BubbleDraw App

Using Threaded Animation and Multitasking in Java

Using Touch to Draw with Your Finger

Using Multitouch to Draw with 10 Fingers at a Time!

Testing Multitouch Events on an Android Device

Changing the App Launcher Icon

Creating a Custom App Icon

Adding the Custom Icon to Your App

Displaying Your New Icon

Changing the App Name

What You Learned

Programming Challenges

#1: Combining One-Finger and Multitouch Events, v1.0

#2: Combining One-Finger and Multitouch Events, v2.0

APPENDIX
DEBUGGING AND AVOIDING COMMON ERRORS IN JAVA

Spelling and Case

Correcting Typos in Eclipse

Correcting Typos in Android Studio

Avoiding Other Common Spelling Errors

Comparison Trouble

Grouping Symbols

Quick Fixes in Eclipse

Code Completion in Android Studio

Summary

INDEX