Tài liệu Fundamentals of OOP and Data Structures in Java Richard Wiene - Pdf 10

TEAMFLY

Fundamentals of OOP and Data Structures in Java
Fundamentals of OOP and Data Structures in Java is a text for an introductory course on classical data structures. Part
One of the book presents the basic principles of Object-Oriented Programming (OOP) and Graphical User Interface
(GUI) programming with Java. Part Two introduces each of the major data structures with supporting GUI-based
laboratory programs designed to reinforce the basic concepts and principles of the text. These laboratories allow the
reader to explore and experiment with the properties of each data structure. All source code for the laboratories is
available on the Web.
By integrating the principles of OOP and GUI programming, this book takes the unique path of presenting the
fundamental issues of data structures within the context of paradigms that are essential to today's professional software
developer. From the very beginning, undergraduate students will be learning practical concepts that every professional
must master during his or her career. In fact, professionals will find this book to be an excellent resource for upgrading
their knowledge of OOP, GUI programming and classical data structures. The authors assume the reader has only an
elementary understanding of Java and no experience with OOP.
Richard Wiener is Associate Professor of Computer Science at the University of Colorado at Colorado Springs and
Editor-in-Chief of The Journal of Object-Oriented Programming. He is the author or co-author of twenty-one textbooks
and professional books. In 1983 Richard Wiener received the Outstanding Teacher of the Year Award from the
University of Colorado at Colorado Springs. His areas of research include object-oriented software development,
simulated annealing and genetic algorithms, time series, and applied statistics.
Lewis J. Pinson is President of CIC and Associate Professor of Computer Science at the University of Colorado at
Colorado Springs. His areas of expertise include computer software development, object-oriented problem solving,
genetic algorithms, and complexity studies. He develops and presents training courses and intensive short courses and
workshops on object
-oriented problem solving and object-oriented languages. Dr. Pinson has authored or co-authored
eight books.
Page iii
Fundamentals of OOP and Data Structures in Java
Richard Wiener

Wiener, Richard, 1941–
Fundamentals of OOP and data structures in Java/Richard Wiener, Lewis Pinson.

p. cm.
ISBN 0 -521-66220-6 (hb)
1. Java (Computer program language) 2. Object-oriented programming (Computer
science) 3. Data structures (Computer science) I. Pinson, Lewis J. II. Title.
QA76.73.J38 W53 2000
005.1'17 – dc21
99
-
087328
ISBN 0 521 66220 6 hardback
eISBN 0-511-00168-1 virtual (netLibrary Edition)
Page v
To my children Henrik and Anna and my wife Hanne

who provide joy and love in my life.
r.w.
For Aspen. From the first moment she opened her
eyes, she captured my heart and added new meaning

to my life.
l.j.p.
Page vii
CONTENTS
Preface page xiii2.5 Scalar Types and Their Wrappers
31
2.6 Wrapping and Unwrapping – Conversion from Object to Scalar and Scalar to
Object
32
2.7 Strings 34
2.8 Class
StringBuffer
36
2.9 Arrays 36
2.10 Vector 40
2.11 Enumeration 44
2.12 Summary 48
2.13 Exercises
49
Page viii
3
Class Construction
51
3.1 Responsibilities between a Class and Its Users – Design by Contract 51
3.2 Organization of a Class
55
3.3 Packages 56
3.4 Access Modifiers 60
3.5 Naming Conventions 61
3.6 Summary

6.4 Summary 115
6.5 Exercises 115
7
Errors and Exceptions
119
7.1 Classification of Errors and Exceptions 120
7.2 Advertising Exceptions 121
7.3 Throwing an Exception 124
7.4 Creating Exception Classes
125
7.5 Handling Exceptions 126
7.6 The finally Clause 127
7.7 Putting It All Together – An Example 127
7.8 Catching Runtime Exceptions


An Example
131
7.9 Summary 133
7.10 Exercises 133
Page ix
8
Recursion
135
8.1 Properties for a Well-Behaved Recursion 136
8.2 Iteration Versus Recursion
138
8.3 Relative Complexity of a Recursion 142
8.4 Examples of Single and Double Recursion 145
8.5 Summary 152

Hierarchy


Top Level
171
10.2 The Simplest Containers – Stack and Queue 173
10.3 Supporting Interface and Classes 175
10.4 The Container Hierarchy 178
10.5 UML Description of Container Hierarchy
192

10.6 Summary 194
10.7 Exercises 194
11
Stack and Queue
197
11.1 The Stack 197
11.2 ArrayStack 198
11.3
LinkedStack
201
11.4 Comparing the Efficiency of ArrayStack with LinkedStack 205
11.5 Queue 207
11.6 LinkedQueue 208
11.7 Stack/Queue Laboratory 210
11.8 Summary
211


