Programming with Java - An Algorithmic Introduction

(Copyright Pat Terry, Rhodes University, 2009)

This text is under active development. Please do not copy or distribute it without my permission!

Mistakes and corrections to the text will be posted here in due course. Please help - if you find mistakes in the text, send an e-mail to the author (p.terry@ru.ac.za   )


1    Introduction     (also available in a PDF version)   


2    Simple algorithm design     (also available in a PDF version)   

2.1    Sequential algorithms
2.2    Properties of data
2.3    A simple data processing algorithm
2.4    Basic building blocks of algorithms
2.5    Some suggestions for designing repetitive algorithms
2.6    Stepwise refinement
2.7    Interlude - towards computer coding
2.8    The issues of scale
2.9    The benefits of abstraction
2.10   The problems of syntax


3    Simple Java programs     (also available in a PDF version)   

3.1    A traditional challenge: Hello, world
3.2    Methods and classes
3.3    "Hello, world" in Java
3.4    Compiling and executing the "Hello, world" program
3.5    Variables, identifiers and data types
3.6    A second example: Holding a simple conversation
3.7    Variations on a theme
3.8    What really lies behind reading and writing values?
3.9    Redirecting input and output from and to disk files
3.10   String literals
3.11   A variation that introduces a second method
3.12   Commentary


4    Making decisions     (also available in a PDF version)   

4.1    Taking simple decisions - the "if" statement
4.2    Wider choices - nested if statements
4.3    The problem of the "dangling else"
4.4    Even wider choices - the "switch - case" statement


5    Expressions, assignments and data types     (also available in a PDF version)   

5.1    The concept of an expression
5.2    Simple assignment statements
5.3    Data types, variable declarations and variable initialization
5.4    Properties of the main primitive types
5.5    The String type
5.6    More about expressions
5.7    Displaying results neatly - formatted output
5.8    Some useful methods that can be called when forming numerical expressions
5.9    Some other interesting operators in Java


6    Simple repetition - loops     (also available in a PDF version)   

6.1    Definite loops
6.2    Indefinite loops


7    Structured programs - decomposition into methods     (also available in a PDF version)   

7.1    Numerical methods
7.2    Boolean methods
7.3    void methods
7.4    Scope rules - parameters and local variables


8    Structured programs - more about methods     (also available in a PDF version)   

8.1    Parameter passing in Java is "by value"
8.2    Methods that perform input and output
8.3    Parameterless methods
8.4    Correspondence between formal parameters and arguments
8.5    Method overloading
8.6    Scope rules - globally accessible field variables
8.7    Checking on the validity of arguments
8.8    Developing a library of methods
8.9   Recursion, and recursive methods


9    Stepwise refinement     (also available in a PDF version)   

9.1    Some interesting perfect squares
9.2    Obtaining a list of prime numbers
9.3    The Rock-Paper-Scissors game (Ching Chong Cha)
9.4    A simple game with a pile of matches


10    Object-oriented programming - classes and objects     (also available in a PDF version)   

10.1    A class may be used to provide a template for creating an object
10.2    Objects are not accessed "directly" , but "by reference"
10.3    It is easy to confuse references and objects
10.4    Encapsulation - classes may define methods in addition to declaring fields
10.5    Public and private access to fields and methods - information hiding
10.6    Package ("friendly") access to fields and methods
10.7    Packages in practice - the "classpath"
10.8    Object-oriented analysis and design
10.9    A note of caution


11    Object-oriented programming - more about developing classes     (also available in a PDF version)   

11.1    Constructors
11.2    Method and constructor overloading
11.3    Mutable and immutable classes
11.4    Classes may have both instance members and class members.
11.5    Scope rules
11.6    The toString() method
11.7    References to objects may be passed as arguments to methods
11.8    References to objects may be returned from methods
11.9    Other uses for the this reference
11.10   Making a deep copy of an object
11.11   Comparison of objects - the equals() method
11.12   Composition and aggregation
11.13   Inner classes
11.14   Defensive programming - errors and exceptions
11.15   Information hiding
11.16   UML - Unified Modelling Language


12    Useful standard classes - string handling     (also available in a PDF version)   

12.1    The String class
12.2    String concatenation
12.3    Methods that construct strings from other strings
12.4    The String.format() method
12.5    Strings can be indexed
12.6    Input of strings
12.7    Efficient string manipulation - the StringBuffer and StringBuilder classes
12.8    The StringTokenizer class


13    Wrapper classes and enumerated types     (also available in a PDF version)   

