Java actually
A first course in programming
ISBN: 978-1-844480-418-4
Table of Contents
List of programs | ||
List of figures | ||
List of tables | ||
Preface | ||
1 | Getting started | |
1.1 | Programming | |
1.2 | Editing source code | |
Source code file naming rules | ||
1.3 | Development tools for Java | |
1.4 | Compiling Java programs | |
1.5 | Running Java programs | |
1.6 | The components of a program | |
Operations | ||
Programming with objects | ||
Object-based programming | ||
1.7 | The Java programming language | |
Classes and methods | ||
Comments and indentation | ||
Program entry point | ||
Variables | ||
1.8 | The Java Virtual Machine | |
1.9 | Review questions | |
1.10 | Programming exercises | |
PART 1 | Structured Programming | |
2 | Basic programming elements | |
2.1 | Printing to the terminal window | |
The print() and println() methods | ||
Creating program output using strings | ||
2.2 | Local variables | |
Declaring variables | ||
Assigning variables | ||
Logical errors | ||
Literals and constants | ||
Choosing names | ||
2.3 | Numerical data types | |
Primitive data type int | ||
Primitive data type double | ||
2.4 | Arithmetic expressions and operators | |
Arithmetic expression evaluation rules | ||
Conversion between primitive data types | ||
Precedence and associativity rules | ||
Integer and floating-point division | ||
2.5 | Formatted output | |
Format string | ||
Sample format specifications | ||
Printing with fixed field widths | ||
2.6 | Reading 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.7 | Review questions | |
2.8 | Programming exercises | |
3 | Program control flow | |
3.1 | Boolean 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.2 | Control 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.3 | Control 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.4 | Assertions | |
Making assertions | ||
Assertions as a testing technique | ||
3.5 | Review questions | |
3.6 | Programming exercises | |
PART 2 | Object-based Programming | |
4 | Using objects | |
4.1 | Introduction to the object model | |
Abstractions, classes and objects | ||
Objects, reference values and reference variables | ||
The new operator | ||
Using objects | ||
Object state | ||
4.2 | Strings | |
Characters and strings | ||
String concatenation | ||
Creating string objects | ||
String comparison | ||
Converting primitive values to strings | ||
Other useful methods for strings | ||
4.3 | Manipulating references | |
Reference types and variables | ||
Assignment to reference variables | ||
Aliases: one object, several references | ||
The null literal | ||
Comparing objects | ||
4.4 | Primitive values as objects | |
Auto-boxing | ||
Auto-unboxing | ||
Explicit boxing and unboxing | ||
Useful methods in the wrapper classes | ||
4.5 | Review questions | |
4.6 | Programming exercises | |
5 | More on control structures | |
5.1 | The extended assignment operators | |
5.2 | The increment and decrement operators | |
5.3 | Counter-controlled loops | |
Local variables in the for(;;) loop | ||
Other increments in for(;;) loops | ||
Counting backwards with for(;;) loops | ||
Nested for(;;) loops | ||
5.4 | Changing control flow in loops | |
The break statement | ||
The continue statement | ||
5.5 | Common pitfalls in loops | |
Infinite loop: for(;;) | ||
One-off errors | ||
Errors in initialization | ||
Errors in the loop condition | ||
Optimizing loops | ||
5.6 | Multiple-selection statements | |
The default label | ||
The case label values | ||
Falling through case labels | ||
5.7 | Review questions | |
5.8 | Programming exercises | |
6 | Arrays | |
6.1 | Arrays as data structures | |
6.2 | Creating 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.3 | Initializing arrays | |
6.4 | Iterating 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.5 | Multidimensional 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.6 | More on iteration: enhanced for loop | |
Iterating over multidimensional arrays with the for(:) loop | ||
6.7 | More miscellaneous operations on arrays | |
Copying arrays | ||
Comparing arrays | ||
Working with partially-filled arrays | ||
6.8 | Pseudo-random number generator | |
The Random class | ||
Determining the range | ||
Simulating a dice roll | ||
Generating the same sequence of pseudo-random numbers | ||
6.9 | Review questions | |
6.10 | Programming exercises | |
7 | Defining classes | |
7.1 | Class members | |
7.2 | Defining object properties using field variables | |
Field declarations | ||
Initializing fields | ||
7.3 | Defining 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.4 | Static members of a class | |
Accessing static members | ||
Initializing static variables | ||
The main() method and program arguments | ||
7.5 | Initializing object state | |
Default constructors: implicit or explicit | ||
Constructors with parameters | ||
Overloading constructors | ||
7.6 | Enumerated types | |
Simple form of enumerated types | ||
Selected methods for enumerated types | ||
General form of enumerated types | ||
Declaring enumerated types inside a class | ||
7.7 | Review questions | |
7.8 | Programming exercises | |
8 | Object communication | |
8.1 | Responsibilities 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.2 | Communication 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.3 | Relationships between objects | |
Fields with reference values | ||
Objects that communicate | ||
Object ownership | ||
One-to-one and one-to-many associations | ||
Suggestions for further development | ||
8.4 | Method overloading | |
8.5 | Documenting source code | |
Multi-line comments | ||
Documenting classes and members | ||
Hiding internal methods and fields | ||
A fully-documented Java class | ||
How to document programs | ||
8.6 | Review questions | |
8.7 | Programming exercises | |
PART 3 | Program Building Blocks | |
9 | Sorting and searching arrays | |
9.1 | Natural order | |
Relational operators for primitive data types | ||
Understanding relational operators | ||
9.2 | Selection 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.3 | Insertion 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.4 | Sorting arrays of objects | |
Comparing objects: the Comparable interface | ||
Implementing the natural order for time objects | ||
Sorting arrays of comparable objects | ||
9.5 | Linear search | |
9.6 | Binary search | |
9.7 | Sorting and searching using the Java standard library | |
9.8 | Review questions | |
9.9 | Programming exercises | |
10 | Text file I/O and simple GUI dialogs | |
10.1 | File handling | |
Data records | ||
10.2 | Text files | |
Writing to text files | ||
Basic exception handling | ||
Reading from text files | ||
10.3 | Simple 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.4 | Review questions | |
10.5 | Programming exercises | |
PART 4 | Appendices | |
A | Answers to review questions | |
A.1 | Getting started | |
A.2 | Basic programming elements | |
A.3 | Program control flow | |
A.4 | Using objects | |
A.5 | More on control structures | |
A.6 | Arrays | |
A.7 | Defining classes | |
A.8 | Object communication | |
A.9 | Sorting and searching arrays | |
A.10 | Text file I/O and simple GUI dialogs | |
B | Language reference | |
B.1 | Reserved words | |
B.2 | Operators | |
B.3 | Primitive data types | |
B.4 | Java modifiers | |
C | Formatted values | |
C.1 | Syntax for format strings | |
C.2 | Conversion codes and types | |
C.3 | Examples | |
D | The Unicode character set | |
D.1 | Excerpt from the Unicode character set | |
D.2 | Lexicographical order and alphabetical order | |
E | Console I/O and simple GUI dialog boxes | |
E.1 | Support for console I/O | |
E.2 | Support for simple GUI dialog boxes | |
F | Numeral systems and representation | |
F.1 | Numeral systems | |
Decimal numeral system | ||
Binary numeral system | ||
Octal numeral system | ||
Hexadecimal numeral system | ||
F.2 | Conversions between numeral systems | |
F.3 | Conversions from decimal numeral system | |
F.4 | Integer representation | |
F.5 | String representation of integers | |
G | Programming tools in the JDK | |
G.1 | Programming tools | |
G.2 | Commands | |
Compiling source code: javac | ||
Running the program: java | ||
Generating documentation: javadoc | ||
G.3 | Configuring the CLASSPATH | |
H | Introduction to UML | |
H.1 | Class diagram | |
H.2 | Object diagrams | |
H.3 | Sequence diagrams | |
H.4 | Activity diagrams | |
Index |
Khalid A. Mughal
Torill Hamre
Rolf W. Rasmussen