Table Of ContentSummary of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
PART 1: The ABC's of Objects 11
Chapter 1: A Little Taste of Java . . 13
Chapter 2: Abstraction and Modeling . . . 47
Chapter 3: Objects and Classes. . . . . 59
Chapter 4: Object Interactions . . . . . . . . 81
Chapter 5: Relationships Between Objects. 113
Chapter 6: Collections of Objects . 143
Chapter 7: Some Final Concepts. . . . . . . 163
PART 2: Object Modeling 10 1 189
Chapter 8: The Object Modeling Process in a Nutshell . . 191
Chapter 9: Formalizing Requirements through Use Cases . . . 201
Chapter 10: Modeling the Static I Data Aspects of the System . . 213
Chapter 11: Modeling the Dynamic I Behavioral Aspects of the System . 263
Chapter 12: Wrapping Up Our Modeling Efforts . . . . . . . . . . 285
PART 3: Translating an Object 'Blueprint' into Java Code 293
Chapter 13: A Deeper Look At Java . . . . . . . . . . . . . . . . . . . . . 295
Chapter 14: Transforming Your Model into Java Code ............ 375
Chapter 15: Rounding Out Your Application Part 1: Adding File Persistence. . 427
Chapter 16: Rounding Out Your Application Part 2: Adding a GUI. . 471
Chapter 17: Next Steps. . . . . . . . . . . . . . . . . . . . 601
PART 4: Appendices 607
Appendix A: Suggestions for Using this Book as a Textbook . . 609
Appendix B: Alternative Case Studies . . . . . . . . . . . . . 613
Appendix C: Setting Up a Basic Object Modeling I Java Environment. . 619
Appendix D: SRS Source Code . . . . . . . . . . . . . . .. 631
Appendix E: Note to Experienced C++ Programmers . . . . 635
Appendix F: How Polymorphism Works Under the Covers . . 641
Index ................... . . 651
Beginning Java Objects
Jacquie Barker
APress Media, LLC
Beginning Java Objects
Copyright ©2000 by J acquie Barker
Originally published by Apress in 2000
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 978-1-59059-146-8 ISBN 978-1-4302-1115-0 (eBook)
DOI 10.1007/978-1-4302-1115-0
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.
Technical Reviewers: Gregory Beekman, Allan] ones, Robert FE Shaw
Editorial Directors: Dan Appleman, Gary Cornell, Martin Streicher,Jim Sumser, Karen Watterson,
John Zukowski
Project Manager: Chandima Nethisinghe
Development Editor: Tim Briggs
Proofreader: Helena Sharman
Indexer: Alessandro Ansa
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.
About the Author
Jacquie Barker
Jacquie Barker is a professional software engineer and adjunct faculty member at The George
Washington University. With over 25 years of experience as a software developer and project manager,
she has been focusing on object technology since 1991, and has become proficient as a 'hands-on' object
modeler and as a Sun Microsystems certifiedJava programmer.Jacquie is currently employed as a
principal member of the technical staff at SRA International, Inc. in Fairfax, Virginia, where she
consults for both public and private sector clients, and is also the founder of ObjectStart LLC, an object
technology mentorship and training firm.
Acknowledgements
Writing this book was perhaps the biggest professional challenge that I've faced in my career to date, as
well as being one of the most gratifying. I'd like to offer my sincere, heartfelt thanks to everyone who
helped me to achieve this goal:
0 To Timothy Briggs, for sharing in my vision and for taking a chance with a new author; to Gregory
Beekman, for helping me across the 'finish line' and for his many words of inspiration along the way;
and to both Tim and Greg, for patiently responding to my seemingly millions of email messages!
Thanks to Allan Jones and Robert Shaw, as well, for their wonderful editorial support.
0 To John DiCarlo, for his instrumental role in getting me "jump started" with Java many years ago, and
for serving as my Java mentor ever since.
0 To Mary Helms, a touchstone in my life, for inspiring me to always seek new adventures and to be true
to my dreams.
0 To John Kopsky,John Walton, Sandy Tucker, and Barb Power, for their willingness to review and help
me shape early drafts of my book. And, to all of my Wrox reviewers, whose thoughtful comments kept
me honest!
0 To John Carson, for our many Starbucks sessions and shared 'war stories'-good and bad-about what
it takes to survive writing a book, and for giving me permission to use our jointly developed Java
lecture materials as the basis for some of the examples in Chapter 16.
0 To Dave Pappas, without whose encouragement (and vision for what I could achieve) I might never
have begun teaching in the first place. And, to my entire management team at SRA International, for
providing me with a rewarding workplace that fosters professional development.
0 To all my dear friends: 'Linder', Ron and Renee; Mimi and Bill; Sandy, Ed and Charlie; Susan, Dave,
Emily, and Amanda; Lisa, Mike, Ryan, Kelly, and Brianna; Donna, Chuck, and Michael; Bonnie, Tom,
Katarina, and Larissa; Curt, Kathy, Harrison, and Chelsea; Dan, Katrina, Magda, andJohn; Dave,
Debbie, Daniel, and Elaina; Richard and Bob; Dave, Martha, Michelle andJulie; Denise; Dianne;
Rich, Cathy, Scott, and Ryan;Jeanne, Dan, and Gregory; Linda; Chris, Karen, Steven, and Kyle;
Mary; Ari; Anne; Sue; and 'Grandma' Jennie; for allowing me to drop off the face of the earth (well,
almost!) for the better part of a year.
0 To my wonderfully supportive family: Cheryl, Mike, and Michaela; Doug and Sam; Ginny, Dick,
Colleen, Katie, and Barb; and Louann and Rod; for believing in me and for cheering me on, and for
forgiving me for not making it to the many fun family events that took place during my book writing
adventure.
0 And most of all, to my husband, Steve, for being such a loving, supportive partner through my many 3
AM writing frenzies and the 'roller coaster ride' known as getting published - I love you whole
bunches, 'Mellie'!
To the two most important men in my life:
My husband, Steve, for being my soul mate, best friend, and the best partner in life
anyone could ever wish for.
In loving memory of my Dad, Bill Jost.
Landscape
0 .. •/ 0 ....
Lake acjlscen! to Tree
0 .. 1 0 ....
~
I I
Deciduous Conifer
public class Tree {
procected Landscape landscape;
procected Lake nextTo;
public void setNextTo{Lake l) {
next.To • 1;
public Lake getNextTo()
return nez.tTo;
public al:.stract Color get.LeafColor ( 1;
Table of Contents
Introduction 1
Goals for this Book 1
Why Is Understanding Objects So Critical To Being a Successful 00
Programmer? 2
Who Is This Book Written For? 3
What if You are Interested in Object Modeling, But Not Necessarily in Java
Programming? 5
How This Book is Organized 5
Part 1 5
Part 2 5
Part 3 5
Conventions 6
Which Version of Java is this Book Based On? 6
A Final Thought Before We Get Started 7
Tell Us What You Think 7
Student Registration System Case Study 8
Part 1: The ABC's of Objects 11
Chapter 1: A Little Taste of Java 13
Why Java? 14
Java is Architecture Neutral 14
Java and the World Wide Web 17
Java Provides 'One Stop Shopping' 19
Practice Makes Perfect 21
Java is Heavily Object Oriented 22
Java is an Open Standard 22
Java is Free! 23
Java Language Basics 23
A Note Regarding Pseudocode versus Real Java Code 23
Anatomy of a Simple Java Program 24
Built-In Java Data Types 25
Auto increment/A utodecrement Operators 28
Java Expressions 29
Automatic Type Conversions and Explicit Casting 30
Block Structured Languages and the Scope of a Variable 31
Printing to the Screen 32
print vs. println 34
Using the String Concatenation Operator in a Print Statement 34
Escape Sequences 35
Table of Contents
Controlling a Program's Execution Flow 36
If 36
For 38
While 39
Indentation Style 41
Getting 'Hands On' With Java 43
Summary 44
Exercises 45
Chapter 2: Abstraction and Modeling 47
Simplification Through Abstraction 47
Generalization Through Abstraction 48
Organizing Abstractions Into Classification Hierarchies 49
Abstraction as the Basis for Software Development 52
Reuse of Abstractions 52
Inherent Challenges 53
What Does It Take to Be a Successful Object Modeler? 54
Summary 55
Exercises 56
Chapter 3: Objects and Classes 59
What Is an Object? 59
Data/State/A ttributes 60
Behavior/ Operations/Methods 61
Classes 63
A Note Regarding Naming Conventions 64
Instantiation 64
Encapsulation 66
Objects vs. Database Records 66
Classes as Abstract Data Types 67
Instantiating Objects: A Closer Look 69
Objects as Attributes 73
Composite Classes 74
Three Distinguishing Features of an Object-Oriented Programming Language 77
Summary 77
Exercises 78
ii