Table of Contents

¨
List of programs
List of figures
List of tables
Preface
1Getting started
1.1Programming
1.2Editing source code
Source code file naming rules
1.3Development tools for Java
1.4Compiling Java programs
1.5Running Java programs
1.6The components of a program
Operations
Programming with objects
Object-based programming
1.7The Java programming language
Classes and methods
Comments and indentation
Program entry point
Variables
1.8The Java Virtual Machine
1.9Review questions
1.10Programming exercises
PART 1Structured Programming
2Basic programming elements
2.1Printing to the terminal window
The print() and println() methods
Creating program output using strings
2.2Local variables
Declaring variables
Assigning variables
Logical errors
Literals and constants
Choosing names
2.3Numerical data types
Primitive data type int
Primitive data type double
2.4Arithmetic expressions and operators
Arithmetic expression evaluation rules
Conversion between primitive data types
Precedence and associativity rules
Integer and floating-point division
2.5Formatted output
Format string
Sample format specifications
Printing with fixed field widths
2.6Reading numbers from the keyboard
The Scanner class
Reading integers
Reading floating-point numbers
Error handling
Reading multiple values per line
Skipping the rest of the line when reading values from the keyboard
2.7Review questions
2.8Programming exercises
3Program control flow
3.1Boolean expressions
Boolean primitive data type
Relational operators
Understanding relational operators
Logical operators
Short-circuit evaluation
De Morgan’s laws
Using Boolean expressions to control flow of execution
3.2Control flow: selection statements
Simple selection statement: if
Blocks of statements: { ... }
Local variables in a block
Selection statement with two choices: if-else
Nested selection statement
3.3Control flow: loops
Loop test before loop body: while
Loop test after loop body: do-while
Infinite loops
Using loops to implement user dialogue
Choosing the right loop
Nested loops
3.4Assertions
Making assertions
Assertions as a testing technique
3.5Review questions
3.6Programming exercises
PART 2Object-based Programming
4Using objects
4.1Introduction to the object model
Abstractions, classes and objects
Objects, reference values and reference variables
The new operator
Using objects
Object state
4.2Strings
Characters and strings
String concatenation
Creating string objects
String comparison
Converting primitive values to strings
Other useful methods for strings
4.3Manipulating references
Reference types and variables
Assignment to reference variables
Aliases: one object, several references
The null literal
Comparing objects
4.4Primitive values as objects
Auto-boxing
Auto-unboxing
Explicit boxing and unboxing
Useful methods in the wrapper classes
4.5Review questions
4.6Programming exercises
5More on control structures
5.1The extended assignment operators
5.2The increment and decrement operators
5.3Counter-controlled loops
Local variables in the for(;;) loop
Other increments in for(;;) loops
Counting backwards with for(;;) loops
Nested for(;;) loops
5.4Changing control flow in loops
The break statement
The continue statement
5.5Common pitfalls in loops
Infinite loop: for(;;)
One-off errors
Errors in initialization
Errors in the loop condition
Optimizing loops
5.6Multiple-selection statements
The default label
The case label values
Falling through case labels
5.7Review questions
5.8Programming exercises
6Arrays
6.1Arrays as data structures
6.2Creating and using arrays
Declaring array reference variables
Creating arrays
Default initialization
Arrays of objects
The length field
Accessing an array element
Array bounds
Array aliases
Alternate notation for array declaration
6.3Initializing arrays
6.4Iterating over an array
Comparing all values in an array with a given value
Finding the lowest value in an array
Finding the highest value in an array
Adding the values in an array
Iterating over an array of objects
6.5Multidimensional arrays
Creation and initialization of multidimensional arrays
Printing a two-dimensional array in tabular form
Iterating over a specific row in a two-dimensional array
Iterating over a specific column in a two-dimensional array
Iterating over all the columns in a two-dimensional array
Ragged arrays
Arrays with more than two dimensions
6.6More on iteration: enhanced for loop
Iterating over multidimensional arrays with the for(:) loop
6.7More miscellaneous operations on arrays
Copying arrays
Comparing arrays
Working with partially-filled arrays
6.8Pseudo-random number generator
The Random class
Determining the range
Simulating a dice roll
Generating the same sequence of pseudo-random numbers
6.9Review questions
6.10Programming exercises
7Defining classes
7.1Class members
7.2Defining object properties using field variables
Field declarations
Initializing fields
7.3Defining behaviour using instance methods
Method declaration and formal parameters
Method calls and actual parameter expressions
Parameter passing: call-by-value
The current object: this
Method execution and the return statement
Passing information using arrays
Automatic garbage collection
7.4Static members of a class
Accessing static members
Initializing static variables
The main() method and program arguments
7.5Initializing object state
Default constructors: implicit or explicit
Constructors with parameters
Overloading constructors
7.6Enumerated types
Simple form of enumerated types
Selected methods for enumerated types
General form of enumerated types
Declaring enumerated types inside a class
7.7Review questions
7.8Programming exercises
8Object communication
8.1Responsibilities and roles
A naive solution to a given problem
Combining properties and behaviour in classes
Using references as parameters
Advantages of good abstractions
Rethinking responsibility
8.2Communication and cooperation
Dividing and assigning responsibility
Clarifying the responsibilities of classes in the program
Communication between objects at runtime
Assigning the responsibility for calculating salaries
8.3Relationships between objects
Fields with reference values
Objects that communicate
Object ownership
One-to-one and one-to-many associations
Suggestions for further development
8.4Method overloading
8.5Documenting source code
Multi-line comments
Documenting classes and members
Hiding internal methods and fields
A fully-documented Java class
How to document programs
8.6Review questions
8.7Programming exercises
PART 3Program Building Blocks
9Sorting and searching arrays
9.1Natural order
Relational operators for primitive data types
Understanding relational operators
9.2Selection sort
Sorting an array of integers
Pseudocode for the selection sort
Coding the selection sort
Analysing the amount of work required by a selection sort
9.3Insertion sort
Sorting an array of integers using an insertion sort
Pseudocode for the insertion sort
Coding the insertion sort
Analysing the amount of work required by an insertion sort
9.4Sorting arrays of objects
Comparing objects: the Comparable interface
Implementing the natural order for time objects
Sorting arrays of comparable objects
9.5Linear search
9.6Binary search
9.7Sorting and searching using the Java standard library
9.8Review questions
9.9Programming exercises
10Text file I/O and simple GUI dialogs
10.1File handling
Data records
10.2Text files
Writing to text files
Basic exception handling
Reading from text files
10.3Simple GUI dialogue design
Overview of the JOptionPane class
Message dialogs – presenting information to the user
Input dialogs – reading data from the user
Confirmation dialogs – getting confirmation from the user
10.4Review questions
10.5Programming exercises
PART 4Appendices
AAnswers to review questions
A.1Getting started
A.2Basic programming elements
A.3Program control flow
A.4Using objects
A.5More on control structures
A.6Arrays
A.7Defining classes
A.8Object communication
A.9Sorting and searching arrays
A.10Text file I/O and simple GUI dialogs
BLanguage reference
B.1Reserved words
B.2Operators
B.3Primitive data types
B.4Java modifiers
CFormatted values
C.1Syntax for format strings
C.2Conversion codes and types
C.3Examples
DThe Unicode character set
D.1Excerpt from the Unicode character set
D.2Lexicographical order and alphabetical order
EConsole I/O and simple GUI dialog boxes
E.1Support for console I/O
E.2Support for simple GUI dialog boxes
FNumeral systems and representation
F.1Numeral systems
Decimal numeral system
Binary numeral system
Octal numeral system
Hexadecimal numeral system
F.2Conversions between numeral systems
F.3Conversions from decimal numeral system
F.4Integer representation
F.5String representation of integers
GProgramming tools in the JDK
G.1Programming tools
G.2Commands
Compiling source code: javac
Running the program: java
Generating documentation: javadoc
G.3Configuring the CLASSPATH
HIntroduction to UML
H.1Class diagram
H.2Object diagrams
H.3Sequence diagrams
H.4Activity diagrams
Index

Khalid A. Mughal
Torill Hamre
Rolf W. Rasmussen