Books by K. N. King
When Java appeared in 1995, I immediately noticed its potential as a language for beginning programmers. Java satisfies today's need for early instruction in an object-oriented language, while avoiding the complexities of C++. At the same time, Java is similar enough to C++ to serve as a steppingstone to that language. (At my institution, Java is the first language introduced, immediately followed by C++.) With no pointers to cause problems, Java programs are immune to those frustrating crashes that are so common in C++. If a Java program does encounter an error at run time, the interpreter provides a stack trace that can often pinpoint the problem. And, last but not least, the software needed to write and execute Java programs can be downloaded from the Web at no charge.
Who could resist a language with this many advantages for teaching? Certainly not me! I put aside my plans for writing a C++ book, and devoted the next four years to the book that you now hold in your hands.
This book attempts to satisfy a number of goals:
I've employed a spiral approach to many topics, gradually adding detail over the course of several chapters rather than covering each topic in a single place. The treatment of objects and classes is spread over Chapters 3, 7, 10, and 11, for example. Control structures are covered in Chapters 4, 5, and 8.
Unlike some Java texts, this book covers objects early and often, on the theory that if students don't know that objects are "hard," they'll quickly become accustomed to using them. To illustrate the use of objects, I rely heavily on the graphics portion of Java's Abstract Window Toolkit. To illustrate inheritance and polymorphism, I use examples from the GUI portion of the AWT.
Although GUI programming is an important part of developing software, it's not the central focus of this book. There's little point in subjecting beginners to the myriad of details that are required in developing professional GUI applications. Full GUI programming is postponed until Chapter 12, after inheritance and polymorphism have been explained. For simplicity, I've chosen to cover the older AWT classes instead of the newer Swing classes.
To make it easier for students to get started,
I've written a package named
The book was designed to be compatible with version 1.1 of the Java Development Kit and all later versions. Some chapters briefly mention some of the newer classes and methods available in version 1.2. (For brevity, I've chosen to refer to the Java 2 SDK, Standard Edition, Version 1.2 as simply "JDK version 1.2.")
Applications Versus Applets
Although many introductory Java books focus on applets rather than applications, I've chosen to discuss only applications. There are several reasons for this decision:
For instructors who wish to cover applets, Appendix D explains the mechanics of writing applets and shows how to convert applications into applets.
Each chapter ends with a "Q&A section"a series of questions and answers related to material covered in the chapter. Topics addressed in these sections include:
Many questions in Q&A sections relate directly to specific places in the chapter; these places are marked by a special icon to signal the reader that additional information is available.
In addition to Q&A sections, I've included a number of useful features, many of which are marked with simple but distinctive icons.
Choosing illustrative programs isn't an easy job. If programs are too brief and artificial, readers won't get any sense of how the features are used in the real world. On the other hand, if a program is too realistic, its point can easily be lost in a forest of details. I've chosen a middle course, using small, simple examples to make concepts clear when they're first introduced, then gradually building up to larger, more realistic programs.
This book is designed as a primary text for the college course traditionally known as CS1. No previous programming experience is necessary. The only prerequisites are a modest amount of computer literacy and the ability to think logically. The book can also be used with students who already have experience in a programming language. Students in this group should be able to progress much faster and cover more chapters.
Thanks to its Q&A sections and emphasis on practical problems, the book will also appeal to readers who are enrolled in a training class or who are learning Java by self-study.
One key principle behind the organization of this book was to cover objects early, but without full details. Later chapters provide reinforcement and fill in missing details. Chapter 3 provides the first introduction to objects and classes. Chapter 7 introduces class variables and methods. Chapter 10 emphasizes the development of instantiable classes, and shows how to integrate instance variables and methods with class variables and methods. Chapter 11 covers subclasses.
The first ten chapters are the core of the book. Students who complete these chapters should be well-prepared for further study in Java, C++, or other object-oriented languages. These ten chapters are meant to be covered in order.
Some instructors may prefer to skip Chapter 6, which deals with graphics. (I find graphics to be a great motivator, so I always cover the chapter in my own classes.) References to Chapter 6 appear in Chapters 7, 9, and 11, but the instructor can easily adjust for the missing material.
After the first ten chapters, instructors have some flexibility. Chapter 11 is a prerequisite for Chapters 12 and 14, and for parts of Chapter 13, but those three chapters can be covered in any order. Moreover, instructors can cover portions of Chapters 13 and 14 without feeling obligated to cover the entire chapter.
When this book is used as a CS1 text, a reasonable goal is to cover the first ten chapters in a semester. In a class whose students already know another programming language, the first chapter can be skipped and other early chapters can be covered rapidly, allowing additional chapters to be covered beyond the first ten. Students with previous exposure to object-oriented programming should be able to complete the entire book within a semester.
Having a variety of good exercises is obviously essential for a textbook. I've provided over 700 exercises, which fall into three categories:
Instructors who use this book as a text will find a variety of useful resources at the book's Web site (http://knking.com/books/java/), including teaching notes, overheads, solutions to selected exercises and programming projects, additional programming projects, test questions (with answers), and technical updates.
Errors, Lack of (?)
I've taken great pains to ensure the accuracy of this book. Inevitably, however, any book of this size contains a few errors. If you spot one, please email me. I'd also appreciate hearing about which features you found especially helpful, which ones you could do without, and what you'd like to see added. Errors will be listed at the book's Web site (http://knking.com/books/java/) and corrected in future printings.
First, I'd like to thank my editor at Norton, Joe Wisnovsky, for his patience and sage advice. Ann Koonce did an admirable job of copyediting the manuscript.
The following colleagues were kind enough to review some or all of the manuscript:
Richard Borie deserves special thanks for reading the entire manuscript with great care.
I also received much valuable feedback from friends and colleagues in Atlanta. Alan Wright provided many useful comments on the early chapters. Joe Levine and Ali Khanmohamadi caught a number of mistakes. The support and encouragement of my department chairs, first Fred Massey and then Marty Fraser, was especially appreciated. A number of students provided feedback, including Steve Hilbun and Jeff Padia. I am especially indebted to Jeff, whose sharp eyes spotted many a slip.
My wife, Susan Cole, helped with the book in innumerable ways, including reading the manuscript and testing the programs. As always, I owe her a huge debt for her support and assistance. Our cats, Bronco and Dennis, provided a welcome distraction from the rigors of writing.