Java actually
A comprehensive primer in programming
ISBN: 978-1-84480-933-2, Cengage Learning
Table of Contents
| List of Programs | ||
| List of Figures | ||
| List of Tables | ||
| Preface to second version | ||
| PART 1 | Structured Programming | |
| 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 | ||
| Method calls | ||
| Variables | ||
| 1.8 | The Java Virtual Machine | |
| 1.9 | Review questions | |
| 1.10 | Programming exercises | |
| 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 | |
| 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 | Useful Techniques for Building Programs | |
| 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 a CD collection | |
| 9.8 | Sorting and searching using the Java standard library | |
| 9.9 | Review questions | |
| 9.10 | Programming exercises | |
| 10 | Exception handling | |
| 10.1 | What is an exception? | |
| 10.2 | Method execution and exception propagation | |
| Method execution | ||
| Stack trace | ||
| Exception propagation | ||
| 10.3 | Exception handling | |
| try-catch scenario 1: no exception | ||
| try-catch scenario 2: exception handling | ||
| try-catch scenario 3: exception propagation | ||
| 10.4 | Checked exceptions | |
| Dealing with checked exceptions using the throws clause | ||
| Programming with checked exceptions | ||
| 10.5 | Unchecked exceptions | |
| 10.6 | Review questions | |
| 10.7 | Programming exercises | |
| 11 | Text file I/O and simple GUI dialogs | |
| 11.1 | File handling | |
| Data records | ||
| 11.2 | Text files | |
| Writing to text files | ||
| Reading from text files | ||
| 11.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 | ||
| 11.4 | Review questions | |
| 11.5 | Programming exercises | |
| PART 4 | Object-Oriented Programming (OOP) | |
| 12 | Inheritance | |
| 12.1 | Main concepts | |
| Superclasses and subclasses | ||
| Specialization and generalization | ||
| Inheritance in Java | ||
| 12.2 | Extending properties and behaviour | |
| Initializing object state | ||
| Extending behaviour | ||
| 12.3 | Using inherited members | |
| Accessing inherited members in the subclass | ||
| Accessing inherited members using subclass references | ||
| Using superclass references to refer to subclass objects | ||
| Conversion between reference types | ||
| Using superclass and subclass references | ||
| 12.4 | Extending behaviour in the subclass | |
| Overriding instance methods in the subclass | ||
| Using an overridden method from the superclass | ||
| Using a shadowed field | ||
| 12.5 | Final classes and methods | |
| Final classes | ||
| Final methods | ||
| Overloading methods | ||
| 12.6 | Review questions | |
| 12.7 | Programming exercises | |
| 13 | Polymorphism and interfaces | |
| 13.1 | Programming with inheritance | |
| A superclass with multiple subclasses | ||
| Polymorphism and polymorphic references | ||
| Arrays of polymorphic references | ||
| Consequences of polymorphism | ||
| Inheritance is transitive | ||
| 13.2 | Abstract classes | |
| Abstract and concrete classes | ||
| Implementing abstract methods in subclasses | ||
| Abstract classes in the Java standard library | ||
| 13.3 | Aggregation | |
| 13.4 | Interfaces in Java | |
| The concept of contracts in programs | ||
| Declaring an interface | ||
| Implementing an interface | ||
| Implementing multiple interfaces | ||
| Inheritance between interfaces | ||
| Polymorphic use of interface references | ||
| 13.5 | Review questions | |
| 13.6 | Programming exercises | |
| PART 5 | Applying OOP | |
| 14 | Test-driven program development | |
| 14.1 | Developing large programs | |
| 14.2 | Simple testing with assert | |
| The game board | ||
| Game board state | ||
| The state of several cells | ||
| Stacking pieces | ||
| Better error messages when tests fail | ||
| 14.3 | Testing framework | |
| JUnit framework | ||
| Writing test methods | ||
| Running tests with JUnit | ||
| Fixing test failures with JUnit | ||
| Full columns | ||
| 14.4 | Printing the game board | |
| Simulated game play | ||
| Method for printing game board | ||
| 14.5 | Refactoring program code | |
| 14.6 | Interactive Four-in-a-Row game | |
| Functional decomposition | ||
| Dropping the next piece (steps 3a and 3b) | ||
| A simplified end of game test (step 2) | ||
| 14.7 | Ending a game | |
| No winners | ||
| Four pieces in a row | ||
| 14.8 | Machine-controlled player | |
| The interface IPlayer and implementations | ||
| A game between arbitrary players | ||
| 14.9 | Class libraries | |
| Packages and the import statement | ||
| Static import | ||
| Declaring packages | ||
| Desigining frameworks | ||
| 14.10 | Encapsulating implementation detail | |
| Access modifiers for package members | ||
| Access modifiers for class members | ||
| Application programming interface (API) | ||
| Limiting access to fields | ||
| Tell - don't ask | ||
| 14.11 | Base classes | |
| Base classes in the game package | ||
| The package game.terminal | ||
| 14.12 | Compiling and running code in packages | |
| The package game.test | ||
| 14.13 | Review questions | |
| 14.14 | Programming exercises | |
| 15 | Using dynamic data structures | |
| 15.1 | Overview | |
| Abstract data types | ||
| Organizing and manipulating data | ||
| 15.2 | Character sequences | |
| Creating a character sequence | ||
| Modifying the contents of a StringBuilder | ||
| Other classes for handling character sequences | ||
| 15.3 | Introduction to generic types | |
| Declaring generic classes | ||
| Using generic classes | ||
| Generic interfaces | ||
| Handling of generic types at compile time | ||
| 15.4 | Collections | |
| Superinterface | ||
| Traversing a collection using an iterator | ||
| Traversing a collection using a for(:) loop | ||
| Default string representation of a collection | ||
| 15.5 | Lists: ArrayList<E> | |
| Subinterface List<E> | ||
| Using lists | ||
| 15.6 | Sets: HashSet<E> | |
| Interface Set<E> | ||
| Using sets | ||
| 15.7 | Maps. HashMap<K,v> | |
| Hashing | ||
| The Map<K,V> interface | ||
| Map views | ||
| Using maps | ||
| 15.8 | More on generic types | |
| Specifying subtypes: <? extends T> | ||
| Specifying supertypes: <? super T> | ||
| Specifying any type: <?> | ||
| Generic methods | ||
| 15.9 | Sorting and searching methods from the Java API | |
| 15.10 | Review questions | |
| 15.11 | Programming exercises | |
| 16 | Implementing dynamic data structures | |
| 16.1 | Simple linked lists | |
| Manipulating references in a linked list | ||
| Operations on linked lists | ||
| Inserting at the head of a linked list | ||
| Inserting at the tail of a linked list | ||
| Removing from the head of a linked list | ||
| Removing from the tail of a linked list | ||
| Removing a node from inside a linked list | ||
| Iterator for a linked list | ||
| Converting to an array | ||
| Remarks on linked lists | ||
| 16.2 | Stacks and queues | |
| Implementing a stack | ||
| Using stacks | ||
| Queues | ||
| Using queues | ||
| 16.3 | Review questions | |
| 16.4 | Programming exercises | |
| 17 | Recursion | |
| 17.1 | Recursion and iteration | |
| Factorials: an example from mathematics | ||
| Using recursive method calls | ||
| 17.2 | Designing recursive algorithms | |
| 17.3 | Infinite recursion | |
| 17.4 | Recursive binary search | |
| 17.5 | Towers of Hanoi | |
| Recursive solution | ||
| Iterative solution | ||
| 17.6 | Quicksort: a recursive sorting algorithm | |
| 17.7 | Fibonacci series: an example of iterative solution | |
| 17.8 | Review questions | |
| 17.9 | Programming exercises | |
| 18 | More on exception handling | |
| 18.1 | Exception classes | |
| The Exception class | ||
| The RuntimeException class | ||
| The Error class | ||
| 18.2 | Throwing an exception programmatically | |
| 18.3 | Handling several types of exceptions | |
| Typical programming errors in exception handling | ||
| 18.4 | Defining new exceptions | |
| 18.5 | Using the finally block | |
| 18.6 | Review questions | |
| 18.7 | Programming exercises | |
| 19 | Files and streams | |
| 19.1 | Streams | |
| Overview of byte streams | ||
| Overview of character streams | ||
| 19.2 | Terminal window I/O | |
| Writing to the terminal window | ||
| Reading from the keyboard | ||
| 19.3 | Binary files | |
| File path | ||
| Writing to binary files | ||
| Reading binary values | ||
| 19.4 | Object serialization | |
| Writing objects | ||
| Reading objects | ||
| Effective storing of objects | ||
| 19.5 | Random access files | |
| Overview of the class RandomAccessFile | ||
| Setting up random access | ||
| The file pointer | ||
| Reading and writing random access files | ||
| 19.6 | Review questions | |
| 19.7 | Programming exercises | |
| 20 | Graphical user intefaces | |
| 20.1 | Building GUIs | |
| Designing GUI-based dialogs | ||
| Delegating events | ||
| GUI building packages | ||
| Basics of GUI development | ||
| 20.2 | Components and containers | |
| Components | ||
| Containers | ||
| Windows and frames | ||
| Panels | ||
| Steps in developing a GUI | ||
| GUI and the main() method | ||
| 20.3 | GUI control components | |
| Text fields | ||
| Buttons | ||
| GUI design with panels | ||
| Using different types of buttons | ||
| 20.4 | Designing layout | |
| FlowLayout | ||
| BorderLayout | ||
| GridLayout | ||
| 20.5 | Event-driven programming | |
| Events | ||
| Event delegation model | ||
| Classes ActionEvent and WindowEvent | ||
| Programming paradigms for event handling | ||
| 20.6 | Dialog boxes | |
| Application using dialog boxes | ||
| Implementing a dialog box | ||
| 20.7 | Anonymous classes as listeners | |
| Creating and registering anonymous listener objects | ||
| Using anonymous listener objects | ||
| Some remarks on anonymous classes | ||
| 20.8 | Programming model for GUI-based applications | |
| 20.9 | GUI for the Four-in-a-Row game | |
| 20.10 | Review questions | |
| 20.11 | Programming exercises | |
| PART 6 | 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 | Exception handling | |
| A.11 | Text file I/O and simple GUI dialogs | |
| A.12 | Inheritance | |
| A.13 | Polymorphism and interfaces | |
| A.14 | Test-driven program development | |
| A.15 | Using dynamic data structures | |
| A.16 | Implementing dynamic data structures | |
| A.17 | Recursion | |
| A.18 | More on exception handling | |
| A.19 | Files and streams | |
| A.20 | Graphical user interfaces | |
| 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
