knking.com -- Programming Language Books and Training

 C C++ Java

Books by K. N. King
Home
Books by K. N. King
Short courses
Recommended books
Recommended links
FAQ

Java Programming: From the Beginning

Preface

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.

Goals

This book attempts to satisfy a number of goals:

  • Cover objects early. It's now apparent that the entire software field will soon be "object-oriented," if it's not already. Students need to think in terms of objects and classes from the beginning of their studies.
  • Cover "traditional" computer science concepts. Coverage of topics such as data types, conditionals, and loops is crucial to lay a foundation for future studies in computer science.
  • Teach problem-solving skills. Student need to learn how to write algorithms and then translate those algorithms into working programs.
  • Emphasize the importance of program design. Once their programs are large enough to justify a discussion of design techniques, students need to be exposed to stepwise refinement and object-oriented design as tools for program design. Design is a difficult subject for beginners, whose programs are so small that the need for a good design isn't always apparent. In addition to incorporating discussions of design throughout the text, I've included a number of case studies, which allow a discussion of design in more depth than would be possible with smaller programs.
  • Emphasize good style. Although an appreciation for design takes time to develop, even beginners can quickly grasp the importance of style. I've included many discussions of style in the book. I've adopted a style that's consistent with industry practices, and I've been careful to use that style in all the examples and case studies.
  • Introduce students to the culture of computer science. The target audience for this book includes many students whose knowledge of the computing field is limited to word processing and game playing. I've tried to use this book as a springboard for introducing the terminology, concepts, and general culture of computer science.
  • Prepare students to learn other languages. At most colleges, Java won't be the only programming language that students will learn. Recognizing this fact, I've tried to prepare students for the transition to C++ and other languages.
  • Make programming fun, not work. By providing interesting programming projects—often involving graphics—I've tried to show students that they can have fun at the same time they're learning.
  • Use real-world examples. Rather than relying on simplistic examples, I emphasize practical, real-world problems. Using realistic examples helps motivate students and shows clearly how programming techniques are used in the real world.
  • Emphasize understanding, not imitating. I have little patience for books that take a cookbook approach to programming, allowing students to imitate rather than understand.

Approach

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 jpb that can be downloaded from the book's Web site (http://knking.com/books/java/). This package provides classes for console input, displaying graphics in a window, and converting strings to floating-point numbers. I'm not a big fan of instructor-supplied classes, so I've kept the use of these classes to a bare minimum.

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:

  • Writing applets requires "magic." I prefer that beginners understand what's going on as much as possible. With applets, there is no clear flow of control. Instead, methods are called by a Web browser when needed.
  • Writing applets requires an understanding of inheritance. Writing an applet involves extending the Applet class, calling inherited methods, and overriding other methods. Many applications, on the other hand, require no use of inheritance, so a discussion of that topic can be postponed until students have a better understanding of classes.
  • Applets have no more power than applications. Relying on applications doesn't mean that we can't use graphics, animation, or any other Java feature. An application can do virtually everything that an applet can.
  • Applets are restricted. Restrictions on applets—especially where files are concerned—means that students will eventually need to write applications anyway.
  • Applications are more important than applets. In the long run, the vast majority of Java programs will be applications, not applets. Java is a powerful, general-purpose language whose potential uses go far beyond applets.
  • Applets don't exist in other languages. Java isn't the only language that students will be exposed to during their studies. The applet concept doesn't exist in other languages, so students who are primarily adept at writing applets won't be able to transfer their skills as well to those languages.

For instructors who wish to cover applets, Appendix D explains the mechanics of writing applets and shows how to convert applications into applets.

Q&A Sections

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:

  • Frequently asked questions. I've tried to answer questions that come up frequently in my own courses, in other books, and on newsgroups related to Java.
  • Additional discussion and clarification of tricky issues. Although some readers may be satisfied with a brief explanation and a couple of examples, others may want more detail.
  • Side issues that don't belong in the main flow. Some questions raise technical issues that won't be of interest to all readers.
  • Material too advanced or too esoteric to interest the average reader. Curious readers with previous programming experience may wish to delve into these questions immediately; others should skip them on a first reading.

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.

Other Features

In addition to Q&A sections, I've included a number of useful features, many of which are marked with simple but distinctive icons.

  • Warnings alert readers to common pitfalls. Although Java has fewer traps than many popular languages (C and C++ come to mind), Java programmers are not immune to them. I've tried to point out the pitfalls that are most common and/or most important.
  • Style tips discuss stylistic issues, including naming conventions, indentation, brace placement, comment usage, private versus public, and more. Each tip provides a recommendation for establishing a consistent and logical style.
  • Design tips are similar to style tips, but tackle larger issues, such as the proper use of class variables, the importance of using helper methods, and how to design a class. Design tips begin to appear starting with Chapter 7. Some sections of the book are devoted entirely to design issues; the "design tip" icon appears at the beginning of such a section.
  • Cross-references provide a hypertext-like ability to locate information. Although many of these are pointers to topics covered later in the book, some point to previous topics that the reader may wish to review.
  • Debugging sections offer tips for effective testing and debugging.
  • Sidebars cover topics that aren't strictly part of Java but that every knowledgeable Java programmer should be aware of, including number bases, the IEEE floating-point standard, and pseudorandom numbers.

Programs

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.

Source Code

Source code for all programs in this book is available via the Web at http://knking.com/books/java/. Updates, corrections, and news about the book are also available at this site.

Audience

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.

Organization

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.

Exercises

Having a variety of good exercises is obviously essential for a textbook. I've provided over 700 exercises, which fall into three categories:

  • Review questions. Most sections end with several review questions. Answers to review questions appear at the end of each chapter.
  • End-of-chapter exercises. Exercises appear at the end of each chapter, grouped by section. Some exercises are quite easy, while others might take an hour or so to complete.
  • Programming projects. Each chapter except the first contains programming projects. Some of these projects involve modifying programs given in the current chapter or in a previous chapter. Others are modifications to earlier projects. A substantial number are new projects. The difficulty varies considerably, from programs that can be completed in an hour or so to programs that might take several days to finish.

Instructor Resources

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.

Acknowledgments

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:

Manuel Bermudez, University of Florida
Richard Borie, University of Alabama
Jim Clarke, University of Toronto
Doug Cooper, Southeast Asian Software Research Center, Bangkok
Ronald Leach, Howard University
John Motil, California State University, Northridge
William Topp, University of the Pacific

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.


Copyright © 2000 W. W. Norton & Company, Inc. All rights reserved.

Copyright © 2000-2004 K. N. King. All rights reserved.