Team-Fly
®

Page x
12

14.4 Summary 312
14.5 Exercises 313
15
Search Trees
315

15.1 Review of Search Table Abstraction 315
15.2 Binary Search Tree 316
15.3 Searching for an Element in a Search Tree
317
15.4 Balance of Search Tree
318
15.5 Adding an Element to a Binary Search Tree 320
15.6 Removing an Element in a Binary Search Tree 320
15.7 Method add for Binary Search Tree 322
15.8 Method
remove
for Binary Search Tree
323
15.9 Performance of Binary Search Tree 330
15.10 AVL Tree 330
15.11 Tree Rotation 331
15.12 AVL
add
333
15.13 AVL Deletion 340
15.14 Splay Tree 342

399
17.3 Implementing the Dictionary Interface 402
17.4 The Dictionary Laboratory 413
17.5 The OrderedDictionary Interface 415
17.6 Implementing the
OrderedDictionary
Interface
418
17.7 The Ordered Dictionary Laboratory 422
17.8 Summary 424

17.9 Exercises 424
18
Sorting
427
18.1 Simple and Inefficient Sorting Algorithms 427
18.2 Efficient Sorting Algorithms 430
18.3 Binary Search 434
18.4 Sort Laboratory
434
18.5 Summary 435
18.6 Exercises 435
Appendix A
Unified Modeling Language Notation
437
A.1 Representing Classes in UML
437

programming, and classical data structures.
The software development principles associated with OOP provide a strong framework for presenting and implementing
classical data structures. We adhere to and emphasize these principles throughout this book.
Universities have been slow to introduce courses related to OOP into their curricula. Curriculum change has always
occurred slowly at universities, but the past dozen years have been particularly disappointing in the area of OOP
education. Often a department assumes that because it has switched language from Pascal or C to C++ or Java in CS 1 or
CS 2 that it has made a commitment to object-oriented software education. This is simply not true. Object orientation
embodies a set of principles often obscured by the intensive preoccupation with language details often evident in early
university courses and the books that cater to these courses. The spate of CS 1 and CS 2 books featuring C++ or Java are
often nothing more than warmed-over reruns of structured programming texts written originally for Pascal or C.
The principles of OOP and classical data structures are language independent. Our experience has shown that these
principles need to be brought to life using well-crafted examples supported by a rich object-oriented programming
language. In our view, Java fits this bill. It provides constructs and predefined standard libraries that directly support and
connect to the rich body of underlying OOP and data structure principles. We have chosen Java because its usage is
rising rapidly, it provides relative safety in programming, it is readily and inexpensively available (free in many cases),
and it offers the user a clean and powerful object model. But make no mistake – this is not yet another book on Java
programming. So what do we wish to achieve?
Part One of this book presents the basic principles of OOP and GUI programming. These principles are brought to life
using examples crafted in Java. The principles and techniques presented in Part One of the book are carefully chosen to
support Part Two of the book.
Page xiv
Part Two, the main part of the book, presents classical data structures. As the chapters of this part unfold, a Java-based
package (package foundations) of data structure components evolves. Most of the source code for this package is
available to the reader except in areas where, through exercises, the reader is expected to complete or enhance some data
structure classes.
In Part Two, each of the major data structure presentations are supported by laboratories designed to support and
reinforce the basic concepts and principles. Some of the laboratory programs allow the reader to extend his or her

. wishes to learn the fundamental principles of object
-
oriented software construction using Java
Page xv
2. has prior experience with the elementary and basic aspects of the Java programming language or will learn these from
basic programming books or language tutorials
3. has little or no experience with object-oriented programming
4
. has not purchased this book as yet another book on Java programming
5. wishes to extend and reinforce Java programming skills with particular emphasis on GUI software development
6. wishes to see a GUI-based presentation of major programming examples and applications
7. desires a practical OOP-based presentation of data structures and their applications.
This book has evolved from a set of notes that has undergone several iterations based on class testing over a one-year
period. We are grateful to our students for their thoughtful corrective feedback. We assume full responsibility for any
errors or inaccuracies that remain. We thank our colleague Ben Nystuen for stimulating discussion and general support
throughout the development of the notes that provide the basis for this book. We welcome feedback and corrections
from you, the reader. You may provide these by sending e-mail to or We thank you in
advance for your constructive comments.
RICHARD WIENER AND LEWIS J. PINSON
COLORADO SPRINGS, COLORADO
Page 1
PART ONE—
FOUNDATIONS


