Trang 2
Beginning Java Objects: From Concepts to Code, Second Edition Copyright © 2005 by Jacquie Barker All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN (pbk): 1-59059-457-6 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Steve Anglin Technical Reviewer: James Huddleston Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser Assistant Publisher: Grace Wong Project Manager: Beckie Stones Copy Edit Manager: Nicole LeClerc Copy Editors: Nicole LeClerc, Ami Knox Production Manager: Kari Brooks-Copony Production Editor: Katie Stence Compositor and Artist: Kinetic Publishing Services, LLC Proofreader: Sue Boshers Indexer: Broccoli Information Management Interior Designer: Van Winkle Design Group Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany. In the United States: phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders@springer-ny.com, or visit http://www.springer-ny.com. Outside the United States: fax +49 6221 345229, e-mail orders@springer.de, or visit http://www.springer.de. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Downloads section. www.traintelco.com
Trang 3
In loving memory of “the Weens”—sweet little Shylow and funny little Chloe— who graced our lives with unconditional love for almost 17 years. www.traintelco.com
Trang 4
Contents at a Glance About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi PART 1 ■ ■ ■ The ABCs of Objects CHAPTER 1 Abstraction and Modeling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 CHAPTER 2 Some Java Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 CHAPTER 3 Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 CHAPTER 4 Object Interactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 CHAPTER 5 Relationships Between Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 CHAPTER 6 Collections of Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 CHAPTER 7 Some Final Object Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 PART 2 ■ ■ ■ Object Modeling 101 CHAPTER 8 The Object Modeling Process in a Nutshell . . . . . . . . . . . . . . . . . . . . 333 CHAPTER 9 Formalizing Requirements Through Use Cases. . . . . . . . . . . . . . . . . 343 CHAPTER 10 Modeling the Static/Data Aspects of the System. . . . . . . . . . . . . . . 355 CHAPTER 11 Modeling the Dynamic/Behavioral Aspects of the System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 CHAPTER 12 Wrapping Up Our Modeling Efforts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 v www.traintelco.com
Trang 5
vi ■CONTENTS AT A GLANCE PART 3 ■ ■ ■ Translating an Object Blueprint into Java Code CHAPTER 13 Rounding Out Your Java Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 CHAPTER 14 Transforming Your Model into Java Code . . . . . . . . . . . . . . . . . . . . . . 567 CHAPTER 15 Rounding Out Your Application, Part 1: Adding a Data Access Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619 CHAPTER 16 Rounding Out Your Application, Part 2: Adding a Presentation Layer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 CHAPTER 17 SRS, Take 3: Adding a GUI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773 CHAPTER 18 Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831 PART 4 ■ ■ ■ Appendixes APPENDIX A Suggestions for Using This Book As a Textbook . . . . . . . . . . . . . . . . 839 APPENDIX B Alternative Case Studies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 APPENDIX C Setting Up Your Java Development Environment . . . . . . . . . . . . . . . 849 APPENDIX D Downloading and Compiling the Book’s Source Code . . . . . . . . . . 865 APPENDIX E Note to Experienced C++ Programmers . . . . . . . . . . . . . . . . . . . . . . . 867 APPENDIX F How Polymorphism Works Behind the Scenes (Static vs. Dynamic Binding) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873 APPENDIX G Collections Prior to J2SE 5.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879 APPENDIX H Programming 101, and the Role of a Compiler . . . . . . . . . . . . . . . . . 891 INDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897 www.traintelco.com
Trang 6
Contents About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi PART 1 ■ ■ ■ The ABCs of Objects ■CHAPTER 1 Abstraction and Modeling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Simplification Through Abstraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Generalization Through Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Organizing Abstractions into Classification Hierarchies . . . . . . . . . . . 5 Abstraction As the Basis for Software Development . . . . . . . . . . . . . . 8 Reuse of Abstractions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Inherent Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 What Does It Take to Be a Successful Object Modeler? . . . . . . . . . . 11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 ■CHAPTER 2 Some Java Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Why Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Java Is Architecture Neutral. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Java Provides “One-Stop Shopping” . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Java Is Object-Oriented from the Ground Up . . . . . . . . . . . . . . . . . . . 22 Practice Makes Perfect. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Java Is an Open Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Java Is Free! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 A Reminder Regarding Pseudocode vs. Real Java Code. . . . . . . . . . 24 Anatomy of a Simple Java Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 The Class Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The main Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 vii www.traintelco.com
Trang 7
viii ■CONTENTS The “Mechanics” of Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Compiling Java Source Code into Bytecode . . . . . . . . . . . . . . . . . . . . 30 Executing Bytecode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 A Behind-the-Scenes Look at the JVM . . . . . . . . . . . . . . . . . . . . . . . . 32 Primitive Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Variable Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Variable Initialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 The String Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Case Sensitivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Java Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Arithmetic Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Relational and Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Evaluating Expressions and Operator Precedence. . . . . . . . . . . . . . . 41 The Type of an Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Automatic Type Conversions and Explicit Casting . . . . . . . . . . . . . . . . . . . . 42 Loops and Other Flow-Control Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . 44 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 switch Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 for Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 while Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Jump Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Block-Structured Languages and the Scope of a Variable . . . . . . . . . . . . 53 Printing to the Screen. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 print vs. println. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Elements of Java Style. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Proper Use of Indentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Use Comments Wisely. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Placement of Braces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Descriptive Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 ■CHAPTER 3 Objects and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Software at Its Simplest. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Functional Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 The Object-Oriented Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 What Is an Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 State/Data/Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Behavior/Operations/Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 www.traintelco.com
Trang 8
■CONTENTS ix What Is a Class? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 A Note Regarding Naming Conventions. . . . . . . . . . . . . . . . . . . . . . . . 73 Declaring a Class, Java Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Encapsulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 User-Defined Types and Reference Variables. . . . . . . . . . . . . . . . . . . . . . . . 76 Naming Conventions for Reference Variables. . . . . . . . . . . . . . . . . . . 77 Instantiating Objects: A Closer Look . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Objects As Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 A Compilation Trick: “Stubbing Out” Classes . . . . . . . . . . . . . . . . . . . 88 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 The Advantages of References As Attributes . . . . . . . . . . . . . . . . . . . 91 Three Distinguishing Features of an Object-Oriented Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 ■CHAPTER 4 Object Interactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Events Drive Object Collaboration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Declaring Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Method Headers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Method Naming Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Passing Arguments to Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Method Return Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 An Analogy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Method Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Features May Be Declared in Any Order . . . . . . . . . . . . . . . . . . . . . . 103 return Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Methods Implement Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Objects As the Context for Method Invocation . . . . . . . . . . . . . . . . . . . . . . 108 Java Expressions, Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Capturing the Value Returned by a Method . . . . . . . . . . . . . . . . . . . 111 Method Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Choosing Descriptive Method Names . . . . . . . . . . . . . . . . . . . . . . . . 114 Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Message Passing Between Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Delegation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Obtaining Handles on Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Objects As Clients and Suppliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 www.traintelco.com
Trang 9
x ■CONTENTS Information Hiding/Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Public Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Private Accessibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Publicizing Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Method Headers, Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Accessing the Features of a Class from Within Its Own Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Accessing Private Features from Client Code. . . . . . . . . . . . . . . . . . . . . . . 132 Declaring Accessor Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Recommended “Get”/“Set” Method Headers. . . . . . . . . . . . . . . . . . 134 The “Persistence” of Attribute Values. . . . . . . . . . . . . . . . . . . . . . . . . 137 Using Accessor Methods from Client Code . . . . . . . . . . . . . . . . . . . . 137 The Power of Encapsulation Plus Information Hiding . . . . . . . . . . . . . . . . 138 Preventing Unauthorized Access to Encapsulated Data . . . . . . . . . 139 Helping to Ensure Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Limiting “Ripple Effects” When Private Features Change. . . . . . . . 140 Using Accessor Methods from Within a Class’s Own Methods . . . 143 Exceptions to the Public/Private Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Exception #1: Internal Housekeeping Attributes. . . . . . . . . . . . . . . . 147 Exception #2: Internal Housekeeping Methods. . . . . . . . . . . . . . . . . 148 Exception #3: “Read-Only” Attributes. . . . . . . . . . . . . . . . . . . . . . . . . 149 Exception #4: Public Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Default Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Writing Our Own Explicit Constructors. . . . . . . . . . . . . . . . . . . . . . . . 151 Passing Arguments to Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Replacing the Default Parameterless Constructor . . . . . . . . . . . . . . 153 More Elaborate Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Overloading Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 An Important Caveat Regarding the Default Constructor . . . . . . . . 157 Using the “this” Keyword to Facilitate Constructor Reuse . . . . . . . 158 Software at Its Simplest, Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 ■CHAPTER 5 Relationships Between Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Associations and Links. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Multiplicity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Multiplicity and Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Aggregation and Composition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 www.traintelco.com
Trang 10
■CONTENTS xi Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Responding to Shifting Requirements with a New Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 (Inappropriate) Approach #1: Modify the Student Class . . . . . . . . . 176 (Inappropriate) Approach #2: “Clone” the Student Class to Create a GraduateStudent Class . . . . . . . . . . . . . . . . . . . . . . . . 179 The Proper Approach (#3): Taking Advantage of Inheritance . . . . . 180 The “is a” Nature of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 The Benefits of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Class Hierarchies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 The Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Is Inheritance Really a Relationship? . . . . . . . . . . . . . . . . . . . . . . . . . 186 Avoiding “Ripple Effects” in a Class Hierarchy. . . . . . . . . . . . . . . . . 187 Rules for Deriving Classes: The “Do’s” . . . . . . . . . . . . . . . . . . . . . . . 187 Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Reusing Superclass Behaviors: The “super” Keyword . . . . . . . . . . 191 Rules for Deriving Classes: The “Don’ts”. . . . . . . . . . . . . . . . . . . . . . 194 Private Features and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Inheritance and Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 A Few Words About Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . 205 Three Distinguishing Features of an OOPL, Revisited. . . . . . . . . . . . . . . . 209 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 ■CHAPTER 6 Collections of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 What Are Collections?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Collections Are Defined by Classes and Must Be Instantiated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Collections Organize References to Other Objects. . . . . . . . . . . . . . 214 Collections Are Encapsulated. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Three Generic Types of Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Ordered Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Dictionaries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Arrays As Simple Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Declaring and Instantiating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Accessing Individual Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . 221 Initializing Array Contents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Manipulating Arrays of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 www.traintelco.com
Trang 11
xii ■CONTENTS A More Sophisticated Type of Collection: The ArrayList Class . . . . . . . . . 227 Using the ArrayList Class: An Example. . . . . . . . . . . . . . . . . . . . . . . . 228 Import Directives and Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 The Namespace of a Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 User-Defined Packages and the Default Package . . . . . . . . . . . . . . 233 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 ArrayList Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Iterating Through ArrayLists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Copying the Contents of an ArrayList into an Array . . . . . . . . . . . . . 238 The HashMap Collection Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 The TreeMap Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 The Same Object Can Be Simultaneously Referenced by Multiple Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Inventing Our Own Collection Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Approach #1: Designing a New Collection Class from Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Approach #2: Extending a Predefined Collection Class (MyIntCollection). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Approach #3: Encapsulating a Standard Collection (MyIntCollection2). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Trade-offs of Approach #2 vs. Approach #3 . . . . . . . . . . . . . . . . . . . 258 Collections As Method Return Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Collections of Derived Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Revisiting Our Student Class Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 The courseLoad Attribute of Student . . . . . . . . . . . . . . . . . . . . . . . . . 262 The transcript Attribute of Student . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 The transcript Attribute, Take 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Our Completed Student Data Structure . . . . . . . . . . . . . . . . . . . . . . . 270 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 ■CHAPTER 7 Some Final Object Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Polymorphism Simplifies Code Maintenance . . . . . . . . . . . . . . . . . . 279 Three Distinguishing Features of an Object-Oriented Programming Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 The Benefits of User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . . 281 The Benefits of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 The Benefits of Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 www.traintelco.com
Trang 12
■CONTENTS xiii Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Implementing Abstract Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Abstract Classes and Instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Declaring Reference Variables of Abstract Types . . . . . . . . . . . . . . . 289 An Interesting Twist on Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . 289 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Implementing Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Another Form of the “Is A” Relationship. . . . . . . . . . . . . . . . . . . . . . . 296 Interfaces and Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Implementing Multiple Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Interfaces and Casting, Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Interfaces and Instantiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Interfaces and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 The Importance of Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Static Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 Static Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 A Design Improvement: Burying Implementation Details . . . . . . . . 317 Static Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Restrictions on Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 Revisiting the Syntax of Print Statements . . . . . . . . . . . . . . . . . . . . . 321 Utility Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 The final Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 The Static Import Facility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 Custom Utility Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 PART 2 ■ ■ ■ Object Modeling 101 ■CHAPTER 8 The Object Modeling Process in a Nutshell . . . . . . . . . . . . . . 333 The “Big Picture” Goal of Object Modeling . . . . . . . . . . . . . . . . . . . . . . . . . 333 Modeling Methodology = Process + Notation + Tool . . . . . . . . . . . 334 Jacquie’s Recommended Object Modeling Process, in a Nutshell . . . . . 337 Thoughts Regarding Object Modeling Software Tools. . . . . . . . . . . 338 A Reminder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 www.traintelco.com
Trang 13
xiv ■CONTENTS ■CHAPTER 9 Formalizing Requirements Through Use Cases. . . . . . . . . . 343 What Are Use Cases? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Functional vs. Technical Requirements . . . . . . . . . . . . . . . . . . . . . . . 344 Involving the Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Actors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 Identifying Actors and Determining Their Roles . . . . . . . . . . . . . . . . 346 Diagramming a System and Its Actors. . . . . . . . . . . . . . . . . . . . . . . . 347 Specifying Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Matching Up Use Cases with Actors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 To Diagram or Not to Diagram? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 ■CHAPTER 10 Modeling the Static/Data Aspects of the System. . . . . . . . 355 Identifying Appropriate Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Noun Phrase Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Refining the Candidate Class List . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Revisiting the Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 Producing a Data Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Determining Associations Between Classes. . . . . . . . . . . . . . . . . . . . . . . . 368 Identifying Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 UML Notation: Modeling the Static Aspects of an Abstraction. . . . . . . . . 372 Classes, Attributes, and Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 372 Relationships Between Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 Reflecting Multiplicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Object Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Associations As Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 Information “Flows” Along an Association “Pipeline” . . . . . . . . . . . . . . . . 389 “Mixing and Matching” Relationship Notations . . . . . . . . . . . . . . . . . . . . . 395 Association Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Our “Completed” Student Registration System Class Diagram. . . . . . . . 400 Metadata. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 ■CHAPTER 11 Modeling the Dynamic/Behavioral Aspects of the System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 How Behavior Affects State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412 www.traintelco.com
Trang 14
■CONTENTS xv Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Scenario #1 for the “Register for a Course” Use Case . . . . . . . . . . 416 Scenario #2 for the “Register for a Course” Use Case . . . . . . . . . . 418 Sequence Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 Using Sequence Diagrams to Determine Methods . . . . . . . . . . . . . . . . . . 424 Communication Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 Revised SRS Class Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 ■CHAPTER 12 Wrapping Up Our Modeling Efforts . . . . . . . . . . . . . . . . . . . . . . . 431 Testing the Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 Revisiting Requirements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 Reusing Models: A Word About Design Patterns . . . . . . . . . . . . . . . . . . . . 434 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 PART 3 ■ ■ ■ Translating an Object Blueprint into Java Code ■CHAPTER 13 Rounding Out Your Java Knowledge. . . . . . . . . . . . . . . . . . . . . . 441 Java-Specific Terminology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 Java Application Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444 Java Archive (JAR) Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 Creating a JAR File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 Inspecting the Contents of a JAR File. . . . . . . . . . . . . . . . . . . . . . . . . 448 Using the Bytecode Contained Within a JAR File . . . . . . . . . . . . . . . 448 Extracting Content from a JAR File. . . . . . . . . . . . . . . . . . . . . . . . . . . 449 “Jarring” Entire Directory Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . 449 Javadoc Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450 The Object Nature of Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 Operations on Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 Strings Are Immutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 The StringBuffer Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 The StringTokenizer Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462 Instantiating Strings and the String Literal Pool. . . . . . . . . . . . . . . . 465 Testing the Equality of Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469 Message Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470 Object Self-Referencing with “this”. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 www.traintelco.com
Trang 15
xvi ■CONTENTS Java Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473 The Mechanics of Exception Handling. . . . . . . . . . . . . . . . . . . . . . . . 475 Catching Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484 Interpreting Exception Stack Traces. . . . . . . . . . . . . . . . . . . . . . . . . . 489 The Exception Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490 Catching the Generic Exception Type. . . . . . . . . . . . . . . . . . . . . . . . . 493 Compiler Enforcement of Exception Handling. . . . . . . . . . . . . . . . . . 494 Taking Advantage of the Exception That We’ve “Caught”. . . . . . . . 496 Nesting of try/catch Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496 User-Defined Exception Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 Throwing Multiple Types of Exceptions . . . . . . . . . . . . . . . . . . . . . . . 500 Enum(eration)s. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501 Enumerating Choices Prior to J2SE 5.0. . . . . . . . . . . . . . . . . . . . . . . 508 Providing Input to Command Line–Driven Programs . . . . . . . . . . . . . . . . 509 Accepting Command-Line Arguments: The args Array . . . . . . . . . . 510 Introducing Custom Command-Line Flags to Control a Program’s Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 Using the Wrapper Classes for Input Conversion . . . . . . . . . . . . . . . 516 Accepting Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518 Features of the Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522 Determining the Class That an Object Belongs To . . . . . . . . . . . . . . 522 Testing the Equality of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523 Overriding the equals Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 Overriding the toString Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530 A Deeper Look Behind the Scenes of the JVM. . . . . . . . . . . . . . . . . . . . . . 532 The JVM’s Class Loader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532 The -verbose Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533 The Static Nature of main(...) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 Import Directives, Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 Static Initializers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 The Date Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541 Accessibility, Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 Default Accessibility of a Feature . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 Public vs. Nonpublic Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 Variable Initialization, Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552 Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554 Additional J2SE 5.0 Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 Formatted Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558 Formatted Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559 Variable Arguments (Varargs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564 www.traintelco.com
Trang 16
■CONTENTS xvii ■CHAPTER 14 Transforming Your Model into Java Code . . . . . . . . . . . . . . . . 567 Suggestions for Getting the Maximum Value from This and Subsequent Chapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567 The SRS Class Diagram Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 The Person Class (Specifying Abstract Classes). . . . . . . . . . . . . . . . 571 The Student Class (Reuse Through Inheritance, Abstract Class Extension, Delegation). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574 The Professor Class (Bidirectional Relationships) . . . . . . . . . . . . . . 582 The Course Class (Reflexive Relationships, Unidirectional Relationships) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584 The Section Class (Representing Association Classes, Public Static Final Attributes, Enums) . . . . . . . . . . . . . . . . . . . . . . 587 Delegation Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595 The ScheduleOfClasses Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600 The TranscriptEntry Association Class (Static Methods) . . . . . . . . . 602 The Transcript Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605 The SRS Driver Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606 The Importance of Model–View and Model–Data Layer Separation. . . . 617 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618 ■CHAPTER 15 Rounding Out Your Application, Part 1: Adding a Data Access Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619 An Overview of Upcoming SRS Enhancements . . . . . . . . . . . . . . . . . . . . . 621 Approaches to Object Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621 The Basics of File I/O in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624 Reading from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624 Writing to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626 Exception Handling with File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629 Populating the Main SRS Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 The ScheduleOfClasses Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 Two New Encapsulated Collection Classes: CourseCatalog and Faculty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 Initializing the SRS Collections from Record-Oriented Data Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634 Persisting Student Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 637 Read vs. Write Access to the Data Files . . . . . . . . . . . . . . . . . . . . . . 639 Configuring Applications with the Java Properties Class . . . . . . . . . . . . . 639 The FileNames.properties File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640 Accessing System Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641 www.traintelco.com
Trang 17
xviii ■CONTENTS Defining Custom Exceptions for the SRS. . . . . . . . . . . . . . . . . . . . . . . . . . . 644 Encapsulating Persistence Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 Introducing the SRSDataAccess Class. . . . . . . . . . . . . . . . . . . . . . . . 645 Initializing the ScheduleOfClasses Collection . . . . . . . . . . . . . . . . . . 648 Initializing the Faculty Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652 Initializing the CourseCatalog Collection . . . . . . . . . . . . . . . . . . . . . . 655 Initializing a Student’s State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657 Persisting the State of a Student. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660 Streamlining the SRS Driver Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 661 The Importance of Model–Data Access Layer Separation . . . . . . . . . . . . 669 Objects and Databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670 Using the JDBC API: A Conceptual Overview. . . . . . . . . . . . . . . . . . . 671 Retrofitting the SRS with Database Access . . . . . . . . . . . . . . . . . . . 674 Our SRS Modifications, Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679 ■CHAPTER 16 Rounding Out Your Application, Part 2: Adding a Presentation Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 Java GUIs: a Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682 Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682 Containers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683 Model–View Separation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684 AWT vs. Swing Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688 Crafting the View/Presentation of a Java GUI. . . . . . . . . . . . . . . . . . . . . . . 692 JFrames. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692 Positioning a Frame on the Screen. . . . . . . . . . . . . . . . . . . . . . . . . . . 696 Explicitly Positioning a Frame on the Screen . . . . . . . . . . . . . . . . . . 697 Centering a Frame on the Screen. . . . . . . . . . . . . . . . . . . . . . . . . . . . 698 Adding Components to a JFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700 JPanels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702 Common Component Properties and Behaviors. . . . . . . . . . . . . . . . 702 Layout Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706 BorderLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706 GridLayout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711 FlowLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717 JLabels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719 JTextFields and JPasswordFields . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719 JButtons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722 JLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723 www.traintelco.com
Trang 18
■CONTENTS xix A Simple Calculator Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726 An Improved Application Architecture for GUIs. . . . . . . . . . . . . . . . . . . . . . 729 Adding a “Test Scaffold” main() Method . . . . . . . . . . . . . . . . . . . . . . 733 Other Interesting AWT/Swing Components to Explore . . . . . . . . . . . . . . . 734 Java Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735 Events: Basic Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735 Basic Event Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738 Creating and Registering Listeners. . . . . . . . . . . . . . . . . . . . . . . . . . . 739 Adding Behavior to Our Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . 742 Combining Listeners: Utilizing the ActionEvent. . . . . . . . . . . . . . . . . 746 Closing a Window, Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748 Adapter Classes vs. Listener Interfaces. . . . . . . . . . . . . . . . . . . . . . . 751 Selecting an Item from a JList. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754 More Container Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761 JDialog. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761 One-Step Dialog Boxes with JOptionPane. . . . . . . . . . . . . . . . . . . . . 766 Reusable Custom Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771 ■CHAPTER 17 SRS, Take 3: Adding a GUI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773 Our SRS Code Road Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773 Preparing a Concept of Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775 The SRS Concept of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776 The MainFrame Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783 Planning the “Look” of MainFrame: Getting Creative with GridLayout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783 Coding the “Look” of MainFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784 Adding Behaviors to MainFrame. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789 The PasswordPopup Class: Sharing Information Across Windows/Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803 The SRS Driver Class, Significantly Streamlined . . . . . . . . . . . . . . . . . . . . 805 An Overview of J2EE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806 The Downside of Desktop Application Deployment. . . . . . . . . . . . . 807 The Upside of Web Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808 What Is J2EE?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813 What Is a Servlet? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814 What Is a JavaServer Page? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816 J2EE, Model View Controller–Style. . . . . . . . . . . . . . . . . . . . . . . . . . . 819 N-Tier Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822 www.traintelco.com
Trang 19
xx ■CONTENTS What Are EJBs? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822 Dispelling J2EE Myths. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825 The Relevance of What You’ve Learned in This Book to J2EE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826 Review of the Architecture of an “Industrial-Strength” Java Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829 ■CHAPTER 18 Next Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831 Jacquie’s “Tried and True” Approach to Learning Java Properly. . . . . . . 832 Taming the Technology Tidal Wave. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833 Other Recommended Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834 Your Comments, Please! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835 PART 4 ■ ■ ■ Appendixes ■APPENDIX A Suggestions for Using This Book As a Textbook. . . . . . . . . 839 Recommended Teaching Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839 Suitability of Java As a Teaching Language . . . . . . . . . . . . . . . . . . . . . . . . 840 Some Final Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840 ■APPENDIX B Alternative Case Studies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 Case Study #1: Prescription Tracking System . . . . . . . . . . . . . . . . . . . . . . 843 Background. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843 Simplifying Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844 Case Study #2: Conference Room Reservation System . . . . . . . . . . . . . . 845 Background. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845 Goals for the System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846 Case Study #3: Blue Skies Airline Reservation System . . . . . . . . . . . . . . 847 Background. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847 Other Simplifying Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848 ■APPENDIX C Setting Up Your Java Development Environment . . . . . . . . 849 The Java Software Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 849 Testing Your Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850 www.traintelco.com
Trang 20
■CONTENTS xxi Troubleshooting Your Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852 PATH and CLASSPATH Settings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852 Common Compilation Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857 Common Run-Time Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860 Using the Online Java Documentation with Windows. . . . . . . . . . . . . . . . 861 Special Tips for Using Microsoft Windows Command Prompts. . . . . . . . 862 Capturing Program Output to a File . . . . . . . . . . . . . . . . . . . . . . . . . . 863 Resizing the Command Prompt Window . . . . . . . . . . . . . . . . . . . . . . 863 ■APPENDIX D Downloading and Compiling the Book’s Source Code. . . 865 ■APPENDIX E Note to Experienced C++ Programmers. . . . . . . . . . . . . . . . . . 867 No More Pointers! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867 Dynamic vs. Static Object Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869 “Breaking the OO Rules” with C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869 Platform Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870 Abstract Methods and Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 870 Other Simplifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870 ■APPENDIX F How Polymorphism Works Behind the Scenes (Static vs. Dynamic Binding) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873 Static Binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874 Dynamic Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875 ■APPENDIX G Collections Prior to J2SE 5.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879 Summary of 5.0 Collection Enhancements. . . . . . . . . . . . . . . . . . . . . . . . . 879 Constraining Collection Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880 Iterating Through Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881 Managing Primitive Types with Collections. . . . . . . . . . . . . . . . . . . . . . . . . 884 The Iterator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886 Using the -Xlint:unchecked Compiler Option . . . . . . . . . . . . . . . . . . . . . . . 888 ■APPENDIX H Programming 101, and the Role of a Compiler. . . . . . . . . . . 891 A LEGOs Analogy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 891 The Compiler’s Job in a Nutshell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 892 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897 www.traintelco.com
Hiển thị 20 trang đầu. Tải tài liệu để xem đầy đủ.
Bình luận (0)
Chưa có bình luận nào. Hãy là người đầu tiên!