13.1    The Integer class
13.2    The Double class
13.3    The Boolean class
13.4    The Character class
13.5    Enumerated types


14    One-dimensional arrays     (also available in a PDF version)   

14.1    Array structures
14.2    Manipulating array elements
14.3    Arrays as parameters and as returned values from methods
14.4    Comparing and copying arrays
14.5    Arrays of strings
14.6    Command line arguments
14.7    Arrays of characters
14.8    Arrays of other objects
14.9    Variable length argument lists
14.10   Some other array operations


15    Multi-dimensional arrays     (also available in a PDF version)   

15.1    Two-dimensional arrays
15.2    Two-dimensional array initializers
15.3    Multi-dimensional arrays


16    Simple list handling     (also available in a PDF version)   

16.1    Partially filled arrays
16.2    Processing all the items in a list
16.3    Self-expanding lists
16.4    Ordered lists
16.5    Simple sorting algorithms
16.6    Searching algorithms
16.7    Scrambling lists of data
16.8    Dealing with completely filled arrays
16.9    Lists of objects
16.10   Lists of arrays - permutations
16.11   The ArrayList class
16.12   Objects are really references
16.13   Using the ArrayList class with primitive types


17    Text file handling classes     (also available in a PDF version)   

17.1    The File class
17.2    The InFile class
17.3    The OutFile class
17.4    Opening and closing files
17.5    Error reporting
17.6    Examples
17.7    The Scanner class


18    Reusing and extending classes     (also available in a PDF version)   

18.1    Everything is an object
18.2    The "has-a" relationship
18.3    The interface concept
18.4    Composition and aggregation
18.5    The "is-a" relationship
18.6    Simple inheritance
18.7    Everything is also an Object
18.8    Constructors and constructor chaining
18.9    Method overriding, and method overloading
18.10   Overriding and overloading are easily confused


19    Class hierarchies and design     (also available in a PDF version)   

19.1    Establishing a sensible hierarchy
19.2    Abstract classes
19.3    Inheritance or Composition?
19.4    Protected access and packages
19.5    Adult behaviour
19.6    The Object class
19.7    Inheritance - pitfalls and pointers
19.8    The final word


20    Polymorphism and the use of abstract classes     (also available in a PDF version)   

20.1    Late binding - when there seems to be a choice, which method gets called?
20.2    Examples of polymorphism used to good effect
20.3    Abstract classes and polymorphism
20.4    What if there were no such thing as polymorphism?


21    Polymorphism and the use of interfaces and generics     (also available in a PDF version)   

21.1    The concept of the interface to a class
21.2    Objects do not have to inherit from one another to have features in common
21.3    The interface type
21.4    Abstract classes and interfaces compared
21.5    Why do we need interfaces, and how do we use them to advantage?
21.6    Interfaces used to simulate multiple inheritance
21.7    Interfaces used to exploit other commonality
21.8    Interfaces as parameter types, and as return types for methods
21.9    Lists of comparable objects
21.10   Typed interfaces and generic methods
21.11   Summary - some design issues


22    Exception handling     (also available in a PDF version)   

22.1    Exceptions and exception handlers
22.2    Exception classes
22.3    User-defined exception handlers
22.4    Try-catch statements within loops - recovering from errors
22.5    Checked and unchecked exceptions
22.6    Throwing your own exceptions and deriving your own exception classes
22.7    The finally block
22.8    Returning from within a finally block
22.9    Alternative approaches to handling runtime errors


23    Sequential file handling     (also available in a PDF version)   

23.1    Binary files, text files, buffers and streams
23.2    The File class again
23.3    Files as streams of bytes
23.4    Binary files - primitive values and strings
23.5    Binary files - primitive values, strings and objects
23.6    Readers and writers - character handling and text files
23.7    Files as streams of characters
23.8    The PrintWriter class
23.9    The Scanner class again

Chapter 24 will be on GUI design, but is not yet available.


Appendix 1    Recommended further reading     (also available in a PDF version)   

Appendix 2    Java reserved words

Appendix 3    The ASCII character set

Appendix 4    Escape sequences for character specification

Appendix 5    A screen handling library

Appendix 6    A rudimentary GUI for simple I/O


An online electronic copy of this book is available at http://www.scifac.ru.ac.za/javabook   

The online version will be corrected as and when errors are detected!

Please help - if you find mistakes in the text, send an e-mail to the author (p.terry@ru.ac.za   )

Please do not make copies of the electronic version and distribute them - we need to get the errors corrected first!


Home  © P.D. Terry