Java, Java, Java™: Object-Oriented Problem Solving,Third Edition Table of Contents Copyright Preface What's New in the ThirdEdition?
Why Start with Objects? Organization of the Text
Chapter 0 Computers, Objects,and Java
Trang 3Section 0.2 What Is aComputer?
Section 0.3 Networks, theInternet, and the World WideWeb Section 0.4 Why StudyProgramming? Section 0.5 ProgrammingLanguages
Section 0.6 Why Java? Section 0.7 What Is Object-Oriented Programming? Chapter Summary
Trang 4Chapter 1 Java ProgramDesign and Development Section 1.1 Introduction Section 1.2 Designing GoodPrograms Section 1.3 Designing a RiddleProgram Section 1.4 Java LanguageElements
Section 1.5 Editing, Compiling,and Running a Java Program Section 1.6 From the JavaLibrary: System and
Trang 5Chapter Summary Solutions to Self-StudyExercises
Exercises
Chapter 2 Objects: Using,Creating, and Defining Section 2.1 Introduction Section 2.2 Using StringObjects
Section 2.3 Drawing Shapeswith a Graphics Object
(Optional)
Trang 7Section 3.4 Retrieving
Trang 9Section 4.5 Case Study: TheOne-Row Nim Game
Trang 11Representation + Action Chapter Summary Solutions to Self-StudyExercises Exercises Chapter 6 Control Structures Section 6.1 Introduction Section 6.2 Flow of Control:Repetition Structures
Section 6.3 Counting Loops Section 6.4 Example: Car LoanSection 6.5 Graphics Example:Drawing a Checkerboard
Trang 12Section 6.7 Example:Computing Averages Section 6.8 Example: DataValidation Section 6.9 Principles of LoopDesign
Section 6.10 The switch
Trang 13Exercises
Chapter 7 Strings and StringProcessing
Trang 14Processing Names andPasswords
Section 7.8 Processing EachCharacter in a String
Section 7.9 Comparing Strings Section 7.10 From the JavaLibrary:
java.util.StringTokenizer
Section 7.11 Handling Text ina Graphics Context (Optional) Chapter Summary
Solutions to Self-StudyExercises
Trang 15Chapter 8 Inheritance andPolymorphism
Section 8.1 Introduction
Section 8.2 Java's InheritanceMechanism
Section 8.3 Abstract Classes,Interfaces, and Polymorphism Section 8.4 Example: A ToggleButton
Trang 19Section 10.4 Handling
Trang 20Chapter 11 Files and Streams:Input/Output Techniques
Section 11.1 Introduction
Section 11.2 Streams and Files Section 11.3 Case Study:
Trang 24Designing a Basic GUI
Section 13.7 Containers andLayout Managers
Trang 26Solutions to Self-StudyExercises Exercises Chapter 15 Sockets andNetworking Section 15.1 Introduction Section 15.2 An Overview ofNetworks
Section 15.3 Using NetworkResources from an Applet Section 15.4 From the JavaLibrary: java.net.URL
Trang 27Section 15.6 Using NetworkResources from an Application Section 15.7 Client/Server
Communication via Sockets Section 15.8 CASE STUDY:Generic Client/Server Classes Section 15.9 Playing One-RowNim Over the Network
Section 15.10 Java NetworkSecurity Restrictions
Section 15.11 Java Servlets andJava Server Pages
Trang 28Exercises Exercises
Chapter 16 Data Structures:Lists, Stacks, and Queues Section 16.1 Introduction Section 16.2 The Linked ListData Structure Section 16.3 Object-OrientedDesign: The List Abstract DataType (ADT)
Trang 29Framework and Generic Types Section 16.7 Using the Set andMap Interfaces
Trang 30Use of Braces File Names and Layout Statements Executable Statements Preconditions andPostconditions Sample Programs Appendix B The JavaDevelopment Kit
The Java Compiler: javac The Java Interpreter: java The appletviewer
Trang 31javadoc
Appendix C The ASCII andUnicode Character Sets
Appendix D Java Keywords Appendix E Operator
Precedence Hierarchy
Appendix F Java Inner Classes What Are Inner Classes?
Trang 35• Table ofContents• IndexJava, Java, Java™: Object-Oriented Problem Solving,Third Edition
By Ralph Morelli, Ralph Walde - Trinity College
Publisher: Prentice Hall
Pub Date : December 22, 2005Print ISBN-10: 0-13-147434-0Print ISBN-13: 978-0-13-147434-5eText ISBN-10: 0-13-185995-1eText ISBN-13: 978-0-13-185995-1Pages: 880
This text takes an objects-first approach to programming and problem solving
using Java A flexible design allows instructors to choose between
Trang 36• Table ofContents• IndexJava, Java, Java™: Object-Oriented Problem Solving,Third Edition
By Ralph Morelli, Ralph Walde - Trinity College
Publisher: Prentice Hall
Trang 37What's New in the Third Edition?xiii
Why Start with Objects?xiv
Organization of the Textxv
Chapter 0 Computers, Objects, and Java1
Section 0.1 Welcome2
Section 0.2 What Is a Computer?2 Section 0.3 Networks, the Internet, and
the World Wide Web 3
Section 0.4 Why Study Programming?6 Section 0.5 Programming Languages6
Section 0.6 Why Java?8
Section 0.7 What Is Object-OrientedProgramming? 10 Chapter Summary18 Exercises20 Chapter 1 Java Program Design andDevelopment 23 Section 1.1 Introduction24
Trang 38Section 1.5 Editing, Compiling, and
Running a Java Program 48
Section 1.6 From the Java Library:
System and PrintStream 52 Chapter Summary54 Solutions to Self-Study Exercises56 Exercises57 Chapter 2 Objects: Using, Creating, andDefining 63 Section 2.1 Introduction64
Section 2.2 Using String Objects64 Section 2.3 Drawing Shapes with a
Graphics Object (Optional) 68
Trang 39Chapter 3 Methods: Communicating with
Objects 103
Section 3.1 Introduction104
Section 3.2 Passing Information to an
Object 104
Section 3.3 Constructor Methods111 Section 3.4 Retrieving Information froman Object 115 Section 3.5 Passing a Value andPassing a Reference 118 Section 3.6 Flow of Control: ControlStructures 121 Section 3.7 Testing an ImprovedOneRowNim 129 Section 3.8 From the Java Library:java.lang.Object 133
Section 3.9 Object-Oriented Design:
Inheritance and Polymorphism 135
Section 3.10 Drawing Lines and
Defining Graphical Methods (Optional) 136
Trang 40Solutions to Self-Study Exercises140
Exercises142
Chapter 4 Input/Output: Designing the
User Interface 149
Section 4.1 Introduction150
Section 4.2 The User Interface150 Section 4.3 A Command-Line Interface151 Section 4.4 A Graphical User Interface
(GUI) 159
Section 4.5 Case Study: The One-Row
Nim Game 175
Section 4.6 From the Java Library:
Trang 42Section 6.4 Example: Car Loan260 Section 6.5 Graphics Example: Drawing
a Checkerboard 262
Section 6.6 Conditional Loops266 Section 6.7 Example: Computing
Averages 272
Section 6.8 Example: Data Validation275 Section 6.9 Principles of Loop Design277 Section 6.10 The switch MultiwaySelection Structure 278 Section 6.11 Object-Oriented Design:Structured Programming 282 Chapter Summary290 Solutions to Self-Study Exercises291 Exercises298 Chapter 7 Strings and String Processing305 Section 7.1 Introduction306
Section 7.2 String Basics306 Section 7.3 Finding Things within a
String 313
Trang 43Section 7.5 From the Java Library:java.lang.StringBuffer
317 Section 7.6 Retrieving Parts of Strings320 Section 7.7 Example: Processing
Names and Passwords 322
Section 7.8 Processing Each Character
in a String 323
Section 7.9 Comparing Strings326 Section 7.10 From the Java Library:
java.util.StringTokenizer 332
Section 7.11 Handling Text in a
Graphics Context (Optional) 334 Chapter Summary340 Solutions to Self-Study Exercises341 Exercises344 Chapter 8 Inheritance and Polymorphism349 Section 8.1 Introduction350 Section 8.2 Java's InheritanceMechanism 350
Section 8.3 Abstract Classes,
Interfaces, and Polymorphism 359
Trang 44Section 8.5 Example: The Cipher ClassHierarchy 368 Section 8.6 Case Study: A Two-PlayerGame Hierarchy 375 Section 8.7 Principles of Object-Oriented Design 396 Chapter Summary397 Solutions to Self-Study Exercises398 Exercises400 Chapter 9 Arrays and Array Processing405 Section 9.1 Introduction406
Section 9.2 One-Dimensional Arrays406 Section 9.3 Simple Array Examples413 Section 9.4 Example: Counting
Frequencies of Letters 416
Section 9.5 Array Algorithms: Sorting420 Section 9.6 Array Algorithms:
Searching 426
Trang 45Section 9.9 Object-Oriented Design:
Polymorphic Sorting (Optional) 439
Section 9.10 From the Java Library:
java.lang.Vector 443
Section 9.11 CASE STUDY: An
N-Player Computer Game 444
Trang 46Section 10.6 Creating and Throwing
Your Own Exceptions 497
Section 10.7 From the Java Library:javax.swing.JOptionPane 501 Chapter Summary503 Solutions to Self-Study Exercises505 Exercises507 Chapter 11 Files and Streams: Input/OutputTechniques 511 Section 11.1 Introduction512 Section 11.2 Streams and Files512 Section 11.3 Case Study: Reading and
Writing Text Files 518
Section 11.4 The File Class530 Section 11.5 Example: Reading and
Writing Binary Files 533
Section 11.6 Object Serialization:
Reading and Writing Objects 542
Section 11.7 From the Java Library:
Trang 48Chapter 13 Graphical User Interfaces603 Section 13.1 Introduction604 Section 13.2 Java GUIs: From AWT to
Swing 604
Section 13.3 The Swing Component Set607 Section 13.4 Object-Oriented Design:
Model-View-Controller Architecture 607
Section 13.5 The Java Event Model609 Section 13.6 Case Study: Designing a
Basic GUI 614
Section 13.7 Containers and Layout
Managers 627
Section 13.8 Checkboxes, Radio
Buttons, and Borders 633
Trang 50java.net.URL
Section 15.5 The Slide-Show Applet717 Section 15.6 Using Network Resources
from an Application 722
Section 15.7 Client/Server
Communication via Sockets 732
Section 15.8 CASE STUDY: Generic
Client/Server Classes 736
Section 15.9 Playing One-Row Nim
Over the Network 745
Trang 51Section 16.3 Object-Oriented Design:
The List Abstract Data Type (ADT) 780
Section 16.4 The Stack ADT785 Section 16.5 The Queue ADT788
Trang 52Executable Statements817 Preconditions and Postconditions818
Sample Programs818
Appendix B The Java Development Kit819
The Java Compiler: javac819
The Java Interpreter: java821
The appletviewer821
The Java Archiver jar Tool825
The Java Documentation Tool: javadoc827 Appendix C The ASCII and Unicode
Character Sets 828
Appendix D Java Keywords829 Appendix E Operator Precedence
Hierarchy 830
Appendix F Java Inner Classes832
What Are Inner Classes?832
Nested Top-Level Versus Member
Classes 832
Trang 54Copyright[Page ii]Library of Congress Cataloging-in-Publication DataMorelli, R (Ralph) Java, Java, Java : object oriented problem solving / R Morelli and R Walde 3rd ed p cm ISBN 0-13-147434-0 1
Object-oriented programming (Computer science) 2 Java (Computer programlanguage I Walde, R (Ralph) II Title.
Trang 55005.13'3dc21 2005054959Vice President and Editorial
Trang 56Director for Creative Services:
Paul Belfanti
Creative Director: Juan LopezCover Designer: Bruce Kenselaar
Managing Editor, AV Management
and Production: Patricia BurnsArt Editor: Xiaohong ZhuManufacturing Manager: Alexis
Heydt-Long
Manufacturing Buyer: Lisa
Trang 57Marketing Manager: Robin O'BrienMarketing Assistant: Barrie
Reinhold
© 2006 Pearson Education, Inc.Pearson Prentice Hall
Pearson Education, Inc.
Upper Saddle River, New Jersey 07458All rights reserved No part of this bookmay be reproduced, in any format or byany means, without permission in writingfrom the publisher.
Trang 58used their best efforts in preparing thisbook These efforts include the
development, research, and testing of thetheories and programs to determine theireffectiveness The author and publishermake no warranty of any kind, expressedor implied, with regard to these programsor the documentation contained in thisbook The author and publisher shall notbe liable in any event for incidental orconsequential damages in connection with,or arising out of, the furnishing,
performance, or use of these programs.Pearson Prentice Hall™ is a trademark ofPearson Education, Inc.
Trang 59Microsystems, Inc 4150 Network Circle,Santa Clara, CA 95054.
Printed in the United States of America10 9 8 7 6 5 4 3 2 1
Pearson Education Ltd., London
Pearson Education Australia Pty Ltd., Sydney
Pearson Education Singapore, Pte Ltd.
Pearson Education North Asia Ltd., Hong KongPearson Education Canada Inc., Toronto
Pearson Educatíon de Mexico, S.A de C.V.
Pearson EducationJapan, Tokyo
Pearson Education Malaysia, Pte Ltd.
Pearson Education, Upper Saddle River, New Jersey
Trang 60Dedication
Trang 62Preface
We have designed this third edition of
Java, Java, Java to be suitable for a
typical Introduction to Computer Science(CS1) course or for a slightly moreadvanced Java as a Second Languagecourse This edition retains the "objectsfirst" approach to programming and
problem solving that was characteristic ofthe first two editions Throughout the textwe emphasize careful coverage of Javalanguage features, introductory
programming concepts, and object-oriented design principles.
Trang 63including:
Early Introduction of Objects
Emphasis on Object Oriented Design(OOD)
Unified Modeling Language (UML)Diagrams
Self-study Exercises with AnswersProgramming, Debugging, andDesign Tips
Trang 64Object-Oriented Design Sections
End-of-Chapter Exercises
Companion Web Site, with PowerPoints and other Resources
The In the Laboratory sections from the
first two editions have been moved ontothe book's Companion Web Site Table 1
shows the Table of Contents for the thirdedition.
Table 1 Table of Contents for theThird Edition.
Trang 65Chapter 0Computers, Objects, and Java (revised)
Chapter 1Java Program Design and Development
Chapter 2Objects: Defining, Creating, and Using
Chapter 3 Methods: Communicating with Objects
(revised)
Chapter 4 Input/Output: Designing the User Interface
(new)
Chapter 5Java Data and Operators
Chapter 6Control Structures
Trang 66Chapter 8Inheritance and Polymorphism (new)
Chapter 9Arrays and Array Processing
Chapter 10Exceptions: When Things Go Wrong
Chapter 11Files and Streams
Chapter 12Recursive Problem Solving
Chapter 13Graphical User Interfaces
Chapter 14Threads and Concurrent Programming
Chapter 15Sockets and Networking (expanded)
Chapter 16 Data Structures: Lists, Stacks, and Queues
Trang 68[Page xiii (continued)]
What's New in the ThirdEdition?
The third edition has the followingsubstantive changes:
Although the book retains its
Trang 69[Page xiv]
Chapters 0 (Computers, Objects, andJava) and 1 (Java Program Designand Development) have beensubstantially reorganized andrewritten The new presentation isdesigned to reduce the pace withwhich new concepts are introduced.The treatment of object-oriented(OO) and UML concepts has alsobeen simplified, and some of themore challenging OO topics, such aspolymorphism, have been moved to anew Chapter 8.