LOCATION OF VIDEONOTES IN THE TEXT Chapter 1 Compiling a Java program, p. 20 Writing an algorithm, p. 25 Recognizing a hidden error, p. 28 Another applet example, p.
38 Writing an algorithm for Project 5, p. 42 Chapter 2 Another sample program, p. 61 Writing arithmetic expressions and statements, p. 87 Pitfalls involving nextLine(), p.
97 Solving a conversion problem, p. 130 Solution to Project 13, p. 132 Chapter 3 Using multibranch if-else statements, p. 159 Using switch statements, p.
177 Solution to Project 2, p. 190 Responding to user input, p. 190 Chapter 4 Using nested while loops, p. 213 Comparing loop statements, p.
238 Solution to Project 9, p. 252 Nesting for statements, p. 253 Chapter 5 Writing and involving methods, p. 292 Investigating public and private access, p.
300 Objects and references, p. 326 Exploring parameters of class types, p. 345 Developing a solution to Project 8, p. 363 Solution to Project 12, p.
366 Chapter 6 Writing construction, p. 387 Using static and non-static methods, p. 396 Writing and invoking overloaded methods, p. 433 Solving a similar problem, p.
468 Solution to Project 12, p. 469 (Continued on Inside Back Cover) get with the programming Through the power of practice and immediate personalized feedback, MyProgrammingLab improves your performance. Learn more at www.com This page intentionally left blank ™ An Introduction to Problem Solving & Programming This page intentionally left blank ™ 6th edition An Introduction to Problem Solving & Programming Walter Savitch University of California, San Diego Contributor Kenrick Mock University of Alaska Anchorage Prentice Hall Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City São Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo Editorial Director: Marcia Horton Text Designer: Jerilyn Bockorick, Nesbitt Graphics, Inc. Editor-in-Chief: Michael Hirsch Cover Designer: Rachael Cronin Acquisitions Editor: Matt Goldstein Manager, Rights and Permissions: Michael Joyce Editorial Assistant: Chelsea Bell Text Permission Coordinator: Jennifer Kennett Director of Marketing: Patrice Jones Cover Art: Aron Jungermann / Getty Images Marketing Manager: Yezan Alayan Media Director: Daniel Sandin Marketing Coordinator: Kathryn Ferranti Media Project Manager: Wanda Rockwell Managing Editor: Jeff Holcomb Full-Service Project Management: Rose Kernan, Nesbitt Production Project Manager: Heather McNally Graphics, Inc.
Senior Operations Supervisor: Alan Fischer Composition: Glyph International Ltd. Operations Specialist: Lisa McDowell Interior Printer/Bindery: Edwards Brothers Art Director: Kristine Carney Cover Printer: Coral Graphics Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on appropriate page within text. Microsoft® and Windows® are registered trademarks of the Microsoft Corporation in the U. and other coun- tries.
Screen shots and icons reprinted with permission from the Microsoft Corporation. This book is not spon- sored or endorsed by or affiliated with the Microsoft Corporation. Copyright © 2012, 2009, 2005, 2004, 2001 Pearson Education, Inc., publishing as Prentice Hall, 1 Lake Street, Upper Saddle River, New Jersey, 07458. All rights reserved.
Manufactured in the United States of America. This publication is protected by Copyright, and permission should be obtained from the publisher prior to any prohibited reproduc- tion, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopy- ing, recording, or likewise. To obtain permission(s) to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, 1 Lake Street, Upper Saddle River, New Jersey, 07458 Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Library of Congress Cataloging-in-Publication Data LIBRARY OF CONGRESS CATALOGING-IN-PUBLICATION DATA ON FILE 10 9 8 7 6 5 4 3 2 1—EB—15 14 13 12 11 ISBN 10: 0-132-16270-9 ISBN 13: 978-0-132-16270-8 Preface for Instructors Welcome to the sixth edition of Java: An Introduction to Problem Solving & Programming. This book is designed for 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 quar- ter or as long as a full academic year.
No previous programming experience is required, nor is any mathematics, other than a little high school algebra. The 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. Changes in This Edition The following list highlights how this sixth edition differs from the fifth edition: ■ Updates have been made for Java version 7, including strings in switch statements and the use of type inference in generic instance creation. ■ Additional case studies including unit testing, use of the Comparable inter- face, processing comma-separated value files, and others.
■ Chapter 5 now begins with a simpler class to more gradually introduce how classes are constructed. ■ Chapter 8 has been reorganized with a greater emphasis and more examples on polymorphism and interfaces. ■ Chapter 2 describes how to create a Swing application using the JFrame class so thereafter students have the option of implementing graphics in applets or in an application. ■ Chapter 12 includes an overview of the Java Collections Framework and examples using the HashMap and HashSet classes.
■ A description of System.printf has been added to Chapter 2. ■ A description of Math.random has been added to Chapter 6. ■ Twenty new programming projects have been added. ■ New VideoNotes added throughout the text to enhance student understanding of programming concepts and techniques.
vii viii PREFACE FOR INSTRUCTORS Latest Java Coverage All of the code in this book has been tested using a pre-release version of Oracle’s Java SE Development Kit (JDK), version 7. Any imported classes are standard and in the Java Class Library that is part of Java. No additional classes or specialized libraries are needed. Flexibility If you are an instructor, this book adapts to the way you teach, rather than making you adapt to the book.
It does not tightly prescribe the sequence in which your course must cover topics. You can easily change the order in which you teach many chapters and sections. The particulars involved in rearranging material are explained in the dependency chart that follows this preface and in more detail in the “Prerequisites” section at the start of each chapter. Early Graphics Graphics supplement sections end each of the first ten chapters.
This gives you the option of covering graphics and GUI programming from the start of your course. The graphics supplement sections emphasize applets but also cover GUIs built using the JFrame class. Any time after Chapter 8, you can move on to the main chapters on GUI programming (Chapters 13 through 15), which are now on the Web. Alternatively, you can continue through Chapter 10 with a mix of graphics and more traditional programming.
Instructors who prefer to postpone the coverage of graphics can postpone or skip the graphics supplement sections. Coverage of Problem-Solving and Programming Techniques This book is designed to teach students basic problem-solving and program- ming techniques and is not simply a book about Java syntax. It contains numerous case studies, programming examples, and programming tips. Ad- ditionally, many sections explain important problem-solving and program- ming techniques, such as loop design techniques, debugging techniques, style techniques, abstract data types, and basic object-oriented programming tech- niques, including UML, event-driven programming, and generic programming using type parameters.
Early Introduction to Classes Any course that really teaches Java must teach classes early, since everything in Java involves classes. A Java program is a class. The data type for strings of characters is a class. Even the behavior of the equals operator (==) depends on whether it is comparing objects from classes 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 given in Chapters 1 and 2. Chapter 5 covers how to define classes. All PREFACE FOR INSTRUCTORS ix of the basic information about classes, including inheritance, is presented by the end of Chapter 8 (even if you omit Chapter 7).
However, some topics regarding classes, including inheritance, can be postponed until later in the course. Although this book introduces classes early, it does not neglect traditional programming techniques, such as top-down design and loop design tech- niques. These older topics may no longer be glamorous, but they are informa- tion that all beginning students need. Generic Programming Students are introduced to type parameters when they cover lists in Chapter 12.
The class ArrayList is presented as an example of how to use a class that has a type parameter. Students are then shown how to define their own classes that include a type parameter. Language Details and Sample Code This book teaches programming technique, rather than simply the Java lan- guage. However, neither students nor instructors would be satisfied with an introductory programming course that did not also teach the programming language.
Until you calm students’ fears about language details, it is often im- possible to focus their attention on bigger issues. For this reason, the book gives complete explanations of Java language features and lots of sample code. Programs are presented in their entirety, along with sample input and output. In many cases, in addition to the complete examples in the text, extra complete examples are available over the Internet.
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 an- swers for all the self-test questions, including those requiring full programs, are given at the end of each chapter.
Exercises and Programming Projects Completely new exercises appear at the end of each chapter. Since only you, and not your students, will have access to their answers, these exercises are suitable for homework. Some could be expanded into programming projects. However, each chapter also contains other programming projects, several of which are new to this edition.
Support Material The following support materials are available on the Internet at www.com/irc: x PREFACE FOR INSTRUCTORS For instructors only: ■ Solutions to most exercises and programming projects ■ PowerPoint slides ■ Lab Manual with associated code. Instructors should click on the registration link and follow instructions to re- ceive a password. If you encounter any problems, please contact your local Pearson Sales Representative. For the name and number of your sales represen- tative, go to pearsonhighered.
For students: ■ Source code for programs in the book and for extra examples ■ Student lab manual ■ VideoNotes: video solutions to programming examples and exercises.com/savitch to access the student resources. Online Practice and Assessment with MyProgrammingLab MyProgrammingLab helps students fully grasp the logic, semantics, and syn- tax of programming. Through practice exercises and immediate, personalized feedback, MyProgrammingLab improves the programming competence of be- ginning students who often struggle with the basic concepts and paradigms of popular high-level programming languages. A self-study and homework tool, a MyProgrammingLab course consists of hundreds of small practice problems organized around the structure of this textbook.
For students, the system automatically detects errors in the logic and syntax of their code submissions and offers targeted hints that enable students to figure out what went wrong—and why. For instructors, a comprehensive gradebook tracks correct and incorrect answers and stores the code inputted by students for review. MyProgrammingLab is offered to users of this book in partnership with Turing’s Craft, the makers of the CodeLab interactive programming exer- cise system. For a full demonstration, to see feedback from instructors and students, or to get started using MyProgrammingLab in your course, visit www.
VideoNotes VideoNote VideoNotes are Pearson’s new visual tool designed for teaching students key programming concepts and techniques.