Preface
·
What this book will tell you
This book explains how to write Java
programs that run either as independent applications or as applets (part of a
web page).
·
This book is for novices
If you have never done any programming
before – if you are a complete novice – this book is for you. This book
assumes no prior knowledge of programming. It starts from scratch. It is
written in a simple, direct style for maximum clarity. It is aimed primarily at
first year undergraduates at universities and colleges, but it is also suitable
for novices studying alone.
·
Why Java?
Java is probably one of the best programming
languages to learn and use because of the following features.
Java is small and beautiful
The designers of Java have deliberately
left out all the superfluous features of programming languages; they cut the
design to the bone. The result is a language that has all the necessary
features, combined in an elegant and logical way. The design is lean and mean.
It is easy to learn, but powerful.
Java is object-oriented
Object-oriented languages are the latest
and most successful approach to programming. Object-oriented programming is the
most popular approach to programming. Java is completely object-oriented from
the ground up. It is not a language that has had object-orientedness grafted
onto it as an afterthought.
Java supports the Internet
A major motivation for Java is to enable
people to develop programs that use the Internet and the World-Wide Web. Java applets
can easily be invoked from Web browsers such as Internet Explorer to provide
valuable and spectacular facilities. In addition, Java programs can be easily
transmitted around the Internet and run on any computer.
Java is general-purpose
Java is a truly general-purpose language.
Anything that C++, Visual Basic etc. can do, so can Java.
Java is platform-independent
Java programs will run on almost all
computers and with nearly all operating systems –
unchanged! Try that with any other programming language. (You almost certainly can't!) This is summed up in the
slogan "write once –
run anywhere".
Java is robust
The Java compiler carries out many
stringent checks as it prepares a program for execution. Once a program has
been corrected and compiles without errors, it often performs correctly.
However, if a Java program goes wrong (and programs do have that tendency), it won't create mayhem, damage and
uncertainty.
Java has libraries
Because Java is a small language, most of
its functionality is provided by pieces of program held in libraries. A whole
host of library software is available to do graphics, access the Internet, provide
graphical user interfaces (GUIs) and many other things.
·
You will need
To learn to program you need a computer and
some software. A typical system is a PC (personal computer) with the Java Software
Development Kit (SDK). This is also available for Unix, GNU/Linux
and Apple systems. This kit allows you to prepare and run Java programs. There
are also more convenient development environments. We provide
the SDK and additional software on the accompanying CD-ROM
.
·
Exercises are good for you
If you were to read this book time and
again until you could recite it backwards, you still wouldn't
be able to write programs. The practical work of writing programs and program
fragments is vital to becoming fluent and confident at programming.
There are exercises for the
reader at the end of each chapter. Please do some of them to enhance your
ability to program.
There are also short self-test
questions with answers throughout the text, so that you can check you have
understood things properly.
·
What's
included?
This book explains the fundamentals of
programming:
·
variables;
·
assignment;
·
input and output;
·
calculation;
·
graphics and windows programming;
·
selection using if;
·
repetition using while.
It also covers integer numbers,
floating-point numbers and character strings. Arrays are also described. All
these are topics that are fundamental,
whatever kind of programming you go on to do.
This book also thoroughly
addresses the object-oriented aspects of programming:
·
using library classes.
·
writing classes;
·
using objects;
·
using methods;
We also look at some of the more sophisticated
aspects of object-oriented programming, like:
·
inheritance;
·
polymorphism;
·
interfaces
·
What's
not included
This book describes the essentials of Java.
It does not explain the bits and pieces, the bells and whistles. Thus the
reader is freed from unnecessary detail and can concentrate on mastering Java
and programming in general.
·
Applications or Applets?
There are two distinct types of Java
program:
·
a distinct free-standing program (this is called
an application);
·
a program invoked from a Web browser (this is
called an applet).
In this book we concentrate on applications,
because we believe that this is the main way in which Java is being used. (We
explain how to run applets in an appendix.)
·
Graphics or text?
Throughout the text we have emphasized
programs that use graphical images rather than text input and output. We think
they are more fun, more interesting and clearly demonstrate all the important
principles of programming. We haven't
ignored programs that input and output text –
they are included, but they come second best.
·
Graphical user interfaces (GUIs)
The programs we present use many of the
features of a graphical user interface (GUI), such as windows, buttons,
scrollbars and using the mouse in lots of different ways.
·
AWT or Swing?
There
are two Java mechanisms for creating and using GUI's - AWT and Swing. The Swing set of
user interface components is more complete and powerful than the AWT set. This
book uses the Swing approach because it is being used more widely.
·
The sequence of material
Programming involves many challenging
ideas, and one of the problems of writing a book about programming is deciding
how and when to introduce new ideas. We introduce simple ideas early and more
sophisticated ideas later on. We use objects from an early stage. Then later we
see how to write new objects. Our approach is to start with ideas like
variables and assignment, then introduce selection and looping, and then go on
to objects and classes (the object-oriented features). We also wanted to make
sure that the fun element of programming is paramount, so we use graphics right
from the start.
·
Bit by bit
In this book we introduce new ideas
carefully one-at-a-time, rather than all at once. So there is a single chapter
on writing methods, for example.
·
Computer Applications
Computers are used in many different
applications and this book uses examples from all these areas:
·
l information processing;
·
l games;
·
l scientific
calculations.
The reader can choose to
concentrate on those application areas of interest and spend less time on the
other areas.
·
Different kinds of programming
There are many different kinds of
programming – examples are procedural, logic,
functional, spreadsheet, visual and object-oriented programming. This book is
about the dominant type of programming –
object-oriented programming –
as practiced in languages like Visual Basic, C++, C#, Eiffel and Smalltalk.
·
Which version of Java?
Java is evolving, but slowly. From
time to time Sun release a new version of the Java Software Development Kit
(SDK). A new version usually means additional items in the libraries - and a
new version number. All versions from 1.2 have the generic name of Java 2. You can use this
book with any version from version 1.2 onwards.
·
Have fun
Programming is creative and interesting,
particularly in Java. Please have fun!
·
l Visit our Web site
All the programs presented in this book are
available on our Web site, which can be reached via: www.booksites.net/bell.
·
Changes to this edition
If you have used earlier
editions of this book, you might like to know what is different about this
edition.
In making changes we have tried
to keep to the sprit of the original and at the same time simplify things where
possible. We have also tried to follow the main trends in using Java. So while
the older edis used applets and the AWT for GUIs, this edition uses
applications and Swing. The older editions used the method paint extensively and everyone find it difficult to
understand. So we have eliminated paint to give major simplifications.
We have also made minor changes,
such as using the term “call” rather than the long-winded term “invoke”. We
have also used double variables instead of float because
declaring literals is easier.
There used to be a chapter on
applet architecture, but we were never sure that it worked. With the
elimination of method paint and the simpler structure of applications, we
don’t think this chapter is needed anymore. There was also a chapter on GUI
components, which seemed out of place. So this chapter has been removed and
incorporated within an improved appendix on the Java library, which presents
sample programs.
The topic of abstract classes
has been moved into the chapter on inheritance, where it rightly belongs. We have created a new chapter on array lists,
which serves as an introduction to data structures.
We hope you like the changes.
·
Any comments on this book?
If you want to email the authors, we are at
D.H.Bell@shu.ac.uk and M.Parr@shu.ac.uk. We look forward to hearing from you.