Preface to the second edition

Java Gently is a first programming text. It aims to teach students, as well as those fascinated by the possibilities of their desktop computers, how to program, and how to do it in the best possible style in the Java programming language. In the process, Java Gently covers all of the Java 1.1 language, most of its core libraries and utilities and the prospects it offers for the future. In terms of programming, it covers object-orientation, software design, structured programming, graphical user interfacing, event-driven programming, networking and an introduction to data structures.

A philosophy of teaching Java

When this book began in 1996, the way to teach Java as a first programming language was still uncharted. My experience since then in teaching Java courses many times, and in the overwhelmingly favourable reaction to the first edition, has reinforced my belief that teaching Java requires a different model to previous languages. Java is different from its predecessor first languages (Pascal, C++, Ada) in the following ways:

1. It is small, yet object-oriented.

2. It has no built-in data structure types other than the array.

3. It provides for full graphical user interface (GUI) and multimedia (sound, image and animation) facilities, and promotes GUI programs as applets associated with web browsers.

4. It provides built-in multithreading features and facilitates programming on the network in a variety of ways.

Given this list there seem to be two possible approaches to presenting Java as a first teaching language. The first is immediately attractive: start at point 3; go straight into GUI, multimedia, applets and the whole network environment, thereby catching the students’ interest and showing them how modern and different Java really is. The second approach is to start at the beginning, laying a sound foundation of basic concepts, and then moving on to the newer aspects.

Java Gently does some of both, as they both have advantages. Starting with applets and GUI is exciting, but requires so much underlying knowledge of objects that even if students learn the outer shell of programs parrot-fashion, they may never fully understand what they are doing. On the other hand, proceeding at a steady pace through all the background first is a sound policy, but can be both boring and overwhelming.

Java Gently’s approach

The approach I have devised is a middle ground, which hinges on Java’s two main selling points: it is network-enabled but it is small. So we start off with programs that show how the web is used, but then move straight into covering the basic concepts. Because Java has a smaller number of constructs than Pascal or C++, we can spend less time here, and therefore can get back to the GUI and network aspects sooner, certainly within the first half of a course. Then applets, multimedia, graphics and so on can be introduced properly, to students who have already encountered classes, objects, instantiation and inheritance, and know how to handle them.

Approach is not everything, however, and Java Gently uses three other tried and tested teaching techniques:

diagrams everywhere,

explanation by example,

self-check quizzes and problems after every chapter.

There are three kinds of diagrams used throughout the book:

1. algorithm diagrams, which show the flow of small programs, and also serve to illustrate new concepts such as exceptions and multi-threading;

2. class diagrams, which show the structure of the program at the class level, while still revealing the basic components such as methods and variables and the interaction between them;

3. form diagrams, which show the syntax of each new Java construct in a way that mirrors its usage in practice.

There are over 80 complete fully worked examples, together with test data and output. Each example has been carefully chosen so that it both illustrates the feature that has just been introduced, and also solves a real problem in the best possible Java style. Most of the examples have a ‘real world’ flavour, which reinforces the need for a programmer to understand the user’s problem, and to write programs in a user-friendly way. As soon as a new feature is introduced, it becomes part of the repertoire of the programmer, and will re-emerge in subsequent examples when needed. As the book progresses, the examples get longer and more challenging.

The book subscribes to the belief that Java is, and will be for many years in the future, an excellent first teaching language, and that it is in a different class from its competitors, C++, Pascal and Ada. For that reason, we teach Java properly, with the web, objects and exceptions coming in right at the beginning.

Teaching programming vs teaching Java

For a first-year course, it is no longer sufficient to just teach a language. The expectation of today’s computer users is that the whole computing milieu will assist in the solution of day-to-day problems. In addition to explaining syntax and the construction of a well-formed program, a programming textbook for the next century has to include techniques for problem solving.

Java Gently is indeed such a modern textbook. Each worked problem – and there are over 80 of them – is introduced with a typically inexact statement. This is then refined in the process of devising a solution. Where applicable, the appropriate technique is selected from those previously discussed, and then we proceed to algorithm development. As an additional aid, algorithms are illustrated with structured diagrams, and important techniques are discussed, highlighted and identified for reuse later on. The important programming paradigms of:

structured programming,

object-oriented programming,

class design,

abstraction,

software reuse, and

generality

all receive attention, and examples are carefully chosen to show how these techniques can encourage correctness and efficiency. For example, there are discussions on guidelines for class design, on the relationship between arrays and classes, on the interplay between loops and exceptions, and on the different ways of implementing data structures, to mention just a few.

Order of topics

The topics are grouped into chapters to enable an easy introduction to programming right at the beginning. The order of statements as covered is: output, assignment, for, input, if, while, do, switch. This grouping does not follow that of the Java reference manual, nor of a book intended for someone who already knows programming. It is a pedagogically tried and tested order, which gets the student through the fundamentals of Java, gently, but without undue delay and fuss.

Most of the chapters contain an even mix of program, control and data structure issues, plus some Java specialties. My experience has shown that this integrated approach to the order of topics has tremendous benefits for motivation and understanding, and that students are able to get ahead and accomplish more in a shorter time, without compromising their assimilation of the principles of Java and programming. The key points of the order of all the material are:

rapid entry to genuine programming problems;

the power of object-oriented programming presented with realistic examples from the start;

exceptions introduced early on and explained as a natural way to control flow between classes;

‘no fuss’ discussion of features that are simple in Java (such as class structure) but in-depth treatment of potentially difficult topics (such as casting and cloning);

hash tables grouped with arrays to show their efficacy in providing for non-integer indices;

a custom-made input package, which is used from Chapter 4, and explained in full in Chapter 7 once strings tokenizers have been covered;

introduction to object references and their implications through the development of a linked list class;

almost full coverage of the awt (Abstract Windowing Toolkit) facilities, but geared towards examples;

applets presented through a viewer and a browser, and the differences between applets and applications explained once the student can genuinely appreciate the issues;

multi-threading done before socket programming, so that the socket programs can illustrate multiple client–server operations.

Full coverage

In terms of full coverage, Java Gently ensures that the reader is presented with all of the language. Applets and graphics are covered, as are threads, networking, database connectivity and remote objects. Ample use is made of images and colour, in the modern idiom.

Java Gently aims to excite students and to keep them interested in programming, so the Web aspects of Java are covered, but they are not allowed to overshadow the primary purpose of the book, which is to teach good principles of programming. So much of programming-in-the-web is knowing the correct library method to call, and there is a great deal of repetitive setting of fonts and so on, the details of which can be easily left to a second course.

Because object-oriented programming lends itself to presenting data structures, Part II has a thorough treatment of classical data structures, with mention of sorting and searching algorithms. There is discussion of the standard Java data structures as well as several written especially for the book. Java Gently gives its readers a direct entry into a second course syllabus.

What’s new in the second edition

Most importantly Java Gently second edition has been thoroughly modernised to take full advantage of the new facilities in Java 1.1. Specifically, these include:

the new event model of multiple events, listeners and event handlers;

internationalisation of formatting for dates, currencies and numbers;

inner classes;

Java Database Connectivity for accessing databases from Java;

Java Remove Invocation for linking up programs on different machines.

One might wonder whether these new topics are applicable for a first programming text. My view is that students stumble on them anyway, and the role of Java Gently is to present them in a manner which is easy to understand and assimilate, and yet is correct. They are also "fun" topics, as the examples in Chapter 7 and 14 show. In keeping with the book’s approach, the new topics are not introduced in isolation, but are carried through in later examples as they fit in.

In addition, there are new sections throughout the book, some based on suggestions from adopters. Particular attention has been paid to adding more diagrams to explain the structure and semantics of the language.

how to acquire the Java Development Kit and get Java up at home;

where to find assistance on Java, including more prominent mention of the web site;

parameter passing for objects;

package creation and accessing (several utilities such as opening a file, sorting and the list class are added to a "myutilities" package as the book progresses);

protection modifiers, what do they mean and how does inheritance affect things;

greater emphasis on the common Java libraries, and careful assistance for learners to find their way around them easily; (specifically java.text, java.util and java.awt)

more on graphics and how to draw a graph.

Finally, the usual additions for a second edition include in this case:

The programming presentation has been standardised, with comments everywhere.

more emphasis on screen dumps, with a special colour section to show the full effect of Java in action on the web.

over 30 new examples and case studies, bringing the total to over 80,

many more exercises, accounting for 120 altogether;

collected synatx forms for easier access;

Coping with Java developemnt in the future

The Java community is in the fortunate position of entering a period of relative stability. In assessing Sun’s proposals for Java 1.2 as of January 1998, it would seem that there are minimal language changes now. Specifically, input methods (which the Java Gently Text class addresses) will be added, as well as collections and weak references (one is not sure whether these are a good idea!). Some changes are envisaged to the RMI, but the remainder of the enhancements in Java 1.2 affect performance and the APIs not directly covered by this books, such as Java Beans and Java Foundation Classes. Java 1.2 is due only in mid-1998,a nd will take at least six months to mature, if past experience is anything to go by. Moreover, the browsers will have another task of convering, and this may take up to an year.

The net result is that we can now relax and work with a language (Java 1.1) which covers all needs, is stable and supported, and of course, is the basis for Java Gently 2nd edition.

Teaching aids and the web site

Java Gently contains:

over 80 fully worked examples and case studies;

summaries after each chapter;

ten-point quizzes at the end of each chapter, with solutions;

over 100 exercises;

an active web site;

special Text class for augmenting Java’s input from the keyboard;

notations for algorithms and classes resulting in over 70 diagrams;

a unique way of presenting Java syntax to the novice programmer.

All examples have been tested and run and are available on a web site which will be actively maintained and updated. The web site at

www.cs.up.ac.za/javagently

also contains:

 

web pages which introduce the book,

all the examples for downloading, either individually, in chapters or in one go;

frequently asked questions;

error list;

discussion board;

future plans for the book;

e-mail contact with the author and the Java Gently team.

Who the book is for

The book is intended for students learning to program for the first time in Java, and who have access to the Java Development Kit from Sun, running on PCs, Macs or Unix. There is no reliance in the book on any of the myriad of development environments that are available. Students would normally be in their first year at a university or college, and could be in the science, engineering, commerce or liberal arts faculties: the examples are sufficiently wide-ranging to cater for all. The book does not require mathematical experience, and would certainly be accessible for school pupils taking computer science in senior years. Because there is an emphasis on facts and examples, rather than long discussions, the book would also be suitable for experienced programmers or hobbyists who wish to pick up Java quickly.

The book is based on many courses given in Java since 1996, and on courses in other languages presented to science and engineering students at first year university level since 1980. Included are many of the class-tested examples and exercises from these courses. All examples have been tested on JDK 1.1.4 on a Sun (Unix) and PC (Windows 95). The browser used for the applets was HotJava, with NetScape Communicator and Internet Explorer for the earlier programs..

Because technology, and especially Java, is advancing at a rapid rate, it may be that a new version of Java is available simultaneously with this book. Consult our web site for the latest information and for program updates.

Acknowledgements

This second edition of Java Gently has been greatly influenced and improved by the many people from all round the world who took the trouble to write in and share their experiences and ideas. In particular, I would like to acknowledge the input of Hanspeter Amend, Sonia Berman, Alexander Lawrence, Yehiel Jake Milman, Monica Schraefel and Darrell Wick.

Once again I am grateful to the technical staff and research students in the department who continued to help solve numerous problems of a rapidly moving Java on multiple platforms: Tony Abbott, John Botha, Louis Botha, Alwyn Moolman. This team has also kept the Java Gently programs and web site up to date and running, answering questions and queries from all over the world on a daily basis with me. Louis’ amazing enquiring mind proved a ready source of accurate answers to arcane Java questions at all hours. I am grateful also for his contribution to the programs in Chapter 14.

Addison-Wesley have once again provided outstanding professional service in the editorial and production departments in getting this book to print on time: my gratitude to Emma Mitchell, Michael Strang, Michael Klopstock, Elaine Richardson and their teams.

Finally, as always, my love and thanks to Nigel, William and Michael for yet again having to put up with me writing a book over the summer holiday, and beyond.

Judith M Bishop

Pretoria, South Africa

March 1998