Table of
Contents
Learn to Program with Java
CHAPTER 1---WHERE DO I BEGIN?
WHERE DO WE BEGIN
Programming the Easy Way
PLANNING A PROGRAM IS LIKE PLANNING A HOUSE
WE RECEIVE A CALL FROM OUR 'CLIENT'
WE MEET WITH OUR CLIENT
THE SYSTEMS DEVELOPMENT LIFE CYCLE (SDLC)
PHASE 1: THE PRELIMINARY INVESTIGATION
PHASE 2: ANALYSIS PHASE
PHASE 3: DESIGN PHASE
Output Design
Input Design
Processing Design
PHASE 4: DEVELOPMENT PHASE
PHASE 5: IMPLEMENTATION PHASE
PHASE 6: AUDIT AND MAINTENANCE PHASE
WHERE TO FROM HERE?
SUMMARY
CHAPTER 2--- GETTING COMFORTABLE WITH JAVA
GETTING COMFORTABLE WITH JAVA
WRITING OUR FIRST JAVA PROGRAM
Create the Source File with a file name extension of .java
Compile the Java Source File into a Bytecode File
Common Compiler Errors
Oops: Invalid Option or Argument
Oops: NoClassDefFoundError
Oops: Can’t Find the Compiler
Run the Java Program
ELEMENTS OF A JAVA PROGRAM
Program Comments
The Class: The Essence of a Java Program
The main() Method
CHAPTER 3---DATA
COMPUTER DATA
VARIABLES
Our first variable: The Local Variable
Declaring a Variable
Assigning a Value to a Variable
Declaration and Assignment Combined
A Quick Look at Instance and Static Variables
Variable Scope and Lifetime
CONSTANTS
JAVA DATA TYPES
NUMERIC DATA TYPES
Byte
Short
Int
Long
Float
Double
NONNUMERIC DATA TYPES
Boolean
Char
THE STRING OBJECT
OPERATIONS ON DATA
ARITHMETIC OPERATIONS
The Addition Operator (+)
The Subtraction Operator (-)
The Multiplication Operator (*)
The Division Operator (/)
The Modulus Operator (%)
The Increment Operator (++)
The Decrement Operator (--)
Order of Operations
COMPARISON OPERATORS
LOGICAL OPERATORS
The And Operator (&)
The Or Operator (|)
The Not Operator (!)
SUMMARY
CHAPTER 4---SELECTION STRUCTURES
SELECTION STRUCTURES
THE SEQUENCE STRUCTURE---FALLING ROCK
THE JAVA SELECTION STRUCTURE---THE IF STATEMENT
AN INTRODUCTION TO WINDOWS
Windows Out
Oops, There's Problem
Windows In
THE IF…ELSE STATEMENT
STRING RESPONSE;
THE SWITCH STATEMENT
CONTINUING WITH THE GRADES CALCULATION PROJECT
SUMMARY
CHAPTER 5---LOOPS
WHY LOOPS?
THE FOR LOOP
Variations on the For Loop Theme
WHILE LOOPS
The While Loop
Do-While Loop
ADDING A LOOP TO THE GRADES CALCULATION PROJECT
SUMMARY
CHAPTER 6---CREATING YOUR OWN METHODS
MODULAR PROGRAMS ARE EASIER TO MAINTAIN AND UNDERSTAND
WHAT IS A METHOD?
CREATING YOUR OWN METHODS…
METHOD HEADER
ACCESS SPECIFERS
Public Access Specifer
Private Access Specifer
Protected Access Specifer
Package Access Specifer
Access Specifers Are NOT Class Specifiers
The Return Type
Method Parameters and Arguments
In Java, by Default, Arguments are passed by Value
USING METHODS TO FINE TUNE YOUR CODE
METHOD OVERLOADING
CHAPTER 7---CREATING OBJECTS FROM INSTANTIABLE CLASSES
CREATING OBJECTS FROM INSTANTIABLE CLASSES
CREATING CLASSES IS AN EXTENSION OF MODULAR PROGRAMMING
Objects Have Data that Simulate Object Characteristics
Objects Have Behavior
CREATING OBJECTS FROM YOUR CLASSES
Changing an Object's Attributes
Calling an Object's Methods
CREATING MULTIPLE OBJECTS FROM YOUR CLASSES
CLASS CONSTRUCTORS
CLASS CONTRACTS
OVERLOADED CONSTRUCTORS
CLASS VARIABLES
DESTROYING AN OBJECT---THE JAVA GARBAGE COLLECTOR
CLASS FINALIZERS
SUMMARY
CHAPTER 8---CONTROLLING ACCESS TO THE DATA IN YOUR OBJECT
CONTROLLING ACCESS TO YOUR OBJECT'S DATA
INSTANCE VARIABLES: PUBLIC OR PRIVATE?
USING SET AND GET STATEMENTS
Mutator Methods
Accessor Methods
LET'S ANALYZE THE GRADES CALCULATION PROJECT FOR DATA INTEGRITY
A surprise visit from Frank Olley
SUMMARY
CHAPTER 9---INHERITANCE AND INTERFACES
INHERITANCE
BEFORE INHERITANCE CAME ALONG...
CREATING CLASSES FROM OTHER CLASSES USING INHERITANCE
THE SUPERCLASS (PARENT CLASS)
THE SUBCLASS (CHILD CLASS)
PLANNING YOUR OBJECT HIERARCHY IN ADVANCE
ABSTRACT METHODS AND CLASSES
INTERFACES ARE NOT INHERITANCE
CREATING A SUPERCLASS AND SUBCLASSES IN THE GRADES CALCULATION PROJECT
SUMMARY
CHAPTER 10---ARRAYS
WHY ARRAYS
WHAT'S AN ARRAY?
DECLARING AND INITIALIZING AN ARRAY
ADDING DATA TO THE ELEMENTS OF AN ARRAY
THE WONDERS OF ARRAY PROCESSING
USING AN ARRAY FOR AVERAGING
A PROBLEM WITH OUR ARRAY
MULTIPLE DIMENSIONED ARRAYS
CREATING ARRAYS OF OBJECTS
SUMMARY
CHAPTER 11---EXCEPTION HANDLING
COMMON BEGINNER ERRORS
COMPILER ERRORS
Java is Case Sensitive
Public Class---File name Must Be the Same
Forgetting the Semicolon at the End of a Statement
Braces (and Parentheses) Must Occur in Matching Pairs
Forgetting the Left and Right Parentheses for the Condition in an If Structure
Confusing the Equality Operator (==) with the Assignment Operator (=)
Not Initializing a Variable Defined in a Method Before that Variable is Used in the Method Body.
Forgetting to Specify a Return Type for a Method You Write
Forgetting to Specify the Return Value for a Method You Write
“I’ve got to tell you,” Ward said, “I’m pretty impressed with Java’s descriptive compiler messages. Many languages I’ve used aren’t nearly this precise.”
Returning the Wrong Type of Return Value for a Method You Write
Returning a Value from a Method Whose Return Type is Void
Creating an Overloaded Method with What You Believe to Be a Different Signature
RUNTIME ERRORS/LOGIC ERRORS
Referring to an Element Outside the Array Bounds
Forgetting to Call System.exit() in an Application that Displays a Graphical User Interface
Forgetting to Increment a Counter Variable
Forgetting to Add to an Accumulator
Not providing a way for a while structure to end
Forgetting to Code a Break Statement when One is Needed in a Switch Structure
DIVISION BY ZERO
JAVA ERROR HANDLING
JAVA EXCEPTIONS
Ignore the Exception
Handle the Exception with Try-Catch-Finally Blocks
Can We Modify the Grades Calculation Project?
SUMMARY
CHAPTER 12---DEVELOPING A GRAPHICAL USER INTERFACE
BUILDING A GRAPHICAL USER INTERFACE
DESIGNING OUR GUI
CREATING OUR GUI
Setting Up the Top-Level Component: The Frame
Adding Swing Components to the Frame
Create the GUI for the Grades Calculation Project
SUMMARY
CHAPTER 13---EVENT HANDLING IN JAVA
JAVA EVENT HANDLING
WHAT'S AN EVENT?
WHAT'S A LISTENER?
IMPLEMENTING A SIMPLE LISTENER IN YOUR CODE
The ActionListener Interface
Passing Your Listener a Reference to Your GUI Object
Using the WindowListener Interface to Close Your Frame
Using the WindowAdapter Class to Close Your Frame
IMPLEMENTING LISTENERS IN THE GRADES CALCULATION PROJECT
TESTING THE PROGRAM
DELIVERING AND IMPLEMENTING THE GRADES CALCULATION PROJECT IN THE ENGLISH DEPARTMENT
SUMMARY