language because one can invoke functions without classes or objects. In fact most C programs (C is not an object-
oriented language) will compile and run as is using a C++ compiler. The hybrid nature of C++ makes it even more
challenging
TEAMFLY
Page 4
to use since it allows a mixture of styles of software thinking and organization. In order to use C++ effectively as an
object-oriented language, the programmer must impose rigorous constraints and style guidelines. Even with such
discipline, the C-like nature of C++ allows programmers to work around basic OOP rules and principles such as
encapsulation by using casts and pointers. The preoccupation with pointers in C++ makes the language potentially
dangerous for large software projects because of the ever present specter of memory leakage (failure to de-allocate
storage for objects that are no longer needed).
The Java programming language invented in the mid 1990s at Sun Microsystems and popularized in the late 1990s may
be considered to be a third almost pure object-
oriented language. Like Smalltalk and Eiffel, actions may be invoked only
on objects and classes (except for a limited number of predefined operators used with primitive types). Also like
Smalltalk and Eiffel and unlike C++, Java objects that are no longer needed are disposed of automatically using ''garbage
collection." The programmer is unburdened from having to devote time and effort to this important concern. It might be
argued that the presence of primitive types in Java makes the language impure from an OOP perspective. Although this
is strictly true, the basic nature and character of Java is that of a pure object-oriented language and we consider it such.
OOP got its popular start in Portland, Oregon in 1986 at the first Association for Computing Machinery (ACM)-
sponsored OOPSLA (object-oriented programming, systems, languages, and applications) conference. At that time the
first versions of C++ and Eiffel had recently been released. The three most highly developed languages that were
showcased at this first OOPSLA conference were Object Pascal, Objective-C, and Smalltalk. The first release of the
Java programming language was ten years away.
During the early days of object-oriented programming, attention was focused on the construction and development of
OOP languages. Associated with these newly emerging languages were problem-solving methodologies and notations to
support the software analysis and design processes. It was not until the late 1990s that standardization of the object-
oriented analysis and design notation occurred with the Unified Modeling Language (UML).
The early application areas of OOP were the construction of libraries to support graphical user interfaces (GUIs),
databases, and simulation. These application areas continue to provide fertile soil to support OOP development.
As we enter the twenty-first century, OOP has become widely accepted as a mainstream paradigm for problem solving

physical object but is more often an idea or concept that may be represented by an internal state. As an example consider
a bouncing ball. If we were simulating the motion of the bouncing ball with software we would model the ball as an
object and its dynamic state as its height above the surface on which it was bouncing. Here the software object
represents a physical object. As a more abstract example consider a cashier line at a supermarket. If we were to represent
the line as a software object, its internal state might be the number of customers waiting to check out. Associated with
the line would be a set of behavioral rules. For example, the first customer to arrive would be the first customer to be
served. The last customer to arrive would be the last to be served.
OOP is also based on the notion of sending messages to objects. Messages can modify or return information about the
internal state of an object. We can send a line object the message addCustomer. This causes the internal state of the line
to change. We can send a ball object the message currentHeight. This returns the ball's height above the surface.
The behavior of an object is codified in a class description. The object is said to be an instance of the class
that describes
its behavior. The class description specifies the internal state of the object and defines the types of messages that may be
sent to all its instances. A class
Queue
might be defined to describe the behavior of line objects.
In a program an object is a program variable associated with a class type. The object encapsulates data. An object's
''value" or information content is given by its
Page 6
internal state. This internal state is defined in terms of one or more fields. Each field holds a portion of the information
content of the object. As indicated above, an object can receive messages that either change the internal state (i.e.,
change the value of one or more fields) or return information about the internal state of the object. These messages
represent the operations that may be performed on the object.
1.4—

Class
A class describes the behavior of objects, its instances. The external or ''public" view of a class describes the messages
that may be sent to instances. Each possible message is defined by a method. These include messages that affect the
internal state of the object and messages that return information about this internal state. The internal or "private" view
of a class describes the fields that hold the information content of instances. In addition to fields, the private view of a
class may define private methods that are used to support public methods but cannot be invoked outside of the class.
The user of a class is concerned only with the public or external view of the class. The producer of a class is concerned
with the public and private view. Chapter 3 describes the construction of Java classes in detail.
Let us consider a simple example to illustrate some of the ideas presented above. Consider class Point. The "actions"
that one may take on a point object include:
1
. setX(xValue)
2
. setY(yValue)
3. x()
4. y()
5. distanceFromOrigin()
Note: We prefer to use a noun phrase rather than a verb phrase for a message that returns internal information about an
object. This is justified in Chapter 3.
The five external actions that have been defined for class Point
are called accessor methods. They allow us to set and get
the values of the x and y coordinates of a point object and get the distance of the point to the origin. The first two
accessors,
setX
and
setY
, require a parameter.
Listing 1.1 presents a full Java class definition for Point.
Listing 1.1 Class
Point


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status