PREFACE FOR STUDENTS
This book is designed to teach you the Java programming language, and even more importantly, to teach you basic programming techniques. This book requires no previous programming experience and no mathematics other than some very simple high school algebra. However, to get the full benefit of the book, you should have a version of Java available on your computer, so that you can practice with the examples and techniques given in the book. You should have a version of Java called Java 2 (or some number higher than 2). If you have a version number of the form 1.l.x or 1.2.x, then the version number should be 1.2.x or higher. (The exact number that is filled in for the x is not critical. The x need not even be present. If it says only "version 1.2," that is fine.) If You Have Programmed Before
You need not have any previous programming experience to use this book. This book was designed for beginners. However, the book can still be used to learn Java if you happen to have had experience with some other programming languages, but allow me to give you a few words of advice. If you have programmed before, do not assume that Java is the same as the programming language(s) you are used to using. All languages are different. And the differences, even if small, are large enough to give you problems. Read at least the boxed sections of Section 1.4 in Chapter 1 and all the boxed sections of Chapters 2 and 3. By the time you reach Chapter 4, it would be wise to read the entire chapter.
If you have programmed before in either C or C++, the transition to Java can be troublesome. While Java is very different from C and C++, at first glance it looks as if it is the same as C++. Appendix 11 has a comparison of Java and C++ that will help you see the differences between Java and C++ (or Java and C). Copies of the Programs from the Text
This book contains a CD that includes all the programs and other software examples in the book, so that you can practice with these examples without having to type them into your computer. Obtaining a Copy of Java
How and what version of Java you use depends somewhat on what operating system you are using. Be sure to consult the subsection below that corresponds to your operating system. Microsoft Windows
The CD that comes with this book includes a version of JBuilder 3.5 Foundation, a complete Java integrated environment from Inprise/Borland. JBuilder includes an editor and other utilities in addition to the Java language. This has everything you need to write and run Java programs. This is a professional strength environment, which can be a bit complex for novices, so we also have an alternative that gives you an easier environment.
This is a bit more complicated to initially set up, but easier to use once you do set up things. Download a free Java compile over the Internet from Sun Microsystems. Install that Java compiler and the TextPad environment, which is provided on the CD that comes with this book. The TextPad environment provides an editor and other tools to use when writing Java programs.Unfortunately, users have not been happy with the Mac version of Java provided at this site, and indeed, it may not do all things discussed in this book.
If you are using the Mac operating system, one good alternative is to purchase a version of CodeWarrior from Metrowerks, Inc. It works well with the Mac operating system.
A version of JBuilder for the Mac is due out soon and promises be an excellent alternative for Mac users. You may want to check the following website to see if it is available. If it is, you can download it from there: borland/jbuilder/foundation/download/ UNIX Operating System
The CD that comes with this book includes a version of JBuilder 3.5 Foundation, a complete Java integrated environment from Inprise/Borland. JBuilder includes an editor and other utilities in addition to the Java language. This has all the software you need in order to write and run Java programs. JBuilder has versions for both the Solaris and Linux operating systems.
You can down load a free Java compiler over the Internet from Sun Microsystems.You can use your favorite editor to write programs and then run your Java programs from the command line as described in Chapter 1. (Or you may find an environment you like and can use it.) Self-Test Questions
Each chapter contains numerous self-test questions. Complete answers for all the self-test questions are given at the end of each chapter. One of the best ways to practice what you are learning is to do the self-test questions without looking at the answers. Only look at the answers after you have answered the self-test questions. This Text Is Also a Reference Book
In addition to using this book as a textbook, you can and should use it as a reference. When you need to check a particular point that you may have forgotten or that you hear mentioned by somebody but have not yet learned yourself, just look in the index. Many index entries give a page number for "quick reference." Turn to this quick reference page. It will contain a short entry, usually set off in a box, that gives all the essential points on that topic. This can be done to check details of the Java language, as well as details on programming techniques.
Boxed sections in every chapter give you a quick summary of the main points in that chapter. You can use these boxes to review the chapter, preview the chapter, or check details of the Java language. Updates and Corrections
Any updates or corrections will be listed on the author's website for this book cse.ucsd/users/savitch/books/csl.java/ We Want Your Opinions
This book was written for you, and I would like to hear any comments you have on the book. You can contact me via electronic mail at the following address: wsavitch@ucsd
Unfortunately, I cannot provide you with answers to the programming exercises. Only instructors who adopt the book can receive (selected) answers from the publisher. For help on the programming exercises, you will have to contact your instructor. (Even if you are not enrolled in a class we still cannot provide answers to programming exercises.) But, remember that there are answers to all the self-test questions at the end of each chapter.
cse.ucsd/users/savitch PREFACE FOR INSTRUCTORS
This book was designed to be used in a first course in programming and computer science. It covers programming techniques, as well as the basics of the Java programming language. It is suitable for courses as short as one quarter or as long as one full academic year. It requires no previous programming experience and no mathematics other than a little high school algebra. This book can also be used for a course designed to teach Java to students who have already had another programming course, in which case, the first few chapters can be assigned as outside reading. (If students have had previous programming experience in C or C++, then there is also an appendix that explains some differences between Java and C or C++.) All the code in the book has been tested using Java 2 of Sun Microsystems. The coverage of Java was carefully arrived at by class testing and is a concise, accessible introduction for beginners. Changes in this Edition
If you have not used the first edition of this text, you can skip this subsection. If you have used the first edition, this subsection will tell you how this second edition differs from the first edition.
For instructors, the transition from the first edition of this text to this edition is easy. You can teach the same course with basically the same topics presented in the same order. Some chapters have changed numbers, but you can still cover those chapters in the order you are currently using. The biggest change was to move the arrays chapter forward to Chapter 6. However, you can cover arrays later if you prefer with no loss of continuity in reading the text. The only significant change you will need to contend with is that this edition uses the Swing library instead of using only the AWT library as the first edition did. However, there have been changes and additions that you may find helpful.
This edition adds coverage of the Swing Libraries, the Graphics class, and linked data structures to the topics covered in the first edition. In addition, the entire book has been rewritten to make the material clearer and more complete. There are many more Self-Test Questions and many more Programming Exercises in this edition.
In response to requests from users of the first edition, we have adopted the policy of listing instance variables first in class definitions (as opposed to last, as in the first edition).
This book also contains some early, optional material on applets and another GUI class named JOption Pane. This allows instructors to introduce GUI interfaces early if they wish, or wait to introduce them later (or not at all) if that is preferred. Java 2 Coverage
The first edition of this book was already fully compatible with Java 2. This edition adds coverage of Swing and other Java 2 details to provide more complete coverage of Java 2. Flexible
If you are an instructor, this book adapts to the way you teach, rather than making you adapt to the book. This book does not tightly prescribe the order in which your course must cover topics. Neither does it prescribe the specialized libraries that must be used in your course. You can easily change the order in which chapters and sections are covered. The details about rearranging material are explained in a chart at the end of this preface and in more details in a prerequisite section at the start of each chapter.
Since Java does not include any simple console input, most texts, even more advanced texts, provide some added class library for console input. This book requires that you add as little nonstandard software as possible, since only one simple class is added (for console input). Even that one console input class, which is included early in the book, becomes an understandable programming example for students well before the end of the book. All the remaining software is from standard Java libraries that should be part of any Java installation. Coverage of Problem Solving and Programming Techniques
This book is designed to teach students basic problem-solving and programming techniques and is not simply a Java syntax book. The book contains numerous case studies and programming tips, as well as many other sections that explain important problem-solving and programming techniques, such as loop design techniques, debugging techniques, style techniques, abstract data types, basic object-oriented programming including event-driven programming, and other computer science topics. Object-Oriented and Traditional Techniques
Any course that really teaches Java must teach classes early, since almost everything in Java involves classes. The behavior of parameters depends on whether they are class parameters. Even the behavior of the equals operator (= =) depends on whether it is comparing objects or simpler data items. Classes cannot be avoided, except by means of absurdly long and complicated "magic formulas." This book introduces classes fairly early. Some exposure to using classes is introduced in Chapters 1 and 2. Defining classes is covered in Chapter 4. Moreover, all the basic information about classes, including inheritance, is presented by the end of Chapter 7 (and this can be done omitting Chapter 6). However, some topics on classes, including inheritance, can be postponed to later in a course.
Although this is an early classes book, it does not neglect traditional programming techniques, such as top-down design and loop design techniques. These older topics may no longer be glamorous, but they are information that all beginning students need. Swing, Applets, and Other GUIs
Starting with Java 2, Java comes with an improved GUI library known as Swing that allows programmers to design portability GUIs (graphical user interfaces). This book uses Swing to teach students to produce professional looking windowing interfaces. In the process, students learn event-driven programming, as well as receiving a lot of practice with object-oriented programming.
As this material was class-tested and views of instructors were gathered, we found that Swing was a more accessible way to teach students object-oriented programming than applets. Thus, we place greater emphasis on Swing. This makes sense, since almost all advanced applets tools are really Swing tools. However, for those who do want to cover applets early, Chapter 1 has an optional section that previews applets. Chapter 13 covers applets in detail and may be covered much earlier than the chapter number suggests. You may choose to introduce GUIs early, late, or not at all.
With the introduction of the Swing libraries, there is a new class named JOption P a n e that allows an easier introduction to GUIs than applets provide. This book covers JOption Pane in an optional section of Chapter 2. You have the choice of introducing either or both applets and JOption Pane either late or early (or not at all).
In addition to this optional GUI material in Chapters 1 and 2, this book includes three full chapters on GUIs, which gives thorough coverage of Swing, applets, and the Graphics class for simple two-dimensional graphics. Language Details and Sample Code
This book teaches programming technique and does not simply teach the Java language. However, neither students nor instructors would be satisfied with an introductory programming course that did not also teach the programming language. Until you calm a student's fears about language details, it is often impossible to get her or his attention to discuss bigger issues. For this reason, this book gives complete explanations of Java language features and lots of sample code. Programs are given in their entirety along with sample input and output. In many cases, there are even extra complete examples on the CD, in addition to the complete examples in the text. Self-Test Questions
Self-test questions are spread throughout each chapter. These questions have a wide range of difficulty levels. Some require only a one-word answer, whereas others require the reader to write an entire, nontrivial program. Complete answers for all the self-test questions, including those requiring full programs, are given at the end of each chapter. Class Tested
The material in this book has been fully class tested. Much of the material and methods of presentation were revised in response to this class testing. Support Material
The support materials described below can be obtained from the publisher, obtained over the Internet, or are included with the book. CD-ROM
Each book contains a CD that includes all the programs and classes in the book. The CD also includes a version of JBuilder 3.5 Foundation, a complete Java integrated environment from Inprise/Borland. JBuilder includes an editor and other utilities in addition to the Java language. The CD includes versions of JBuilder for Windows, Solaris, and Linux operating systems. The CD also includes a copy of TextPad, a very nice integrated environment that runs under Windows and that is a suitable environment for use with Sun's Java 2. Free Software
You have a wide choice of free software to use with this book. As already noted the CD that comes with this book includes JBuilder and TextPad.
JBuilder works under Windows, Solaris, and Linux operating systems. At the time this book went to press a version of JBuilder for the Mac was not available, but was due out soon. You may want to check the following website to see if it is available. If it is, you can download it from there: borland/jbuilder/foundation/download/
Another very good alternative is to download a version of Java-2 from Sun Microsystems website.The TextPad environment (which comes on the CD that accompanies this book) is a good environment to use with Sun's Java-2, provided you are using a Windows operating system. TextPad only runs under Windows. Instructor's Resource Guide and Companion Website
Instructor tools include a chapter-by-chapter Instructor's Resource Guide that contains numerous teaching hints, quiz questions with solutions, and solutions to many programming exercises. The Companion Website includes code, PowerPoint slides, and other teaching resources. Instructors should contact their Prentice Hall sales representative to obtain a copy of the Instructor's Resource Guide and receive information on how to access the Companion Website. For the name and number of your sales representative, please call Prentice Hall Faculty Services at 1-800-5260485. Additional information on this book and other Prentice Hall products can be found on Prentice Hall's website at prenhall/ Updates and Corrections
Any updates or corrections will be listed on the author's website for this book cse.ucsd/users/savitch/books/csl.java/