SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Exposes students to state-of-the-art features of Java. Ex.___
Prepares Computer Science students for topics addressed in subsequent CS2/data structures courses. Ex.___
Allows for either early or late coverage of arrays. Since most instructors prefer early coverage of arrays, the array material was move up in this edition to allow even earlier coverage of arrays if that is desired. Ex.___
Give students more practice applying the concepts in the text. Ex.___
Students can run and manipulate the source code from all the examples in the book as well as from additional examples. Students have access to the JBuilder professional integrated development environment/compiler free of charge that they can use to complete programming assignments. The JBuilder software works with Windows, Linux, and Solaris platforms. Ex.___
Highly flexible book allows instructors and readers to adapt coverage of topics to their order of preference. Ex.___
Explains important problem-solving concepts to students within the context of the code itself. Ex.___
Students are able to run manipulate the source code from the CD-ROM and from the Companion Website. Ex.___
Provide suggestions for good programming practices and identify common programming problems and pitfalls. Ex.___
Expose students to real-life applications. Ex.___
Help students check their level of understanding. Ex.___
Provides quick review of chapter material. Ex.___
Provides easy-to-use console input for students, since, unlike other languages, Java does not have any simple console input facility. Ex.___
Written by a best-selling author, this concise, accessible introduction covers key language features as well as uses a conversational style to teach programmers problem solving and programming techniques with Java. Readers are introduced to object-oriented programming and important computer science concepts such as testing and debugging techniques, program style, inheritance, and exception handling. It includes thorough coverage of the Swing libraries and event driven programming. Thorough early coverage of objects is included, with an emphasis on applications over applets. Java: An Introduction to Computer Science and Programming starts from the beginning and teaches traditional, more basic techniques, such as algorithm design. KEY TOPICS: The author includes a highly flexible format that allows instructors and readers to adapt coverage of topics to their preferred order. Covers Java2, Sun's latest version of the Java language and contains a flexible design. MARKET: Appropriate for readers interested in an introduction to Computer Science using Java (CS1 with Java) and other introductory programming courses.
Creating Window Interfaces Using Swing Objects
(NOTE: All chapters conclude with Chapter Summary, Glossary, Answers to Self-Test Questions, and Programming Exercises.)
1. Introduction to Computers and Java Objects.
Computer Basics. Internet Basics. Designing Programs. A Taste of Java.
Primitive Types and Expressions. The Class String. Keyboard and Screen I/O. Documentation and Style. Windowing I/O with JOptionPane (Optional).
Branching Statements. Java Loop Statements. Programming with Loops. The Type Boolean.
Class and Method Definitions. Information Hiding and Encapsulation. Objects and Reference.
Programming with Methods. Static Methods and Static Variables. Designing Methods. Overloading. Constructors. Information Hiding Revisited. Packages. Inner Classes (Optional).
Array Basics. Arrays in Classes and Methods. Programming with Arrays and Classes. Sorting Arrays. Multidimensional Arrays.
Inheritance Basics. Programming with Inheritance. Dynamic Binding and Polymorphism.
Basic Exception Handling. Defining Exception Classes. Using Exception Classes.
An Overview of Streams and File I/O. Text File I/O. Binary File I/O. File Objects and File Names.
Vectors. Linked Data Structures.
The Basics of Recursion. Programming with Recursion.
Background. Basic Swing Details. Buttons and Action Listeners. Container Classes. Text I/O for GUIs.
HTML. Applets.
Menus. Making GUIs Pretty (and More Functional). More Layout Managers. Inner Classes. More on Events and Listeners. Another Look at the Swing Class Hierarchy.
Basic Figures. Colors. Fonts and Other Text Details.
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.)
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).
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.
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.
Alternative 1:
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.
Alternative 2:
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.
At the time this book went to press, the site for the Java compiler download from Sun Microsystems was: http://java.sun.com/products/jdk/1.2/
There is a version of Java for the Mac that can be downloaded from the Sun Microsystems website. 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: http://www.borland.com/jbuilder/foundation/download/
Alternative 1:
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.
Alternative 2:
You can down load a free Java compiler over the Internet from Sun Microsystems. At the time this book went to press, the site for the Java download from Sun Microsystems was: http://java.sun.com/products/jdk/1.2/
We do not have an editor/environment (other than JBuilder) that we recommend for use with this compiler. 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.)
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.
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.
Any updates or corrections will be listed on the author's website for this book http://www.cse.ucsd.edu/users/savitch/books/csl.java/
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.edu
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.
Walter Savitch
http://www.cse.ucsd.edu/users/savitch
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.
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.
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.
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.
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.
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.
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.
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 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.
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.
The support materials described below can be obtained from the publisher, obtained over the Internet, or are included with the book.
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.
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: http://www.borland.com/jbuilder/foundation/download/
Another very good alternative is to download a version of Java-2 from Sun Microsystems website. At the time this book went to press, the URL for the website was: http://java.sun.com/products/jdk/1.2/
Java-2 is the main version of Java that we used in developing this text. 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 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 http://www.prenhall.com/
Any updates or corrections will be listed on the author's website for this book http://www.cse.ucsd.edu/users/savitch/books/csl.java/