Java Structures
Data Structures in Java for the Principled Programmer
√
The
7 Edition
(Software release 33)
Duane A. Bailey
Williams College
September 2007
√
This
7 text copyrighted 2005-2007 by
All rights are reserved by The Author.
No part of this draft publiciation may be reproduced or distributed in any form
without prior, written consent of the author.
Contents
Preface to First Edition
Preface to the Second Edition
Preface to the “Root 7” Edition
0 Introduction
0.1 Read Me . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0.2 He Can’t Say That, Can He? . . . . . . . . . . . . . . . . . . . . .
xi
xiii
xv
1
1
2
1 The Object-Oriented Method
5
6
1.1 Data Abstraction and Encapsulation . . . . . . . . . . . . . . . . .
7
1.2 The Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 Object-Oriented Terminology . . . . . . . . . . . . . . . . . . . .
8
1.4 A Special-Purpose Class: A Bank Account . . . . . . . . . . . . . . 11
1.5 A General-Purpose Class: An Association . . . . . . . . . . . . . . 14
1.6 Sketching an Example: A Word List . . . . . . . . . . . . . . . . . 18
1.7 Sketching an Example: A Rectangle Class
. . . . . . . . . . . . . 20
1.8 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.9 Who Is the User? . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.10 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.11 Laboratory: The Day of the Week Calculator . . . . . . . . . . . . 29
2 Comments, Conditions, and Assertions
33
2.1 Pre- and Postconditions
. . . . . . . . . . . . . . . . . . . . . . . 34
2.2 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.3 Craftsmanship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.4 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.5 Laboratory: Using Javadoc Commenting . . . . . . . . . . . . . . 39
3 Vectors
43
3.1 The Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.2 Example: The Word List Revisited . . . . . . . . . . . . . . . . . . 47
3.3 Example: Word Frequency . . . . . . . . . . . . . . . . . . . . . . 48
3.4 The Implementation . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.5 Extensibility: A Feature . . . . . . . . . . . . . . . . . . . . . . . . 53
. . . . . . . . . . . . . . . . . . . . . . . . . 56
3.6 Example: L-Systems
3.7 Example: Vector-Based Sets . . . . . . . . . . . . . . . . . . . . . 57
3.8 Example: The Matrix Class . . . . . . . . . . . . . . . . . . . . . . 60
3.9 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
iv
Contents
3.10 Laboratory: The Silver Dollar Game . . . . . . . . . . . . . . . . . 67
4 Generics
69
4.1 Motivation (in case we need some) . . . . . . . . . . . . . . . . . 70
4.1.1 Possible Solution: Specialization . . . . . . . . . . . . . . 71
4.2 Implementing Generic Container Classes . . . . . . . . . . . . . . 72
4.2.1 Generic Associations . . . . . . . . . . . . . . . . . . . . 72
4.2.2 Parameterizing the Vector Class
. . . . . . . . . . . . . . 74
4.2.3 Restricting Parameters . . . . . . . . . . . . . . . . . . . . 79
4.3 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5 Design Fundamentals
81
5.1 Asymptotic Analysis Tools . . . . . . . . . . . . . . . . . . . . . . 81
5.1.1 Time and Space Complexity . . . . . . . . . . . . . . . . . 82
5.1.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.1.3 The Trading of Time and Space . . . . . . . . . . . . . . . 91
5.1.4 Back-of-the-Envelope Estimations . . . . . . . . . . . . . . 92
5.2 Self-Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.2.1 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.2.2 Mathematical Induction . . . . . . . . . . . . . . . . . . . 101
5.3 Properties of Design . . . . . . . . . . . . . . . . . . . . . . . . . 108
5.3.1 Symmetry . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
5.3.2 Friction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
5.4 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
5.5 Laboratory: How Fast Is Java? . . . . . . . . . . . . . . . . . . . . 115
6 Sorting
119
6.1 Approaching the Problem . . . . . . . . . . . . . . . . . . . . . . 119
6.2 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
6.3 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
6.4 Mergesort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
6.5 Quicksort
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
6.6 Radix Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
6.7 Sorting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
6.8 Ordering Objects Using Comparators . . . . . . . . . . . . . . . . 140
6.9 Vector-Based Sorting . . . . . . . . . . . . . . . . . . . . . . . . . 143
6.10 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
6.11 Laboratory: Sorting with Comparators . . . . . . . . . . . . . . . 147
7 A Design Method
149
7.1 The Interface-Based Approach . . . . . . . . . . . . . . . . . . . . 149
7.1.1 Design of the Interface . . . . . . . . . . . . . . . . . . . . 150
7.1.2 Development of an Abstract Implementation . . . . . . . . 151
7.1.3 Implementation . . . . . . . . . . . . . . . . . . . . . . . . 152
7.2 Example: Development of Generators . . . . . . . . . . . . . . . . 152
. . . . . . . . . . . . . . . . . . . . . . . 155
7.3 Example: Playing Cards
Contents
v
7.4 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
8 Iterators
161
8.1 Java’s Enumeration Interface . . . . . . . . . . . . . . . . . . . . 161
8.2 The Iterator Interface . . . . . . . . . . . . . . . . . . . . . . . . . 163
. . . . . . . . . . . . . . . . . . . . . . 165
8.3 Example: Vector Iterators
8.4 Example: Rethinking Generators
. . . . . . . . . . . . . . . . . . 167
8.5 Example: Filtering Iterators . . . . . . . . . . . . . . . . . . . . . 170
8.6 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
8.7 Laboratory: The Two-Towers Problem . . . . . . . . . . . . . . . 175
9 Lists
179
9.1 Example: A Unique Program . . . . . . . . . . . . . . . . . . . . . 182
9.2 Example: Free Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 183
. . . . . . . . . . . . . . . 186
9.3 Partial Implementation: Abstract Lists
9.4 Implementation: Singly Linked Lists
. . . . . . . . . . . . . . . . 188
9.5 Implementation: Doubly Linked Lists . . . . . . . . . . . . . . . . 201
. . . . . . . . . . . . . . 206
9.6 Implementation: Circularly Linked Lists
9.7 Implementation: Vectors . . . . . . . . . . . . . . . . . . . . . . . 209
9.8 List Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
9.9 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
9.10 Laboratory: Lists with Dummy Nodes . . . . . . . . . . . . . . . . 215
10 Linear Structures
10.2 Queues
219
10.1 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
10.1.1 Example: Simulating Recursion . . . . . . . . . . . . . . . 222
10.1.2 Vector-Based Stacks
. . . . . . . . . . . . . . . . . . . . . 225
10.1.3 List-Based Stacks . . . . . . . . . . . . . . . . . . . . . . . 227
. . . . . . . . . . . . . . . . . . . . . . . . . 228
10.1.4 Comparisons
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
10.2.1 Example: Solving a Coin Puzzle . . . . . . . . . . . . . . . 231
10.2.2 List-Based Queues
. . . . . . . . . . . . . . . . . . . . . . 234
10.2.3 Vector-Based Queues . . . . . . . . . . . . . . . . . . . . . 235
10.2.4 Array-Based Queues . . . . . . . . . . . . . . . . . . . . . 238
10.3 Example: Solving Mazes . . . . . . . . . . . . . . . . . . . . . . . 242
10.4 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
10.5 Laboratory: A Stack-Based Language . . . . . . . . . . . . . . . . 247
10.6 Laboratory: The Web Crawler . . . . . . . . . . . . . . . . . . . . 251
11 Ordered Structures
253
11.1 Comparable Objects Revisited . . . . . . . . . . . . . . . . . . . . 253
11.1.1 Example: Comparable Ratios
. . . . . . . . . . . . . . . . 254
11.1.2 Example: Comparable Associations . . . . . . . . . . . . . 256
11.2 Keeping Structures Ordered . . . . . . . . . . . . . . . . . . . . . 258
11.2.1 The OrderedStructure Interface . . . . . . . . . . . . . . . 258
11.2.2 The Ordered Vector and Binary Search . . . . . . . . . . . 259
vi
Contents
11.2.3 Example: Sorting Revisited . . . . . . . . . . . . . . . . . 264
11.2.4 A Comparator-based Approach . . . . . . . . . . . . . . . 265
11.2.5 The Ordered List
. . . . . . . . . . . . . . . . . . . . . . . 267
11.2.6 Example: The Modified Parking Lot . . . . . . . . . . . . . 270
11.3 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
11.4 Laboratory: Computing the “Best Of” . . . . . . . . . . . . . . . . 275
12 Binary Trees
277
12.1 Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
12.2 Example: Pedigree Charts . . . . . . . . . . . . . . . . . . . . . . 280
12.3 Example: Expression Trees . . . . . . . . . . . . . . . . . . . . . . 281
12.4 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
12.4.1 The BinaryTree Implementation . . . . . . . . . . . . . . . 284
12.5 Example: An Expert System . . . . . . . . . . . . . . . . . . . . . 287
. . . . . . . . . . . . . . . . . . . . . . 290
12.6 Traversals of Binary Trees
. . . . . . . . . . . . . . . . . . . . . . 291
12.6.1 Preorder Traversal
12.6.2 In-order Traversal
. . . . . . . . . . . . . . . . . . . . . . 293
12.6.3 Postorder Traversal . . . . . . . . . . . . . . . . . . . . . . 295
12.6.4 Level-order Traversal . . . . . . . . . . . . . . . . . . . . . 296
. . . . . . . . . . . . . . . . . . . . 297
12.6.5 Recursion in Iterators
12.7 Property-Based Methods . . . . . . . . . . . . . . . . . . . . . . . 299
12.8 Example: Huffman Compression . . . . . . . . . . . . . . . . . . 303
12.9 Example Implementation: Ahnentafel . . . . . . . . . . . . . . . . 307
12.10Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
12.11Laboratory: Playing Gardner’s Hex-a-Pawn . . . . . . . . . . . . . 313
13 Priority Queues
315
13.1 The Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
13.2 Example: Improving the Huffman Code
. . . . . . . . . . . . . . 317
13.3 A Vector-Based Implementation . . . . . . . . . . . . . . . . . . . 318
13.4 A Heap Implementation . . . . . . . . . . . . . . . . . . . . . . . 319
13.4.1 Vector-Based Heaps
. . . . . . . . . . . . . . . . . . . . . 320
13.4.2 Example: Heapsort . . . . . . . . . . . . . . . . . . . . . . 326
13.4.3 Skew Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . 329
13.5 Example: Circuit Simulation . . . . . . . . . . . . . . . . . . . . . 333
13.6 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
. . . . . . . . . . . . . . . . . . 341
13.7 Laboratory: Simulating Business
14 Search Trees
343
14.1 Binary Search Trees . . . . . . . . . . . . . . . . . . . . . . . . . . 343
14.2 Example: Tree Sort . . . . . . . . . . . . . . . . . . . . . . . . . . 345
14.3 Example: Associative Structures . . . . . . . . . . . . . . . . . . . 345
14.4 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
14.5 Splay Trees
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
14.6 Splay Tree Implementation . . . . . . . . . . . . . . . . . . . . . 357
14.7 An Alternative: Red-Black Trees . . . . . . . . . . . . . . . . . . . 361
Contents
vii
14.8 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
14.9 Laboratory: Improving the BinarySearchTree . . . . . . . . . . . . 367
15 Maps
369
15.1 Example Revisited: The Symbol Table . . . . . . . . . . . . . . . . 369
15.2 The Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
15.3 Simple Implementation: MapList
. . . . . . . . . . . . . . . . . . 372
15.4 Constant Time Maps: Hash Tables . . . . . . . . . . . . . . . . . . 374
15.4.1 Open Addressing . . . . . . . . . . . . . . . . . . . . . . . 375
15.4.2 External Chaining . . . . . . . . . . . . . . . . . . . . . . 383
15.4.3 Generation of Hash Codes . . . . . . . . . . . . . . . . . . 385
15.4.4 Hash Codes for Collection Classes . . . . . . . . . . . . . . 391
15.4.5 Performance Analysis . . . . . . . . . . . . . . . . . . . . . 392
. . . . . . . . . . . . . . . . . . . . . . 392
15.5 Ordered Maps and Tables
15.6 Example: Document Indexing . . . . . . . . . . . . . . . . . . . . 395
15.7 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
15.8 Laboratory: The Soundex Name Lookup System . . . . . . . . . . 401
16 Graphs
403
16.1 Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
16.2 The Graph Interface . . . . . . . . . . . . . . . . . . . . . . . . . 404
16.3 Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
16.3.1 Abstract Classes Reemphasized . . . . . . . . . . . . . . . 408
16.3.2 Adjacency Matrices . . . . . . . . . . . . . . . . . . . . . . 410
16.3.3 Adjacency Lists . . . . . . . . . . . . . . . . . . . . . . . . 416
16.4 Examples: Common Graph Algorithms . . . . . . . . . . . . . . . 422
16.4.1 Reachability . . . . . . . . . . . . . . . . . . . . . . . . . . 422
16.4.2 Topological Sorting . . . . . . . . . . . . . . . . . . . . . . 424
16.4.3 Transitive Closure . . . . . . . . . . . . . . . . . . . . . . 427
16.4.4 All Pairs Minimum Distance . . . . . . . . . . . . . . . . . 428
16.4.5 Greedy Algorithms . . . . . . . . . . . . . . . . . . . . . . 429
16.5 Conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
16.6 Laboratory: Converting Between Units . . . . . . . . . . . . . . . 439
A Answers
441
A.1 Solutions to Self Check Problems . . . . . . . . . . . . . . . . . . 441
. . . . . . . . . . . . . . . 451
A.2 Solutions to Odd-Numbered Problems
B Beginning with Java
489
B.1 A First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
B.2 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
B.2.1 Primitive Types . . . . . . . . . . . . . . . . . . . . . . . . 491
. . . . . . . . . . . . . . . . . . . . . . . 493
B.2.2 Reference Types
B.3 Important Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
B.3.1 The structure.ReadStream Class . . . . . . . . . . . . . . . 494
. . . . . . . . . . . . . . . . . 495
B.3.2 The java.util.Scanner Class
viii
Contents
B.4.1 Conditional Statements
B.4.2 Loops
B.3.3 The PrintStream Class . . . . . . . . . . . . . . . . . . . . 496
B.3.4 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
B.4 Control Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . 498
. . . . . . . . . . . . . . . . . . . 498
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
B.5 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
B.6 Inheritance and Subtyping . . . . . . . . . . . . . . . . . . . . . . 502
B.6.1 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 502
B.6.2 Subtyping . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
. . . . . . . . . . . . . . . 504
B.6.3 Interfaces and Abstract Classes
B.7 Use of the Assert Command . . . . . . . . . . . . . . . . . . . . . 506
B.8 Use of the Keyword Protected . . . . . . . . . . . . . . . . . . . 507
C Collections
511
C.1 Collection Class Features . . . . . . . . . . . . . . . . . . . . . . . 511
C.2 Parallel Features
. . . . . . . . . . . . . . . . . . . . . . . . . . . 511
C.3 Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
D Documentation
513
D.1 Structure Package Hierarchy . . . . . . . . . . . . . . . . . . . . . 513
D.2 Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Index
517
for Mary,
my wife and best friend
without
the model of my mentors,
the comments of my colleagues,
the support of my students,
the friendship of my family
this book would never be
thank you!
Preface to the First Edition
“IT’S A WONDERFUL TIME TO BE ALIVE.” At least that’s what I’ve found myself
saying over the past couple of decades. When I first started working with com-
puters, they were resources used by a privileged (or in my case, persistent) few.
They were physically large, and logically small. They were cast from iron. The
challenge was to make these behemoths solve complex problems quickly.
Today, computers are everywhere. They are in the office and at home. They
speak to us on telephones; they zap our food in the microwave. They make
starting cars in New England a possibility. Everyone’s using them. What has
aided their introduction into society is their diminished size and cost, and in-
creased capability. The challenge is to make these behemoths solve complex
problems quickly.
Thus, while the computer and its applications have changed over time, the
challenge remains the same: How can we get the best performance out of the
current technology? The design and analysis of data structures lay the funda-
mental groundwork for a scientific understanding of what computers can do
efficiently. The motivations for data structure design work accomplished three
decades ago in assembly language at the keypunch are just as familiar to us to-
day as we practice our craft in modern languages on computers on our laps. The
focus of this material is the identification and development of relatively abstract
principles for structuring data in ways that make programs efficient in terms of
their consumption of resources, as well as efficient in terms of “programmability.”
In the past, my students have encountered this material in Pascal, Modula-2,
and, most recently, C++. None of these languages has been ideal, but each has
been met with increasing expectation. This text uses The Java Programming
Language1—“Java”—to structure data. Java is a new and exciting language
that has received considerable public attention. At the time of this writing, for
example, Java is one of the few tools that can effectively use the Internet as a
computing resource. That particular aspect of Java is not touched on greatly
in this text. Still, Internet-driven applications in Java will need supporting data
structures. This book attempts to provide a fresh and focused approach to the
design and implementation of classic structures in a manner that meshes well
with existing Java packages.
It is hoped that learning this material in Java
will improve the way working programmers craft programs, and the way future
designers craft languages.
Pedagogical Implications. This text was developed specifically for use with
CS2 in a standard Computer Science curriculum. It is succinct in its approach,
and requires, perhaps, a little more effort to read. I hope, though, that this text
1 Java is a trademark of Sun Microsystems, Incorporated.
xii
Preface to the First Edition
becomes not a brief encounter with object-oriented data structure design, but a
touchstone for one’s programming future.
The material presented in this text follows the syllabus I have used for sev-
eral years at Williams. As students come to this course with experience using
Java, the outline of the text may be followed directly. Where students are new
to Java, a couple of weeks early in the semester will be necessary with a good
companion text to introduce the student to new concepts, and an introductory
Java language text or reference manual is recommended. For students that need
a quick introduction to Java we provide a tutorial in Appendix B. While the text
was designed as a whole, some may wish to eliminate less important topics
and expand upon others. Students may wish to drop (or consider!) the sec-
tion on induction (Section 5.2.2). The more nontraditional topics—including,
for example, iteration and the notions of symmetry and friction—have been in-
cluded because I believe they arm programmers with important mechanisms for
implementing and analyzing problems. In many departments the subtleties of
more advanced structures—maps (Chapter 15) and graphs (Chapter 16)—may
be considered in an algorithms course. Chapter 6, a discussion of sorting, pro-
vides very important motivating examples and also begins an early investigation
of algorithms. The chapter may be dropped when better examples are at hand,
but students may find the refinements on implementing sorting interesting.
Associated with this text is a Java package of data structures that is freely
available over the Internet for noncommercial purposes. I encourage students,
educators, and budding software engineers to download it, tear it down, build it
up, and generally enjoy it. In particular, students of this material are encouraged
to follow along with the code online as they read. Also included is extensive
documentation gleaned from the code by javadoc. All documentation—within
the book and on the Web—includes pre- and postconditions. The motivation for
this style of commenting is provided in Chapter 2. While it’s hard to be militant
about commenting, this style of documentation provides an obvious, structured
approach to minimally documenting one’s methods that students can appreciate
and users will welcome. These resources, as well as many others, are available
from McGraw-Hill at http://www.mhhe.com/javastructures.
Three icons appear throughout the text, as they do in the margin. The
top “compass” icon highlights the statement of a principle—a statement that
encourages abstract discussion. The middle icon marks the first appearance of
a particular class from the structure package. Students will find these files at
McGraw-Hill, or locally, if they’ve been downloaded. The bottom icon similarly
marks the appearance of example code.
Finally, I’d like to note an unfortunate movement away from studying the
implementation of data structures, in favor of studying applications.
In the
extreme this is a disappointing and, perhaps, dangerous precedent. The design
of a data structure is like the solution to a riddle: the process of developing the
answer is as important as the answer itself. The text may, however, be used as a
reference for using the structure package in other applications by selectively
avoiding the discussions of implementation.
List
nim
NNWSWSENEWSEPreface to the Second Edition
Since the first edition of Java Structures support for writing programs in Java2
has grown considerably. At that time the Java Development Toolkit consisted
of 504 classes in 23 packages3 In Java 1.2 (also called Java 2) Sun rolled out
1520 classes in 59 packages. This book is ready for Java 1.4, where the number
of classes and packages continues to grow.
Most computer scientists are convinced of the utility of Java for program-
ming in a well structured and platform independent manner. While there are
still significant arguments about important aspects of the language (for exam-
ple, support for generic types), the academic community is embracing Java, for
example, as the subject of the Computer Science Advanced Placement Exami-
nation.
It might seem somewhat perplexing to think that many aspects of the origi-
nal Java environment have been retracted (or deprecated) or reconsidered. The
developers at Sun have one purpose in mind: to make Java the indispensable
language of the current generation. As a result, documenting their progress on
the development of data structures gives us valuable insight into the process of
designing useful data structures for general purpose programming. Those stu-
dents and faculty considering a move to this second edition of Java Structures
will see first-hand some of the decisions that have been made in the interven-
ing years. During that time, for example, the Collection-based classes were
introduced, and are generally considered an improvement. Another force—
one similar to calcification—has left a trail of backwards compatible features
that are sometimes difficult to understand. For example, the Iterator class
was introduced, but the Enumeration class was not deprecated. One subject of
the first edition—the notion of Comparable classes—has been introduced into
a number of important classes including String and Integer. This is a step
forward and a reconsideration of what we have learned about that material has
lead to important improvements in the text.
Since the main purpose of the text is to demonstrate the design and behavior
of traditional data structures, we have not generally tracked the progress of
Java where it blurs the view. For example, Java 2 introduces a List interface
(we applaud) but the Vector class has been extended to include methods that
are, essentially, motivated by linked lists (we wonder). As this text points out
frequently, the purpose of an interface is often to provide reduced functionality.
If the data structure does not naturally provide the functionality required by the
application, it is probably not an effective tool for solving the problem: search
elsewhere for an effective structure.
2 The Java Programming Language is a trademark of Sun Microsystems, Incorporated.
3 David Flanagan, et al., Java in a Nutshell, O’Reilly & Associates.
xiv
Preface to the Second Edition
As of this writing, more than 100, 000 individuals have searched for and
downloaded the structure package. To facilitate using the comprehensive set
of classes with the Java 2 environment, we have provided a number of features
that support the use of the structure package in more concrete applications.
Please see Appendix C.
Also new to this edition are more than 200 new problems, several dozen
exercises, and over a dozen labs we regularly use at Williams.
Acknowledgments. Several students, instructors, and classes have helped to
shape this edition of Java Structures. Parth Doshi and Alex Glenday—diligent
Williams students—pointed out a large number of typos and stretches of logic.
Kim Bruce, Andrea Danyluk, Jay Sachs, and Jim Teresco have taught this course
at Williams over the past few years, and have provided useful feedback. I tip
my hat to Bill Lenhart, a good friend and advisor, who has helped improve this
text in subtle ways. To Sean Sandys I am indebted for showing me new ways to
teach new minds.
The various reviewers have made, collectively, hundreds of pages of com-
ments that have been incorporated (as much as possible) into this edition:
Eleanor Hare and David Jacobs (Clemson University), Ram Athavale (North
Carolina State University), Yannick Daoudi (McGill University), Walter Daugh-
erty (Texas A&M University), Subodh Kumar (Johns Hopkins University), Toshimi
Minoura (Oregon State University), Carolyn Schauble (Colorado State Univer-
sity), Val Tannen (University of Pennsylvania), Frank Tompa (University of Wa-
terloo), Richard Wiener (University of Colorado at Colorado Springs), Cynthia
Brown Zickos (University of Mississippi), and my good friend Robbie Moll (Uni-
versity of Massachusetts). Deborah Trytten (University of Oklahoma) has re-
viewed both editions! Still, until expert authoring systems are engineered, au-
thors will remain human. Any mistakes left behind or introduced are purely
those of the author.
The editors and staff at McGraw-Hill–Kelly Lowery, Melinda Dougharty, John
Wannemacher, and Joyce Berendes–have attempted the impossible: to keep me
within a deadline. David Hash, Phil Meek, and Jodi Banowetz are responsible
for the look and feel of things. I am especially indebted to Lucy Mullins, Judy
Gantenbein, and Patti Evers whose red pens have often shown me a better way.
Betsy Jones, publisher and advocate, has seen it all and yet kept the faith:
thanks.
Be aware, though: long after these pages are found to be useless folly, my
best work will be recognized in my children, Kate, Megan, and Ryan. None
of these projects, of course, would be possible without the support of my best
friend, my north star, and my partner, Mary.
Enjoy!
Duane A. Bailey
Williamstown, May 2002
Preface to the
√
7 Edition
In your hand is a special edition of Java Structures designed for use with two
semesters of Williams’ course on data structures, Computer Science 136. This
version is only marginally different than the preceding edition, but is positioned
to make use of Java 5 (the trademarked name for version 1.5 of the JDK).
Because Java 5 may not be available (yet) on the platform you use, most of the
code available in this book will run on older JDK’s. The one feature that would
not be available is Java’s new Scanner class from the java.util package; an
alternative is my ReadStream class, which is lightly documented in Section B.3.1
on page 494. It is a feature of the structure package soon to be removed.
In making this book available in this paperbound format, my hope is that
you find it a more inviting place to write notes: additions, subtractions, and
updates that you’re likely to have discussed in class. Sometimes you’ll identify
improvements, and I hope you’ll pass those along to me. In any case, you can
download the software (as hundreds of thousands have done in the past) and
modify it as you desire.
On occasion, I will release new sections you can incorporate into your text,
including a discussion of how the structure package can make use of generic
types.
I have spent a considerable amount of time designing the structure pack-
age. The first structures were available 8 years ago when Java was still in its
infancy. Many of the structures have since been incorporated (directly or indi-
rectly) into Sun’s own JDK. (Yes, we’ve sold a few books in California.) Still, I
feel the merit of my approach is a slimness that, in the end, you will not find
surprising.
Meanwhile, for those of you keeping track, the following table (adapted
from the 121 cubic inch, 3 pound 6 ounce, Fifth edition of David Flanagan’s
essential Java in a Nutshell) demonstrates the growth of Java’s support:
JDK
1.0
1.1
1.2 (Java 2)
1.3
1.4
1.5 (Java 5)
Packages Classes
8
23
59
76
135
166
212
504
1520
1842
2991
3562
Features
First public version
Inner classes
Collection classes
A “maintenance” release.
Improvments, including assert
Generics, autoboxing, and “varargs.”
Seeing this reminds me of the comment made by Niklaus Wirth, designer of
Pascal and the first two releases of Modula. After the design team briefed him
on the slew of new features to be incorporated into Modula 3, he parried: “But,
what features have you removed?” A timeless question.
xvi
Preface to the
√
7 Edition
Acknowledgments. This book was primarily written for students of Williams
College. The process of publishing and reviewing a text tends to move the focus
off campus and toward the bottom line. The Route 7 edition4—somewhere
between editions 2 and 3—is an initial attempt to bring that focus back to those
students who made it all possible.
For nearly a decade, students at many institutions have played an important
role in shaping these resources. In this edition, I’m especially indebted to Katie
Creel ’10 (Williams) and Brian Bargh ’07 (Messiah): thanks!
Many colleagues, including Steve Freund ’95 (Stanford, now at Williams),
Jim Teresco ’92 (Union, now at Mount Holyoke), and especially Gene Chase ’65
(M.I.T., now at Messiah) continue to nudge this text in a better direction. Brent
Heeringa ’99 (Morris, now at Williams) showers all around him with youthful
enthusiasm.
And a most special thanks to Bill Mueller for the shot heard around the
world—the game-winning run that showed all things were possible. Called by
Joe Castiglione ’68 (Colgate, now at Fenway):
“Three-and-one to Mueller. One out, nineth inning. 10-9 Yankees,
runner at first. Here’s the pitch...swing and a High Drive Deep to
Right...Back Goes Sheffield to the Bullpen...AND IT IS GONE!...AND
THE RED SOX HAVE WON IT!...ON A WALKOFF TWO RUN HOMER
BY BILL MUELLER OFF MARIANO RIVERA! CAN YOU BELIEVE IT?!”
Have I been a Red Sox fan all my life? Not yet.
Finally, nothing would be possible without my running mate, my Sox buddy,
and my best friend, Mary.
Cheers!
Duane A. Bailey ’82 (Amherst, now at Williams)
Williamstown, September 2007
4 Route 7 is a scenic byway through the Berkshires and Green Mountains that eddies a bit as it
passes through Williamstown and Middlebury.
Chapter 0
Introduction
Concepts:
. Approaches to this material
.
Principles
This is an important notice.
Please have it translated.
—The Phone Company
YOUR MOTHER probably provided you with constructive toys, like blocks or
Tinkertoys1 or Lego bricks. These toys are educational: they teach us to think
spatially and to build increasingly complex structures. You develop modules
that can be stuck together and rules that guide the building process.
If you are reading this book, you probably enjoyed playing with construc-
tive toys. You consider writing programs an artistic process. You have grown
from playing with blocks to writing programs. The same guidelines for building
structures apply to writing programs, save one thing: there is, seemingly, no
limit to the complexity of the programs you can write.
Well, almost. When writing large programs, the data structures that main-
tain the data in your program govern the space and time consumed by your
running program. In addition, large programs take time to write. Using differ-
ent structures can actually have an impact on how long it takes to write your
program. Choosing the wrong structures can cause your program to run poorly
or be difficult or impossible to implement effectively.
Thus, part of the program-writing process is choosing between different
structures.
Ideally you arrive at solutions by analyzing and comparing their
various merits. This book focuses on the creation and analysis of traditional
data structures in a modern programming environment, The Java Programming
Language, or Java for short.
I lie.
0.1 Read Me
As might be expected, each chapter is dedicated to a specific topic. Many of the
topics are concerned with specific data structures. The structures we will inves-
tigate are abstracted from working implementations in Java that are available
to you if you have access to the Internet.2 Other topics concern the “tools of the
1 All trademarks are recognized.
2 For more information, see http://www.cs.williams.edu/JavaStructures.
2
Introduction
Unicycles: the
ultimate riding
structure.
Structure
Example
trade.” Some are mathematical and others are philosophical, but all consider
the process of programming well.
The topics we cover are not all-inclusive. Some useful structures have been
left out. Instead, we will opt to learn the principles of programming data struc-
tures, so that, down the road, you can design newer and better structures your-
self.
Perhaps the most important aspect of this book is the set of problems at the
end of each section. All are important for you to consider. For some problems
I have attempted to place a reasonable hint or answer in the back of the book.
Why should you do problems? Practice makes perfect. I could show you how to
ride a unicycle, but if you never practiced, you would never learn. If you study
and understand these problems, you will find your design and analytical skills
are improved. As for your mother, she’ll be proud of you.
Sometimes we will introduce problems in the middle of the running text—
these problems do not have answers (sometimes they are repeated as formal
problems in the back of the chapter, where they do have answers)—they should
be thought about carefully as you are reading along. You may find it useful to
have a pencil and paper handy to help you “think” about these problems on the
fly.
Exercise 0.1 Call3 your Mom and tell her you’re completing your first exercise. If
you don’t have a phone handy, drop her a postcard. Ask her to verify that she’s
proud of you.
This text is brief and to the point. Most of us are interested in experimenting.
We will save as much time as possible for solving problems, perusing code, and
practicing writing programs. As you read through each of the chapters, you
might find it useful to read through the source code online. As we first consider
the text of files online, the file name will appear in the margin, as you see here.
The top icon refers to files in the structure package, while the bottom icon
refers to files supporting examples.
One more point—this book, like most projects, is an ongoing effort, and
the latest thoughts are unlikely to have made it to the printed page.
If you
are in doubt, turn to the website for the latest comments. You will also find
online documentation for each of the structures, generated from the code using
javadoc.
It is best to read the online version of the documentation for the
most up-to-date details, as well as the documentation of several structures not
formally presented within this text.
0.2 He Can’t Say That, Can He?
Sure! Throughout this book are little political comments. These remarks may
seem trivial at first blush. Skip them! If, however, you are interested in ways
3 Don’t e-mail her. Call her. Computers aren’t everything, and they’re a poor medium for a mother’s
pride.
0.2 He Can’t Say That, Can He?
3
to improve your skills as a programmer and a computer scientist, I invite you
to read on. Sometimes these comments are so important that they appear as
principles:
Principle 1 The principled programmer understands a principle well enough to
form an opinion about it.
Self Check Problems
Solutions to these problems begin on page 441.
0.1
0.2
0.3
0.4
Where are the answers for “self check” problems found?
What are features of large programs?
Should you read the entire text?
Are principles statements of truth?
Problems
All odd problems have answers. Where do you find answers to prob-
Surf to the website associated with this text and review the resources
You are an experienced programmer. What five serious pieces of advice
Which of the following structures are described in this text (see Append-
Surf to http://www.javasoft.com and review the Java resources avail-
Solutions to the odd-numbered problems begin on page 451.
0.1
lems? (Hint: See page 451.)
0.2
would you give a new programmer?
0.3
available to you.
0.4
ix D): BinarySearchTree, BinaryTree, BitSet, Map, Hashtable, List?
0.5
able from Sun, the developers of Java.
Review documentation for Sun’s java.util package. (See the Core
0.6
API Documentation at http://www.javasoft.com.) Which of the following
data structures are available in this package: BinarySearchTree, BinaryTree,
BitSet, Dictionary, Hashtable, List?
Check your local library or bookstore for Java reference texts.
0.7
If you haven’t done so already, learn how to use your local Java pro-
0.8
gramming environment by writing a Java application to write a line of text.
(Hint: Read Appendix B.)
Find the local documentation for the structure package. If none is to
0.9
be found, remember that the same documentation is available over the Internet
from http://www.cs.williams.edu/JavaStructures.
0.10
age. Many of these examples are discussed later in this text.
Find the examples electronically distributed with the structure pack-
NNWSWSENEWSEChapter 1
The Object-Oriented Method
Concepts:
. Data structures
. Abstract data types
. Objects
. Classes
.
Interfaces
I will pick up the hook.
You will see something new.
Two things. And I call them
Thing One and Thing Two.
These Things will not bite you.
They want to have fun.
—Theodor Seuss Geisel
COMPUTER SCIENCE DOES NOT SUFFER the great history of many other disci-
plines. While other subjects have well-founded paradigms and methods, com-
puter science still struggles with one important question: What is the best method
to write programs? To date, we have no best answer. The focus of language de-
signers is to develop programming languages that are simple to use but provide
the power to accurately and efficiently describe the details of large programs
and applications. The development of Java is one such effort.
Throughout this text we focus on developing data structures using object-
oriented programming. Using this paradigm the programmer spends time devel- OOP:
oping templates for structures called classes. The templates are then used to
construct instances or objects. A majority of the statements in object-oriented
programs involve sending messages to objects to have them report or change
their state. Running a program involves, then, the construction and coordina-
tion of objects. In this way languages like Java are object-oriented.
Object-oriented
programming.
In all but the smallest programming projects, abstraction is a useful tool
In programming languages including Pascal,
for writing working programs.
Scheme, and C, the details of a program’s implementation are hidden away in
its procedures or functions. This approach involves procedural abstraction. In
object-oriented programming the details of the implementation of data struc-
tures are hidden away within its objects. This approach involves data abstrac-
tion. Many modern programming languages use object orientation to support
basic abstractions of data. We review the details of data abstraction and the
design of formal interfaces for objects in this chapter.
6
The Object-Oriented Method
Macintosh and
UNIX store
strings
differently.
1.1 Data Abstraction and Encapsulation
If you purchase a donut from Morningside Bakery in Pittsfield, Massachusetts,
you can identify it as a donut without knowing its ingredients. Donuts are
circular, breadlike, and sweet. The particular ingredients in a donut are of little
concern to you. Of course, Morningside is free to switch from one sweetener to
another, as long as the taste is preserved.1 The donut’s ingredients list and its
construction are details that probably do not interest you.
Likewise, it is often unimportant to know how data structures are imple-
mented in order to appreciate their use. For example, most of us are familiar
with the workings or semantics of strings or arrays, but, if pressed, we might
find it difficult to describe their mechanics: Do all consecutive locations in the
array appear close together in memory in your computer, or are they far apart?
The answer is: it is unimportant. As long as the array behaves like an array or
the string behaves like a string we are happy. The less one knows about how
arrays or strings are implemented, the less one becomes dependent on a partic-
ular implementation. Another way to think about this abstractly is that the data
structure lives up to an implicit “contract”: a string is an ordered list of charac-
ters, or elements of an array may be accessed in any order. The implementor of
the data structure is free to construct it in any reasonable way, as long as all the
terms of the contract are met. Since different implementors are in the habit of
making very different implementation decisions, anything that helps to hide the
implementation details—any means of using abstraction—serves to make the
world a better place to program.
When used correctly, object-oriented programming allows the programmer
to separate the details that are important to the user from the details that are
only important to the implementation. Later in this book we shall consider very
general behavior of data structures; for example, in Section 10.1 we will study
structures that allow the user only to remove the most recently added item.
Such behavior is inherent to our most abstract understanding of how the data
structure works. We can appreciate the unique behavior of this structure even
though we haven’t yet discussed how these structures might be implemented.
Those abstract details that are important to the user of the structure—including
abstract semantics of the methods—make up its contract or interface. The in-
terface describes the abstract behavior of the structure. Most of us would agree
that while strings and arrays are very similar structures, they behave differently:
you can shrink or expand a string, while you cannot directly do the same with
an array; you can print a string directly, while printing an array involves explic-
itly printing each of its elements. These distinctions suggest they have distinct
abstract behaviors; there are distinctions in the design of their interfaces.
The unimportant details hidden from the user are part of what makes up
the implementation. We might decide (see Figure 1.1) that a string is to be
1 Apple cider is often used to flavor donuts in New England, but that decision decidedly changes
the flavor of the donut for the better. Some of the best apple cider donuts can be found at Atkin’s
apple farm in Amherst, Massachusetts.
1.2 The Object Model
7
Figure 1.1
its length. The terminated string’s length is determined by an end-of-string mark.
Two methods of implementing a string. A counted string explicitly records
constructed from a large array of characters with an attendant character count.
Alternatively, we might specify the length implicitly by terminating the string
with a special end-of-string mark that is not used for any other purpose. Both
of these approaches are perfectly satisfactory, but there are trade-offs. The first
implementation (called a counted string) has its length stored explicitly, while
the length of the second implementation (called a terminated string) is implied.
It takes longer to determine the length of a terminated string because we have to
search for the end-of-string mark. On the other hand, the size of a terminated
string is limited only by the amount of available memory, while the longest
counted string is determined by the range of integers that can be stored in its
length field (often this is only several hundred characters). If implementors can
hide these details, users do not have to be distracted from their own important
design work. As applications mature, a fixed interface to underlying objects
allows alternative implementations of the object to be considered.
Data abstraction in languages like Java allows a structure to take responsibil-
ity for its own state. The structure knows how to maintain its own state without
bothering the programmer. For example, if two strings have to be concatenated
into a single string structure, a request might have to be made for a new allot-
ment of memory. Thankfully, because strings know how to perform operations
on themselves, the user doesn’t have to worry about managing memory.
1.2 The Object Model
To facilitate the construction of well-designed objects, it is useful to have a de-
sign method in mind. As alluded to earlier, we will often visualize the data for
our program as being managed by its objects. Each object manages its own data
that determine its state. A point on a screen, for example, has two coordinates.
Data1234567891011121314n01234567891011121314nLICKETYSPLIT!E O SLICKETYSPLIT!13Counted stringTerminated stringDataCount08
The Object-Oriented Method
A medical record maintains a name, a list of dependents, a medical history, and
a reference to an insurance company. A strand of genetic material has a se-
quence of base pairs. To maintain a consistent state we imagine the program
manipulates the data within its objects only through messages or method calls
to the objects. A string might receive a message “tell me your length,” while
a medical record might receive a “change insurance” message. The string mes-
sage simply accesses information, while the medical record method may involve
changing several pieces of information in this and other objects in a consistent
manner. If we directly modify the reference to the insurance company, we may
forget to modify similar references in each of the dependents. For large applica-
tions with complex data structures, it can be extremely difficult to remember to
coordinate all the operations that are necessary to move a single complex object
from one consistent state to another. We opt, instead, to have the designer of
the data structure provide us a method for carefully moving between states; this
method is activated in response to a high-level message sent to the object.
This text, then, focuses on two important topics: (1) how we implement and
evaluate objects with methods that are logically complex and (2) how we might
use the objects we create. These objects typically represent data structures, our
primary interest. Occasionally we will develop control structures—structures
whose purpose is to control the manipulation of other objects. Control struc-
tures are an important concept and are described in detail in Chapter 8.
1.3 Object-Oriented Terminology
In Java, data abstraction is accomplished through encapsulation of data in an
object—an instance of a class. Like a record in other languages, an object has
fields. Unlike records, objects also contain methods. Fields and methods of an
object may be declared public, which means that they are visible to entities
outside the class, or protected, in which case they may only be accessed by
code within methods of the class.2 A typical class declaration is demonstrated
by the following simple class that keeps track of the ratio of two integer values:
public class Ratio
{
Ratio
protected int numerator;
protected int denominator; // denominator of ratio
// numerator of ratio
public Ratio(int top, int bottom)
// pre: bottom != 0
// post: constructs a ratio equivalent to top::bottom
{
numerator = top;
denominator = bottom;
reduce();
2 This is not quite the truth. For a discussion of the facts, see Appendix B.8.
1.3 Object-Oriented Terminology
9
}
public int getNumerator()
// post: return the numerator of the fraction
{
return numerator;
}
public int getDenominator()
// post: return the denominator of the fraction
{
return denominator;
}
public double getValue()
// post: return the double equivalent of the ratio
{
return (double)numerator/(double)denominator;
}
public Ratio add(Ratio other)
// pre: other is nonnull
// post: return new fraction--the sum of this and other
{
return new Ratio(this.numerator*other.denominator+
this.denominator*other.numerator,
this.denominator*other.denominator);
}
protected void reduce()
// post: numerator and denominator are set so that
// the greatest common divisor of the numerator and denominator is 1
{
int divisor = gcd(numerator,denominator);
if (denominator < 0) divisor = -divisor;
numerator /= divisor;
denominator /= divisor;
}
protected static int gcd(int a, int b)
// post: computes the greatest integer value that divides a and b
{
if (a < 0) return gcd(-a,b);
if (a == 0) {
if (b == 0) return 1;
else return b;
}
if (b < a) return gcd(b,a);
return gcd(b%a,a);
}
10
The Object-Oriented Method
public String toString()
// post: returns a string that represents this fraction.
{
return getNumerator()+"/"+getDenominator();
}
}
First, a Ratio object maintains the numerator and denominator as protected
ints that are not directly modifiable by the user. The Ratio method is a con-
structor: a method whose name is the same as that of the class. (The formal
comments at the top of methods are pre- and postconditions; we discuss these
in detail in Chapter 2.) The constructor is called whenever a new Ratio object is
formed. Constructors initialize all the fields of the associated object, placing the
object into a predictable and consistent initial state. We declare the construc-
tors for a class public. To construct a new Ratio object, users will have to call
these methods. The value method returns a double that represents the ratio,
while the getNumerator and getDenominator methods fetch the current values
of the numerator and denominator of the fraction. The add method is useful for
adding one Ratio to another; the result is a newly constructed Ratio object.
Finally, the toString method generates the preferred printable representation
of the object; we have chosen to represent it in fractional form.
Two methods, reduce and gcd, are utility methods. The gcd method com-
putes the greatest common divisor of two values using Euclid’s method, one of
the oldest numerical algorithms used today. It is used by the reduce method to
reduce the numerator and denominator to lowest terms by removing any com-
mon factors. Both are declared protected because computing the reduction is
not a necessary (or obvious) operation to be performed on ratios of integers;
it’s part of the implementation. The gcd method is declared static because
the algorithm can be used at any time—its utility is independent of the number
of Ratio objects that exist in our program. The reduce method, on the other
hand, works only with a Ratio object.
Exercise 1.1 Nearly everything can be improved. Are there improvements that
might be made to the gcd method? Can you write the method iteratively? Is
iteration an improvement?
As with the Ratio class, data fields are usually declared protected. To ma-
nipulate protected fields the user must invoke public methods. The following
example demonstrates the manipulation of the Ratio class:
public static void main(String[] args)
{
Ratio r = new Ratio(1,1);
r = new Ratio(1,2);
r.add(new Ratio(1,3));
r = r.add(new Ratio(2,8));
System.out.println(r.getValue()); // 0.75 printed
// r == 1.0
// r == 0.5
// sum computed, but r still 0.5
// r == 0.75
1.4 A Special-Purpose Class: A Bank Account
11
System.out.println(r.toString());
System.out.println(r);
// calls toString()
// calls toString()
}
To understand the merit of this technique of class design, we might draw an
analogy between a well-designed object and a lightbulb for your back porch.
The protected fields and methods of an object are analogous to the internal de-
sign of the bulb. The observable features, including the voltage and the size of
the socket, are provided without giving any details about the implementation
If light socket manufacturers depended on a particular imple-
of the object.
mentation of lightbulbs—for example the socket only supported bright xenon
bulbs—it might ultimately restrict the variety of suppliers of lightbulbs in the
future. Likewise, manufacturers of lightbulbs should be able to have a cer-
tain freedom in their implementation: as long as they only draw current in an
agreed-upon way and as long as their bulb fits the socket, they should be free
to use whatever design they want. Today, most lamps take either incandescent
or fluorescent bulbs.
In the same way that fields are encapsulated by a class, classes may be encap-
sulated by a package. A package is a collection of related classes that implement
some set of structures with a common theme. The classes of this text, for ex-
ample, are members of the structure package. In the same way that there are
users of classes, there are users of packages, and much of the analogy holds. In
particular, classes may be declared public, in which case they may be used by
anyone who imports the package into their program. If a class is not public, it
is automatically considered protected. These protected classes may only be
constructed and used by other classes within the same package.
1.4 A Special-Purpose Class: A Bank Account
We now look at the detailed construction of a simplistic class: a BankAccount.
Many times, it is necessary to provide a tag associated with an instance of a
data structure. You might imagine that your bank balance is kept in a database
at your bank. When you get money for a trip through the Berkshires, you swipe
your card through an automated teller bringing up your account. Your account Automated
number, presumably, is unique to your account. Nothing about you or your
banking history is actually stored in your account number. Instead, that num-
ber is used to find the record linked to your account: the bank searches for a
structure associated with the number you provide. Thus a BankAccount is a sim-
ple, but important, data structure. It has an account (an identifier that never
changes) and a balance (that potentially does change). The public methods of
such a structure are as follows:
teller: a robotic
palm reader.
public class BankAccount
{
public BankAccount(String acc, double bal)
// pre: account is a string identifying the bank account
BankAccount
12
The Object-Oriented Method
// balance is the starting balance
// post: constructs a bank account with desired balance
public boolean equals(Object other)
// pre: other is a valid bank account
// post: returns true if this bank account is the same as other
public String getAccount()
// post: returns the bank account number of this account
public double getBalance()
// post: returns the balance of this bank account
public void deposit(double amount)
// post: deposit money in the bank account
public void withdraw(double amount)
// pre: there are sufficient funds in the account
// post: withdraw money from the bank account
}
The substance of these methods has purposefully been removed because, again,
it is unimportant for us to know exactly how a BankAccount is implemented.
We have ways to construct and compare BankAccounts, as well as ways to read
the account number or balance, or update the balance.
Let’s look at the implementation of these methods, individually. To build a
new bank account, you must use the new operator to call the constructor with
two parameters. The account number provided never changes over the life of
the BankAccount—if it were necessary to change the value of the account num-
ber, a new BankAccount would have to be made, and the balance would have to
be transferred from one to the other. The constructor plays the important role
of performing the one-time initialization of the account number field. Here is
the code for a BankAccount constructor:
protected String account;
protected double balance; // the balance associated with account
// the account number
public BankAccount(String acc, double bal)
// pre: account is a string identifying the bank account
// balance is the starting balance
// post: constructs a bank account with desired balance
{
account = acc;
balance = bal;
}
Two fields—account and balance—of the BankAccount object are responsible
for maintaining the object’s state. The account keeps track of the account num-
ber, while the balance field maintains the balance.
1.4 A Special-Purpose Class: A Bank Account
13
Since account numbers are unique to BankAccounts, to check to see if two
accounts are “the same,” we need only compare the account fields. Here’s the
code:
public boolean equals(Object other)
// pre: other is a valid bank account
// post: returns true if this bank account is the same as other
{
BankAccount that = (BankAccount)other;
// two accounts are the same if account numbers are the same
return this.account.equals(that.account);
}
Notice that the BankAccount equals method calls the equals method of the key,
a String. Both BankAccount and String are nonprimitive types, or examples
of Objects. Every object in Java has an equals method. If you don’t explicitly
provide one, the system will write one for you. Generally speaking, one should
assume that the automatically written or default equals method is of little use.
This notion of “equality” of objects is often based on the complexities of our
abstraction; its design must be considered carefully.
One can ask the BankAccount about various aspects of its state by calling its
getAccount or getBalance methods:
public String getAccount()
// post: returns the bank account number of this account
{
return account;
}
public double getBalance()
// post: returns the balance of this bank account
{
return balance;
}
These methods do little more than pass along the information found in the
account and balance fields, respectively. We call such methods accessors. In a
different implementation of the BankAccount, the balance would not have to be
explicitly stored—the value might be, for example, the difference between two
fields, deposits and drafts. Given the interface, it is not much of a concern to
the user which implementation is used.
We provide two more methods, deposit and withdraw, that explicitly mod-
ify the current balance. These are mutator methods:
public void deposit(double amount)
// post: deposit money in the bank account
{
balance = balance + amount;
}
14
The Object-Oriented Method
public void withdraw(double amount)
// pre: there are sufficient funds in the account
// post: withdraw money from the bank account
{
balance = balance - amount;
}
Because we would like to change the balance of the account, it is important to
have a method that allows us to modify it. On the other hand, we purposefully
don’t have a setAccount method because we do not want the account number
to be changed without a considerable amount of work (work that, by the way,
models reality).
Here is a simple application that determines whether it is better to deposit
$100 in an account that bears 5 percent interest for 10 years, or to deposit $100
in an account that bears 2 1
2 percent interest for 20 years. It makes use of the
BankAccount object just outlined:
public static void main(String[] args)
{
// Question: is it better to invest $100 over 10 years at 5%
//
BankAccount jd = new BankAccount("Jain Dough",100.00);
BankAccount js = new BankAccount("Jon Smythe",100.00);
or to invest $100 over 20 years at 2.5% interest?
for (int years = 0; years < 10; years++)
{
jd.deposit(jd.getBalance() * 0.05);
}
for (int years = 0; years < 20; years++)
{
js.deposit(js.getBalance() * 0.025);
}
System.out.println("Jain invests $100 over 10 years at 5%.");
System.out.println("After 10 years " + jd.getAccount() +
" has $" + jd.getBalance());
System.out.println("Jon invests $100 over 20 years at 2.5%.");
System.out.println("After 20 years " + js.getAccount() +
" has $" + js.getBalance());
}
Exercise 1.2 Which method of investment would you pick?
1.5 A General-Purpose Class: An Association
At least Dr.
Seuss started
with 50 words!
The following small application implements a Pig Latin translator based on a
dictionary of nine words. The code makes use of an array of Associations,
each of which establishes a relation between an English word and its Pig Latin
1.5 A General-Purpose Class: An Association
15
translation. For each string passed as the argument to the main method, the
dictionary is searched to determine the appropriate translation.
public class atinLay {
// a pig latin translator for nine words
public static void main(String args[])
{
// build and fill out an array of nine translations
Association dict[] = new Association[9];
dict[0] = new Association("a","aay");
dict[1] = new Association("bad","adbay");
dict[2] = new Association("had","adhay");
dict[3] = new Association("dad","adday");
dict[4] = new Association("day","ayday");
dict[5] = new Association("hop","ophay");
dict[6] = new Association("on","onay");
dict[7] = new Association("pop","oppay");
dict[8] = new Association("sad","adsay");
atinlay
for (int argn = 0; argn < args.length; argn++)
{
// for each argument
for (int dictn = 0; dictn < dict.length; dictn++)
{
// check each dictionary entry
if (dict[dictn].getKey().equals(args[argn]))
System.out.println(dict[dictn].getValue());
}
}
}
}
When this application is run with the arguments hop on pop, the results are
ophay
onay
oppay
While this application may seem rather trivial, it is easy to imagine a large-scale
application with similar needs.3
We now consider the design of the Association. Notice that while the type
of data maintained is different, the purpose of the Association is very similar
to that of the BankAccount class we discussed in Section 1.4. An Association
is a key-value pair such that the key cannot be modified. Here is the interface
for the Association class:
import java.util.Map;
3 Pig Latin has played an important role in undermining court-ordered restrictions placed on music
piracy. When Napster—the rebel music trading firm—put in checks to recognize copyrighted music
by title, traders used Pig Latin translators to foil the recognition software!
Association
16
The Object-Oriented Method
public class Association implements Map.Entry
{
public Association(Object key, Object value)
// pre: key is non-null
// post: constructs a key-value pair
public Association(Object key)
// pre: key is non-null
// post: constructs a key-value pair; value is null
public boolean equals(Object other)
// pre: other is non-null Association
// post: returns true iff the keys are equal
public Object getValue()
// post: returns value from association
public Object getKey()
// post: returns key from association
public Object setValue(Object value)
// post: sets association’s value to value
}
For the moment, we will ignore the references to Map and Map.entry; these will
be explained later, in Chapter 15. What distinguishes an Association from a
more specialized class, like BankAccount, is that the fields of an Association
are type Object. The use of the word Object in the definition of an Association
makes the definition very general: any value that is of type Object—any non-
primitive data type in Java—can be used for the key and value fields.
Unlike the BankAccount class, this class has two different constructors:
protected Object theKey; // the key of the key-value pair
protected Object theValue; // the value of the key-value pair
public Association(Object key, Object value)
// pre: key is non-null
// post: constructs a key-value pair
{
Assert.pre(key != null, "Key must not be null.");
theKey = key;
theValue = value;
}
public Association(Object key)
// pre: key is non-null
// post: constructs a key-value pair; value is null
{
this(key,null);
}
1.5 A General-Purpose Class: An Association
17
The first constructor—the constructor distinguished by having two parame-
ters—allows the user to construct a new Association by initializing both fields.
On occasion, however, we may wish to have an Association whose key field is
set, but whose value field is left referencing nothing. (An example might be a
medical record: initially the medical history is incomplete, perhaps waiting to
be forwarded from a previous physician.) For this purpose, we provide a sin-
gle parameter constructor that sets the value field to null. Note that we use
this(key,null) as the body. The one-parameter constructor calls this object’s
two-parameter constructor with null as the second parameter. We write the
constructors in this dependent manner so that if the underlying implementation
of the Association had to be changed, only the two-parameter method would
have to be updated. It also reduces the complexity of the code and saves your
fingerprints!
Now, given a particular Association, it is useful to be able to retrieve the
key or value. Since the implementation is hidden, no one outside the class is
able to see it. Users must depend on the accessor methods to observe the data.
public Object getValue()
// post: returns value from association
{
return theValue;
}
public Object getKey()
// post: returns key from association
{
return theKey;
}
When necessary, the method setValue can be used to change the value associ-
ated with the key. Thus, the setValue method simply takes its parameter and
assigns it to the value field:
public Object setValue(Object value)
// post: sets association’s value to value
{
Object oldValue = theValue;
theValue = value;
return oldValue;
}
There are other methods that are made available to users of the Association
class, but we will not discuss the details of that code until later. Some of the
methods are required, some are useful, and some are just nice to have around.
While the code may look complicated, we take the time to implement it cor-
rectly, so that we will not have to reimplement it in the future.
Principle 2 Free the future: reuse code.
NNWSWSENEWSE18
The Object-Oriented Method
It is difficult to fight the temptation to design data structures from scratch. We
shall see, however, that many of the more complex structures would be very
difficult to construct if we could not base our implementations on the results of
previous work.
1.6 Sketching an Example: A Word List
Suppose we’re interested in building a game of Hangman. The computer selects
random words and we try to guess them. Over several games, the computer
should pick a variety of words and, as each word is used, it should be removed
from the word list. Using an object-oriented approach, we’ll determine the
essential features of a WordList, the Java object that maintains our list of words.
Our approach to designing the data structures has the following five informal
steps:
1. Identify the types of operations you expect to perform on your object.
What operations access your object only by reading its data? What opera-
tions might modify or mutate your objects?
2. Identify, given your operations, those data that support the state of your
Information about an object’s state is carried within the object
object.
between operations that modify the state. Since there may be many ways
to encode the state of your object, your description of the state may be
very general.
3. Identify any rules of consistency. In the Ratio class, for example, it would
not be good to have a zero denominator. Also, the numerator and denom-
inator should be in lowest terms.
4. Determine the number and form of the constructors. Constructors are
synthetic: their sole responsibility is to get a new object into a good initial
and consistent state. Don’t forget to consider the best state for an object
constructed using the parameterless default constructor.
5. Identify the types and kinds of information that, though declared pro-
tected, can efficiently provide the information needed by the public
methods.
Important choices about the internals of a data structure are
usually made at this time. Sometimes, competing approaches are devel-
oped until a comparative evaluation can be made. That is the subject of
much of this book.
The operations necessary to support a list of words can be sketched out
easily, even if we don’t know the intimate details of constructing the Hangman
game itself. Once we see how the data structure is used, we have a handle on
the design of the interface. Thinking about the overall design of Hangman, we
can identify the following general use of the WordList object:
1.6 Sketching an Example: A Word List
19
WordList list;
String targetWord;
// declaration
list = new WordList(10);
list.add("disambiguate");
list.add("inputted");
list.add("subbookkeeper"); // now that’s coollooking!
while (!list.isEmpty())
{
// game loop
// construction
// is this a word? how about ambiguate?
// really? what verbification!
WordList
targetWord = list.selectAny();
// ...play the game using target word...
list.remove(targetWord);
// update
// selection
}
Let’s consider these lines. One of the first lines (labeled declaration) de-
clares a reference to a WordList. For a reference to refer to an object, the object
must be constructed. We require, therefore, a constructor for a WordList. The
construction line allocates an initially empty list of words ultimately contain-
ing as many as 10 words. We provide an upper limit on the number of words
that are potentially stored in the list. (We’ll see later that providing such infor-
mation can be useful in designing efficient data structures.) On the next three
lines, three (dubious) words are added to the list.
The while loop accomplishes the task of playing Hangman with the user.
This is possible as long as the list of words is not empty. We use the isEmpty
method to test this fact. At the beginning of each round of Hangman, a random
word is selected (selectAny), setting the targetWord reference. To make things
interesting, we presume that the selectAny method selects a random word each
time. Once the round is finished, we use the remove method to remove the word
from the word list, eliminating it as a choice in future rounds.
There are insights here. First, we have said very little about the Hangman
game other than its interaction with our rather abstract list of words. The details
of the screen’s appearance, for example, do not play much of a role in under-
standing how the WordList structure works. We knew that a list was necessary
for our program, and we considered the program from the point of view of the
object. Second, we don’t really know how the WordList is implemented. The
words may be stored in an array, or in a file on disk, or they may use some tech-
nology that we don’t currently understand. It is only important that we have
faith that the structure can be implemented. We have sketched out the method
headers, or signatures, of the WordList interface, and we have faith that an im-
plementation supporting the interface can be built. Finally we note that what
we have written is not a complete program. Still, from the viewpoint of the
WordList structure, there are few details of the interface that are in question.
A reasoned individual should be able to look at this design and say “this will
work—provided it is implemented correctly.” If a reviewer of the code were to
ask a question about how the structure works, it would lead to a refinement of
our understanding of the interface.
We have, then, the following required interface for the WordList class:
20
The Object-Oriented Method
public class WordList
{
public WordList(int size)
// pre: size >= 0
// post: construct a word list capable of holding "size" words
public boolean isEmpty()
// post: return true iff the word list contains no words
public void add(String s)
// post: add a word to the word list, if it is not already there
public String selectAny()
// pre: the word list is not empty
// post: return a random word from the list
public void remove(String word)
// pre: word is not null
// post: remove the word from the word list
}
We will leave the implementation details of this example until later. You might
consider various ways that the WordList might be implemented. As long as
the methods of the interface can be supported by your data structure, your
implementation is valid.
Exercise 1.3 Finish the sketch of the WordList class to include details about the
state variables.
1.7 Sketching an Example: A Rectangle Class
Suppose we are developing a graphics system that allows the programmer to
draw on a DrawingWindow. This window has, associated with it, a Cartesian
coordinate system that allows us to uniquely address each of the points within
the window. Suppose, also, that we have methods for drawing line segments,
say, using the Line object. How might we implement a rectangle—called a
Rect—to be drawn in the drawing window?
One obvious goal would be to draw a Rect on the DrawingWindow. This
might be accomplished by drawing four line segments. It would be useful to
be able to draw a filled rectangle, or to erase a rectangle (think: draw a filled
rectangle in the background color). We’re not sure how to do this efficiently, but
these latter methods seem plausible and consistent with the notion of drawing.
(We should check to see if it is possible to draw in the background color.) This
leads to the following methods:
Rect
public void fillOn(DrawingTarget d)
// pre: d is a valid drawing window
// post: the rectangle is filled on the drawing window d
1.7 Sketching an Example: A Rectangle Class
21
public void clearOn(DrawingTarget d)
// pre: d is a valid drawing window
// post: the rectangle is erased from the drawing window
public void drawOn(DrawingTarget d)
// pre: d is a valid drawing window
// post: the rectangle is drawn on the drawing window
It might be useful to provide some methods to allow us to perform basic calcu-
lations—for example, we might want to find out if the mouse arrow is located
within the Rect. These require accessors for all the obvious data. In the hope
that we might use a Rect multiple times in multiple locations, we also provide
methods for moving and reshaping the Rect.
public boolean contains(Pt p)
// pre: p is a valid point
// post: true iff p is within the rectangle
public int left()
// post: returns left coordinate of the rectangle
public void left(int x)
// post: sets left to x; dimensions remain unchanged
public int width()
// post: returns the width of the rectangle
public void width(int w)
// post: sets width of rectangle, center and height unchanged
public void center(Pt p)
// post: sets center of rect to p; dimensions remain unchanged
public void move(int dx, int dy)
// post: moves rectangle to left by dx and down by dy
public void moveTo(int left, int top)
// post: moves left top of rectangle to (left,top);
//
dimensions are unchanged
public void extend(int dx, int dy)
// post: moves sides of rectangle outward by dx and dy
Again, other approaches might be equally valid. No matter how we might rep-
resent a Rect, however, it seems that all rectangular regions with horizontal
and vertical sides can be specified with four integers. We can, then, construct a
Rect by specifying, say, the left and top coordinates and the width and height.
For consistency’s sake, it seems appropriate to allow rectangles to be drawn
anywhere (even off the screen), but the width and height should be non-negative
22
The Object-Oriented Method
values. We should make sure that these constraints appear in the documenta-
tion associated with the appropriate constructors and methods. (See Section 2.2
for more details on how to write these comments.)
Given our thinking, we have some obvious Rect constructors:
public Rect()
// post: constructs a trivial rectangle at origin
public Rect(Pt p1, Pt p2)
// post: constructs a rectangle between p1 and p2
public Rect(int x, int y, int w, int h)
// pre: w >= 0, h >= 0
// post: constructs a rectangle with upper left (x,y),
//
width w, height h
We should feel pleased with the progress we have made. We have developed
the signatures for the rectangle interface, even though we have no immediate
application. We also have some emerging answers on approaches to implement-
ing the Rect internally. If we declare our Rect data protected, we can insulate
ourselves from changes suggested by inefficiencies we may yet discover.
Exercise 1.4 Given this sketch of the Rect interface, how would you declare the
private data associated with the Rect object? Given your approach, describe how
you might implement the center(int x, int y) method.
1.8 Interfaces
Sometimes it is useful to describe the interface for a number of different classes,
without committing to an implementation. For example, in later sections of this
text we will implement a number of data structures that are able to be modified
by adding or removing values. We can, for all of these classes, specify a few of
their fundamental methods by using the Java interface declaration:
public interface Structure
{
Structure
public int size();
// post: computes number of elements contained in structure
public boolean isEmpty();
// post: return true iff the structure is empty
public void clear();
// post: the structure is empty
public boolean contains(Object value);
// pre: value is non-null
// post: returns true iff value.equals some value in structure
1.8 Interfaces
23
public void add(Object value);
// pre: value is non-null
// post: value has been added to the structure
//
replacement policy is not specified
public Object remove(Object value);
// pre: value is non-null
// post: an object equal to value is removed and returned, if found
public java.util.Enumeration elements();
// post: returns an enumeration for traversing structure;
//
//
all structure package implementations return
an AbstractIterator
public Iterator iterator();
// post: returns an iterator for traversing structure;
//
//
all structure package implementations return
an AbstractIterator
public Collection values();
// post: returns a Collection that may be used with
//
Java’s Collection Framework
}
Notice that the body of each method has been replaced by a semicolon.
It
is, in fact, illegal to specify any code in a Java interface. Specifying just the
method signatures in an interface is like writing boilerplate for a contract with-
out committing to any implementation. When we decide that we are interested
in constructing a new class, we can choose to have it implement the Structure
interface. For example, our WordList structure of Section 1.6 might have made
use of our Structure interface by beginning its declaration as follows:
public class WordList implements Structure
When the WordList class is compiled by the Java compiler, it checks to see that
each of the methods mentioned in the Structure interface—add, remove, size,
and the others—is actually implemented. In this case, only isEmpty is part of
the WordList specification, so we must either (1) not have WordList implement
the Structure interface or (2) add the methods demanded by Structure.
Interfaces may be extended. Here, we have a possible definition of what it
WordList
means to be a Set:
public interface Set extends Structure
{
public void addAll(Structure other);
// pre: other is non-null
// post: values from other are added into this set
Set
24
The Object-Oriented Method
public boolean containsAll(Structure other);
// pre: other is non-null
// post: returns true if every value in set is in other
public void removeAll(Structure other);
// pre: other is non-null
// post: values of this set contained in other are removed
public void retainAll(Structure other);
// pre: other is non-null
// post: values not appearing in the other structure are removed
}
A Set requires several set-manipulation methods—addAll (i.e., set union) retain-
All (set intersection), and removeAll (set difference)—as well as the meth-
ods demanded by being a Structure. If we implement these methods for the
WordList class and indicate that WordList implements Set, the WordList class
could be used wherever either a Structure or Set is required. Currently, our
WordList is close to, but not quite, a Structure. Applications that demand
the functionality of a Structure will not be satisfied with a WordList. Having
the class implement an interface increases the flexibility of its use. Still, it may
require considerable work for us to upgrade the WordList class to the level of
a Structure. It may even work against the design of the WordList to provide
the missing methods. The choices we make are part of an ongoing design pro-
cess that attempts to provide the best implementations of structures to meet the
demands of the user.
1.9 Who Is the User?
When implementing data structures using classes and interfaces, it is sometimes
hard to understand why we might be interested in hiding the implementation.
After all, perhaps we know that ultimately we will be the only programmers
making use of these structures. That might be a good point, except that if
you are really a successful programmer, you will implement the data structure
flawlessly this week, use it next week, and not return to look at the code for
a long time. When you do return, your view is effectively that of a user of the
code, with little or no memory of the implementation.
One side effect of this relationship is that we have all been reminded of the
need to write comments. If you do not write comments, you will not be able to
read the code. If, however, you design, document, and implement your interface
carefully, you might not ever have to look at the implementation! That’s good
news because, for most of us, in a couple of months our code is as foreign to
us as if someone else had implemented it. The end result: consider yourself a
user and design and abide by your interface wherever possible. If you know of
some public field that gives a hint of the implementation, do not make use of it.
Instead, access the data through appropriate methods. You will be happy you
1.10 Conclusions
25
did later, when you optimize your implementation.
Principle 3 Design and abide by interfaces as though you were the user.
A quick corollary to this statement is the following:
Principle 4 Declare data fields protected.
If the data are protected, you cannot access them from outside the class, and
you are forced to abide by the restricted access of the interface.
1.10 Conclusions
The construction of substantial applications involves the development of com-
plex and interacting structures. In object-oriented languages, we think of these
structures as objects that communicate through the passing of messages or,
more formally, the invocation of methods.
We use object orientation in Java to write the structures found in this book.
It is possible, of course, to design data structures without object orientation, but
any effective data structuring model ultimately depends on the use of some form
of abstraction that allows the programmer to avoid considering the complexities
of particular implementations.
In many languages, including Java, data abstraction is supported by sepa-
rating the interface from the implementation of the data structure. To ensure
that users cannot get past the interface to manipulate the structure in an uncon-
trolled fashion, the system controls access to fields, methods, and classes. The
implementor plays an important role in making sure that the structure is usable,
given the interface. This role is so important that we think of implementation
as supporting the interface—sometimes usefully considered a contract between
the implementor and the user. This analogy is useful because, as in the real
world, if contracts are violated, someone gets upset!
Initial design of the interfaces for data structures arises from considering
how they are used in simple applications. Those method calls that are required
by the application determine the interface for the new structure and constrain,
in various ways, the choices we make in implementing the object.
In our implementation of an Association, we can use the Object class—
that class inherited by all other Java classes—to write very general data struc-
tures. The actual type of value that is stored in the Association is determined
by the values passed to the constructors and mutators of the class. This abil-
ity to pass a subtype to any object that requires a super type is a strength of
object-oriented languages—and helps to reduce the complexity of code.
NNWSWSENEWSENNWSWSENEWSE26
The Object-Oriented Method
Self Check Problems
Solutions to these problems begin on page 441.
What is meant by abstraction?
1.1
What is procedural abstraction?
1.2
What is data abstraction?
1.3
How does Java support the concept of a message?
1.4
What is the difference between an object and a class?
1.5
What makes up a method’s signature?
1.6
What is the difference between an interface and an implementation?
1.7
What is the difference between an accessor and a mutator?
1.8
A general purpose class, such as an Association, often makes use of
1.9
parameters of type Object. Why?
1.10 What is the difference between a reference and an object?
1.11 Who uses a class?
Problems
Which of the following are primitive Java types: int, Integer, double,
Solutions to the odd-numbered problems begin on page 451.
1.1
Double, String, char, Association, BankAccount, boolean, Boolean?
1.2
calls?
Which of the following variables are associated with valid constructor
BankAccount a,b,c,d,e,f;
Association g,h;
a = new BankAccount("Bob",300.0);
b = new BankAccount(300.0,"Bob");
c = new BankAccount(033414,300.0);
d = new BankAccount("Bob",300);
e = new BankAccount("Bob",new Double(300));
f = new BankAccount("Bob",(double)300);
g = new Association("Alice",300.0);
h = new Association("Alice",new Double(300));
1.3
For each pair of classes, indicate which class extends the other:
a. java.lang.Number, java.lang.Double
b. java.lang.Number, java.lang.Integer
c. java.lang.Number, java.lang.Object
d. java.util.Stack, java.util.Vector
1.10 Conclusions
27
e. java.util.Hashtable, java.util.Dictionary
Rewrite the compound interest program (discussed when considering
1.4
BankAccounts in Section 1.4) so that it uses Associations.
Write a program that attempts to modify one of the private fields of
1.5
an Association. When does your environment detect the violation? What
happens?
Finish the design of a Ratio class that implements a ratio between
1.6
two integers. The class should support standard math operations: addition,
subtraction, multiplication, and division. You should also be able to construct
Ratios from either a numerator-denominator pair, or a single integer, or with
no parameter at all (what is a reasonable default value?).
1.7
a, b, the probability that a
write a program to compute an approximation to π.
Amazing fact: If you construct a Ratio from two random integers, 0 <
b is already in reduced terms is 6
π2 . Use this fact to
Design a class to represent a U.S. telephone number.
It should sup-
1.8
port three types of constructors—one that accepts three numbers, represent-
ing area code, exchange, and extension; another that accepts two integers,
representing a number within your local area code; and a third constructor
that accepts a string of letters and numbers that represent the number (e.g.,
"900-410-TIME"). Provide a method that determines if the number is provided
toll-free (such numbers have area codes of 800, 866, 877, 880, 881, 882, or
888).
Sometimes it is useful to measure the length of time it takes for a piece
1.9
of code to run. (For example, it may help determine where optimizations of
your code would be most effective.) Design a Stopwatch class to support tim-
ing of events. You should consider use of the nanosecond clock in the Java
environment, System.nanoTime(). Like many stopwatches, it should support
starting, temporary stopping, and a reset. The design of the protected section
of the stopwatch should hide the implementation details.
Design a data structure in Java that represents a musical tone. A tone
1.10
can be completely specified as a number of cycles per second (labeled Hz for
hertz), or the number of half steps above a commonly agreed upon tone, such
as A (in modern times, in the United States, considered to be 440 Hz). Higher
tones have higher frequencies. Two tones are an octave (12 semitones) apart
if one has a frequency twice the other. A half step or semitone increase in tone
√
is 12
2 ≈ 1.06 times higher. Your tone constructors should accept a frequency
(a double) or a number of half steps (an int) above A. Imperfect frequencies
should be tuned to the nearest half step. Once constructed, a tone should be
able to provide its frequency in either cycles per second or half-steps above A.
Extend Problem 1.10 to allow a second parameter to each constructor
1.11
to specify the definition of A upon which the tone’s definition is based. What
modern tone most closely resembles that of modern middle C (9 semitones
below A) if A is defined to be 415 Hz?
28
The Object-Oriented Method
Design a data structure to represent a combination lock. When the
1.12
lock is constructed, it is provided with an arbitrary length array of integers
between 0 and 25 specifying a combination (if no combination is provided,
9 − 0 − 21 − 0 is the default).
Initially, it is locked. Two methods—press
and reset—provide a means of entering a combination: press enters the next
integer to be used toward matching the combination, while reset re-readies
the lock for accepting the first integer of the combination. Only when press is
used to match the last integer of the combination does the lock silently unlock.
Mismatched integers require a call to the reset method before the combination
can again be entered. The isLocked method returns true if and only if the lock
is locked. The lock method locks and resets the lock. In the unlocked state only
the isLocked and lock methods have effect. (Aside: Because of the physical
construction of many combination locks, it is often the case that combinations
have patterns. For example, a certain popular lock is constructed with a three-
number combination. The first and last numbers result in the same remainder x
when divided by 4. The middle number has remainder (x + 2)%4 when divided
by 4!)
Design a data structure to simulate the workings of a car radio. The
1.13
state of the radio is on or off, and it may be used to listen to an AM or FM
station. A dozen modifiable push buttons (identified by integers 1 through 12)
allow the listener to store and recall AM or FM frequencies. AM frequencies can
be represented by multiples of 10 in the range 530 to 1610. FM frequencies are
found at multiples of 0.2 in the range 87.9 to 107.9.
Design a data structure to maintain the position of m coins of radius 1
1.14
through m on a board with n ≥ m squares numbered 0 through n − 1. You may
provide whatever interface you find useful to allow your structure to represent
any placement of coins, including stacks of coins in a single cell. A configuration
is valid only if large coins are not stacked on small coins. Your structure should
have an isValid method that returns true if the coins are in a valid position.
(A problem related to this is discussed in Section 10.2.1.)
Top viewSide view1.11 Laboratory: The Day of the Week
Calculator
Objective. To (re)establish ties with Java: to write a program that reminds us
of the particulars of numeric calculations and array manipulation in Java.
Discussion. In this lab we learn to compute the day of the week for any date
between January 1, 1900, and December 31, 2099.4 During this period of time,
the only calendar adjustment is a leap-year correction every 4 years. (Years
divisible by 100 are normally not leap years, but years divisible by 400 always
are.) Knowing this, the method essentially computes the number of days since
the beginning of the twentieth century in modulo 7 arithmetic. The computed
remainder tells us the day of the week, where 0 is Saturday.
An essential feature of this algorithm involves remembering a short table of
monthly adjustments. Each entry in the table corresponds to a month, where
January is month 1 and December is month 12.
Month
Adjustment
1
1
2
4
3
4
4
0
5
2
6
5
7
0
8
3
9
6
10
1
11
4
12
6
If the year is divisible by 4 (it’s a leap year) and the date is January or February,
you must subtract 1 from the adjustment.
Remembering this table is equivalent to remembering how many days are in
each month. Notice that 144 is 122, 025 is 52, 036 is 62, and 146 is a bit more
than 122. Given this, the algorithm is fairly simple:
1. Write down the date numerically. The date consists of a month between
1 and 12, a day of the month between 1 and 31, and the number of
years since 1900. Grace Hopper, computer language pioneer, was born
December 9, 1906. That would be represented as year 6. Jana the Giraffe,
of the National Zoo, was born on January 18, 2001. That year would be
represented as year 101.
2. Compute the sum of the following quantities:
• the month adjustment from the given table (e.g., 6 for Admiral Hop-
per)
• the day of the month
• the year
4 This particular technique is due to John Conway, of Princeton University. Professor Conway
answers 10 day of the week problems before gaining access to his computer. His record is at the
time of this writing well under 15 seconds for 10 correctly answered questions. See “Scientist
at Work: John H. Conway; At Home in the Elusive World of Mathematics,” The New York Times,
October 12, 1993.
30
The Object-Oriented Method
• the whole number of times 4 divides the year (e.g., 25 for Jana the
Giraffe)
3. Compute the remainder of the sum of step 2, when divided by 7. The
remainder gives the day of the week, where Saturday is 0, Sunday is 1, etc.
Notice that we can compute the remainders before we compute the sum.
You may also have to compute the remainder after the sum as well, but if
you’re doing this in your head, this considerably simplifies the arithmetic.
What day of the week was Tiger Woods born?
1. Tiger’s birth date is 12-30-75.
2. Remembering that 18 × 4 = 72, we write the sum as follows:
6 + 30 + 75 + 18
which is equivalent to the following sum, modulo 7:
6 + 2 + 5 + 4 = 17 ≡ 3 mod 7
3. He was born on day 3, a Tuesday.
Now you practice: Which of Grace and Jana was born on a Thursday? (The
other was born on a Sunday.)
Procedure. Write a Java program that performs Conway’s day of the week chal-
lenge:
1. Develop an object that can hold a date.
2. Write a method to compute a random date between 1900 and 2099. How
will you limit the range of days potentially generated for any particular
month?
3. Write a method of your date class to compute the day of the week associ-
ated with a date. Be careful: the table given in the discussion has January
as month 1, but Java would prefer it to be month 0! Don’t forget to handle
the birthday of Jimmy Dorsey (famous jazzman), February 29, 1904.
4. Your main method should repeatedly (1) print a random date, (2) read a
predicted day of the week (as an integer/remainder), and (3) check the
correctness of the guess. The program should stop when 10 dates have
been guessed correctly and print the elapsed time. (You may wish to set
this threshold lower while you’re testing the program.)
Helpful Hints. You may find the following Java useful:
1. Random integers may be selected using the java.util.Random class:
Random r = new Random();
int month = (Math.abs(r.nextInt()) % 12) + 1;
Jimmy was a
Monday’s child.
1.11 Laboratory: The Day of the Week Calculator
31
You will need to import java.util.Random; at the top of your program
to make use of this class. Be aware that you need to only construct one
random number generator per program run. Also, the random number
generator potentially returns negative numbers. If Math.abs is not used,
these values generate negative remainders.
2. You can find out how many thousandths of seconds have elapsed since
the 1960s, by calling the Java method, System.currentTimeMillis(). It
returns a value of type long. We can use this to measure the duration of
an experiment, with code similar to the following:
In 2001,
1 trillion millis
since the ’60s.
Dig that!
long start = System.currentTimeMillis();
//
// place experiment to be timed here
//
long duration = System.currentTimeMillis()-start;
System.out.println("time: "+(duration/1000.0)+" seconds.");
The granularity of this timer isn’t any better than a thousandth of a second.
Still, we’re probably not in Conway’s league yet.
After you finish your program, you will find you can quickly learn to answer
10 of these day of the week challenges in less than a minute.
Thought Questions. Consider the following questions as you complete the lab:
1. True or not: In Java is it true that (a % 7) == (a - a/7*7) for a >= 0?
2. It’s rough to start a week on Saturday. What adjustments would be nec-
essary to have a remainder of 0 associated with Sunday? (This might
allow a mnemonic of Nun-day, One-day, Twos-day, Wednesday, Fours-day,
Fives-day, Saturday.)
3. Why do you subtract 1 in a leap year if the date falls before March?
4. It might be useful to compute the portion of any calculation associated
with this year, modulo 7. Remembering that value will allow you to opti-
mize your most frequent date calculations. What is the remainder associ-
ated with this year?
For years
divisible by 28:
think zero!
Notes:
Chapter 2
Comments, Conditions,
and Assertions
Preconditions
Postconditions
Concepts:
.
.
. Assertions
. Copyrighting code
Wizards are invited to improve it.
/* This is bogus code.
*/
—Anonymous
CONSIDER THIS: WE CALL OUR PROGRAMS “CODE”! The features of computer
languages, including Java, are designed to help express algorithms in a manner
that a machine can understand. Making a program run more efficiently often
makes it less understandable.
If language design was driven by the need to
make the program readable by programmers, it would be hard to argue against
programming in English.
A comment is a carefully crafted piece of text that describes the state of the
machine, the use of a variable, or the purpose of a control construct. Many
of us, though, write comments for the same reason that we exercise: we feel
guilty. You feel that, if you do not write comments in your code, you “just know”
something bad is going to happen. Well, you are right. A comment you write
today will help you out of a hole you dig tomorrow.
All too often comments are hastily written after the fact, to help under-
stand the code. The time spent thinking seriously about the code has long since
passed, and the comment might not be right. If you write comments before-
hand, while you are designing your code, it is more likely your comments will
describe what you want to do as you carefully think it out. Then, when some-
thing goes wrong, the comment is there to help you figure out the code.
In
fairness, the code and the comment have a symbiotic relationship. Writing one
or the other does not really feel complete, but writing both provides you with
the redundancy of concept: one lucid and one as clear as Java.
The one disadvantage of comments is that, unlike code, they cannot be
checked. Occasionally, programmers come across comments such as “If you
think you understand this, you don’t!” or “Are you reading this?” One could, of
course, annotate programs with mathematical formulas. As the program is com-
piled, the mathematical comments are distilled into very concise descriptions of
Okay, perhaps
French!
Ruth Krauss: “A
hole
is to dig.”
34
Comments, Conditions, and Assertions
Semiformal
convention: a
meeting of tie
haters.
what should be going on. When the output from the program’s code does not
match the result of the formula, something is clearly wrong with your logic. But
which logic? The writing of mathematical comments is a level of detail most
programmers would prefer to avoid.
A compromise is a semiformal convention for comments that provide a rea-
sonable documentation of when and what a program does. In the code associ-
ated with this book, we see one or two comments for each method or function
that describe its purpose. These important comments are the precondition and
postcondition.
2.1 Pre- and Postconditions
The precondition describes, as succinctly as possible in your native tongue, the
conditions under which a method may be called and expected to produce correct
results. Ideally the precondition expresses the state of the program. This state
is usually cast in terms of the parameters passed to the routine. For example,
the precondition on a square root function might be
// pre: x is nonnegative
sqrt
The authors of this square root function expect that the parameter is not a
negative number. It is, of course, legal in Java to call a function or method if
the precondition is not met, but it might not produce the desired result. When
there is no precondition on a procedure, it may be called without failure.
The postcondition describes the state of the program once the routine has
been completed, provided the precondition was met. Every routine should have
some postcondition. If there were not a postcondition, then the routine would
not change the state of the program, and the routine would have no effect!
Always provide postconditions.
Pre- and postconditions do not force you to write code correctly. Nor do they
help you find the problems that do occur. They can, however, provide you with
a uniform method for documenting the programs you write, and they require
more thought than the average comment. More thought put into programs
lowers your average blood pressure and ultimately saves you time you might
spend more usefully playing outside, visiting museums, or otherwise bettering
your mind.
2.2 Assertions
And the
batteries never
needed
replacing.
In days gone by, homeowners would sew firecrackers in their curtains. If the
house were to catch fire, the curtains would burn, setting off the firecrackers. It
was an elementary but effective fire alarm.
An assertion is an assumption you make about the state of your program. In
Java, we will encode the assertion as a call to a function that verifies the state
of the program. That function does nothing if the assertion is true, but it halts
2.2 Assertions
35
your program with an error message if it is false. It is a firecracker to sew in
your program.
If you sew enough assertions into your code, you will get an
early warning if you are about to be burned by your logic.
Principle 5 Test assertions in your code.
The Assert class provides several functions to help you test the state of your
program as it runs:
public class Assert
{
static public void pre(boolean test, String message)
// pre: result of precondition test
// post: does nothing if test true, otherwise abort w/message
Assert
static public void post(boolean test, String message)
// pre: result of postcondition test
// post: does nothing if test true, otherwise abort w/message
static public void condition(boolean test, String message)
// pre: result of general condition test
// post: does nothing if test true, otherwise abort w/message
static public void invariant(boolean test, String message)
// pre: result of an invariant test
// post: does nothing if test true, otherwise abort w/message
static public void fail(String message)
// post: throws error with message
}
Each of pre, post, invariant, and condition methods test to see if its first
argument—the assertion—is true. The message is used to indicate the condition
tested by the assertion. Here’s an example of a check to make sure that the
precondition for the sqrt function was met:
public static double sqrt(double x)
// pre: x is nonnegative
// post: returns the square root of x
{
Assert.pre(x >= 0,"the value is nonnegative.");
double guess = 1.0;
double guessSquared = guess * guess;
while (Math.abs(x-guessSquared) >= 0.00000001) {
// guess is off a bit, adjust
guess += (x-guessSquared)/2.0/guess;
guessSquared = guess*guess;
}
return guess;
}
NNWSWSENEWSE36
Comments, Conditions, and Assertions
Should we call sqrt with a negative value, the assertion fails, the message
is printed out, and the program comes to a halt. Here’s what appears at the
display:
structure.FailedPrecondition:
Assertion that failed: A precondition: the value is nonnegative.
at Assert.pre(Assert.java:17)
at sqrt(examples.java:24)
at main(examples.java:15)
The first two lines of this message indicate that a precondition (that x was non-
negative) failed. This message was printed within Assert.pre on line 17 of the
source, found in Assert.java. The next line of this stack trace indicates that
the call to Assert.pre was made on line 24 of examples.java at the start of
the sqrt function. This is the first line of the sqrt method. The problem is
(probably) on line 15 of the main procedure of examples.java. Debugging our
code should probably start in the main routine.
Beginning with Java 1.4, assertion testing is part of the formal Java language
specification. The assert keyword can be used to perform many of the types of
checks we have described. If, however, you are using an earlier version of Java,
or you expect your users may wish to use a version of Java before version 1.4,
you may find the Assert class to be a more portable approach to the testing of
the conditions of one’s code. A feature of language-based assertion testing is
that the tests can be automatically removed at compile time when one feels se-
cure about the way the code works. This may significantly improve performance
of classes that heavily test conditions.
2.3 Craftsmanship
If you really desire to program well, a first step is to take pride in your work—
pride enough to sign your name on everything you do. Through the centuries,
fine furniture makers signed their work, painters finished their efforts by dab-
bing on their names, and authors inscribed their books. Programmers should
stand behind their creations.
Computer software has the luxury of immediate copyright protection—it is
a protection against piracy, and a modern statement that you stand behind the
belief that what you do is worth fighting for. If you have crafted something as
best you can, add a comment at the top of your code:
// Image compression barrel for downlink to robotic cow tipper.
// (c) 2001, 2002 duane r. bailey
If, of course, you have stolen work from another, avoid the comment and
consider, heavily, the appropriate attribution.
2.4 Conclusions
2.4 Conclusions
37
Effective programmers consider their work a craft. Their constructions are well
considered and documented. Comments are not necessary, but documentation
makes working with a program much easier. One of the most important com-
ments you can provide is your name—it suggests you are taking credit and re-
sponsibility for things you create. It makes our programming world less anony-
mous and more humane.
Special comments, including conditions and assertions, help the user and
implementor of a method determine whether the method is used correctly.
While it is difficult for compilers to determine the “spirit of the routine,” the
implementor is usually able to provide succinct checks of the sanity of the func-
tion. Five minutes of appropriate condition description and checking provided I’ve done my
by the implementor can prevent hours of debugging by the user.
time!
Self Check Problems
Solutions to these problems begin on page 442.
Why is it necessary to provide pre- and postconditions?
What can be assumed if a method has no precondition?
Why is it not possible to have a method with no postcondition?
Object orientation allows us to hide unimportant details from the user.
2.1
2.2
2.3
2.4
Why, then, must we put pre- and postconditions on hidden code?
Problems
Solutions to the odd-numbered problems begin on page 457.
What are the pre- and postconditions for the length method of the
What are the pre- and postconditions for String’s charAt method?
What are the pre- and postconditions for String’s concat method?
What are the pre- and postconditions for the floor function in the
2.1
java.lang.String class?
2.2
2.3
2.4
java.lang.Math class?
Improve the comments on an old program.
2.5
Each of the methods of Assert (pre, post, and condition) takes the
2.6
same parameters. In what way do the methods function differently? (Write a
test program to find out!)
2.7
What are the pre- and postconditions for java.lang.Math.asin class?
2.5 Laboratory: Using Javadoc Commenting
Objective. To learn how to generate formal documentation for your programs.
Discussion. The Javadoc program1 allows the programmer to write comments
in a manner that allows the generation web-based documentation. Program-
mers generating classes to be used by others are particularly encouraged to
consider using Javadoc-based documentation. Such comments are portable,
web-accessible, and they are directly extracted from the code.
In this lab, we will write documentation for an extended version of the Ratio
class we first met in Chapter 1.
Comments used by Javadoc are delimited by a /** */ pair. Note that there
are two asterisks in the start of the comment. Within the comment are a number
of keywords, identified by a leading “at-sign” (@). These keywords identify
the purpose of different comments you right. For example, the text following
an @author comment identifies the programmer who originally authored the
code. These comments, called Javadoc comments, appear before the objects
they document. For example, the first few lines of the Assert class are:
package structure;
/**
Each of the
An assertion is a condition that is expected to
* A library of assertion testing and debugging procedures.
*
* This class of static methods provides basic assertion testing
* facilities.
* be true at a certain point in the code.
* assertion-based routines in this class perform a verification
* of the condition and do nothing (aside from testing side-effects)
* if the condition holds. If the condition fails, however, the
* assertion throws an exception and prints the associated message,
* that describes the condition that failed.
* provided for testing general conditions, and pre- and
* postconditions. There is also a facility for throwing a
* failed condition for code that should not be executed.
*
* Features similar to assertion testing are incorporated
* in the Java 2 language beginning in SDK 1.4.
* @author duane a. bailey
*/
Basic support is
public class Assert
{
. . .
}
For each class you should provide any class-wide documentation, including
@author and @version-tagged comments.
1 Javadoc is a feature of command-line driven Java environments. Graphical environments likely
provide Javadoc-like functionality, but pre- and postcondition support may not be available.
40
Comments, Conditions, and Assertions
Within the class definition, there should be a Javadoc comment for each in-
stance variable and method. Typically, Javadoc comments for instance variables
are short comments that describe the role of the variable in supporting the class
state:
/**
* Size of the structure.
*/
int size;
Comments for methods should include a description of the method’s purpose.
A comment should describe the purpose of each parameter (@param), as well as
the form of the value returned (@return) for function-like methods. Program-
mers should also provide pre- and postconditions using the @pre and @post
keywords.2 Here is the documentation for a square root method.
/**
*
* This method computes the square root of a double value.
* @param x The value whose root is to be computed.
* @return The square root of x.
* @pre x >= 0
* @post computes the square root of x
*/
To complete this lab, you are to
1. Download a copy of the Ratio.java source from the Java Structures web-
site. This version of the Ratio class does not include full comments.
2. Review the code and make sure that you understand the purpose of each
of the methods.
3. At the top of the Ratio.java file, place a Javadoc comment that describes
the class. The comment should describe the features of the class and an
example of how the class might be used. Make sure that you include an
@author comment (use your name).
4. Run the documentation generation facility for your particular environ-
ment. For Sun’s Java environment, the Javadoc command takes a parame-
ter that describes the location of the source code that is to be documented:
javadoc prog.java
2 In this book, where there are constraints on space, the pre- and postconditions are provided in
non-Javadoc comments. Code available on the web, however, is uniformly commented using the
Javadoc comments. Javadoc can be upgraded to recognize pre- and postconditions; details are
available from the Java Structures website.
2.5 Laboratory: Using Javadoc Commenting
41
The result is an index.html file in the current directory that contains links
to all of the necessary documentation. View the documentation to make
sure your description is formatted correctly.
5. Before each instance variable write a short Javadoc comment. The com-
ment should begin with /** and end with */. Generate and view the
documentation and note the change in the documentation.
6. Directly before each method write a Javadoc comment that includes, at
a minimum, one or two sentences that describe the method, a @param
comment for each parameter in the method, a @return comment describ-
ing the value returned, and a @pre and @post comment describing the
conditions.
Generate and view the documentation and note the change in the doc-
umentation. If the documentation facility does not appear to recognize
the @pre and @post keywords, the appropriate Javadoc doclet software
has not been installed correctly. More information on installation of the
Javadoc software can be found at the Java Structures website.
Notes:
Chapter 3
Vectors
Concepts:
. Vectors
.
. Matrices
Extending arrays
Climb high, climb far,
your goal the sky, your aim the star.
—Inscription on a college staircase
THE BEHAVIOR OF A PROGRAM usually depends on its input. Suppose, for ex-
ample, that we wish to write a program that reads in n String values. One
approach would keep track of the n values with n String variables:
public static void main(String args[])
{
// read in n = 4 strings
Scanner s = new Scanner(System.in);
String v1, v2, v3, v4;
v1 = s.next();
v2 = s.next();
v3 = s.next();
v4 = s.next();
// read a space-delimited word
}
This approach is problematic for the programmer of a scalable application—an
application that works with large sets of data as well as small. As soon as n
changes from its current value of 4, it has to be rewritten. Scalable applications
are not uncommon, and so we contemplate how they might be supported.
One approach is to use arrays. An array of n values acts, essentially, as a
collection of similarly typed variables whose names can be computed at run
time. A program reading n values is shown here:
StringReader
public static void main(String args[])
{
// read in n = 4 strings
Scanner s = new Scanner(System.in);
String data[];
int n = 4;
// allocate array of n String references:
data = new String[n];
for (int i = 0; i < n; i++)
44
Vectors
{
}
}
data[i] = s.next();
Here, n is a constant whose value is determined at compile time. As the program
starts up, a new array of n integers is constructed and referenced through the
variable named data.
All is fine, unless you want to read a different number of values. Then n
has to be changed, and the program must be recompiled and rerun. Another
solution is to pick an upper bound on the length of the array and only use the
portion of the array that is necessary. Here’s a modified procedure that uses up
to one million array elements:
public static void main(String args[])
{
// read in up to 1 million Strings
Scanner s = new Scanner(System.in);
String data[];
int n = 0;
data = new String[1000000];
// read in strings until we hit end of file
while (s.hasNext())
{
data[n] = s.next();
n++;
}
}
Unfortunately, if you are running your program on a small machine and have
small amounts of data, you are in trouble (see Problem 3.9). Because the array
is so large, it will not fit on your machine—even if you want to read small
amounts of data. You have to recompile the program with a smaller upper
bound and try again. All this seems rather silly, considering how simple the
problem appears to be.
We might, of course, require the user to specify the maximum size of the
array before the data are read, at run time. Once the size is specified, an appro-
priately sized array can be allocated. While this may appear easier to program,
the burden has shifted to the user of the program: the user has to commit to a
specific upper bound—beforehand:
public static void main(String args[])
{
// read in as many Strings as demanded by input
Scanner s = new Scanner(System.in);
String data[];
int n;
// read in the number of strings to be read
n = s.nextInt();
3.1 The Interface
45
// allocate references for n strings
data = new String[n];
// read in the n strings
for (int i = 0; i < n; i++)
{
data[i] = s.next();
}
}
A nice solution is to build a vector—an array whose size may easily be
changed. Here is our String reading program retooled one last time, using
Vectors:
public static void main(String args[])
{
// read in an arbitrary number of strings
Scanner s = new Scanner(System.in);
Vector data;
// allocate vector for storage
data = new Vector();
// read strings, adding them to end of vector, until eof
while (s.hasNext())
{
String st = s.next();
data.add(st);
}
}
The Vector starts empty and expands (using the add method) with every String
read from the input. Notice that the program doesn’t explicitly keep track of the
number of values stored in data, but that the number may be determined by a
call to the size method.
3.1 The Interface
The semantics of a Vector are similar to the semantics of an array. Both can
store multiple values that may be accessed in any order. We call this property
random access. Unlike the array, however, the Vector starts empty and is ex-
tended to hold object references. In addition, values may be removed from the
Vector causing it to shrink. To accomplish these same size-changing operations
in an array, the array would have to be reallocated.
With these characteristics in mind, let us consider a portion of the “inter-
face”1 for this structure:
1 Stricktly speaking, constructors cannot be specified in formal Javainterfaces. Nonetheless,
adopt a convention of identifying constructors as part of the public view of structures (often called
the Application Program Interface or API).
Vector
46
Vectors
public class Vector extends AbstractList implements Cloneable
{
public Vector()
// post: constructs a vector with capacity for 10 elements
public Vector(int initialCapacity)
// pre: initialCapacity >= 0
// post: constructs an empty vector with initialCapacity capacity
public void add(Object obj)
// post: adds new element to end of possibly extended vector
public Object remove(Object element)
// post: element equal to parameter is removed and returned
public Object get(int index)
// pre: 0 <= index && index < size()
// post: returns the element stored in location index
public void add(int index, Object obj)
// pre: 0 <= index <= size()
// post: inserts new value in vector with desired index,
moving elements from index to size()-1 to right
//
public boolean isEmpty()
// post: returns true iff there are no elements in the vector
public Object remove(int where)
// pre: 0 <= where && where < size()
// post: indicated element is removed, size decreases by 1
public Object set(int index, Object obj)
// pre: 0 <= index && index < size()
// post: element value is changed to obj; old value is returned
public int size()
// post: returns the size of the vector
}
First, the constructors allow construction of a Vector with an optional initial
capacity. The capacity is the initial number of Vector locations that are reserved
for expansion. The Vector starts empty and may be freely expanded to its
capacity. At that point the Vector’s memory is reallocated to handle further
expansion. While the particulars of memory allocation and reallocation are
hidden from the user, there is obvious benefit to specifying an appropriate initial
capacity.
The one-parameter add method adds a value to the end of the Vector, ex-
panding it. To insert a new value in the middle of the Vector, we use the
two-parameter add method, which includes a location for insertion. To access
3.2 Example: The Word List Revisited
47
an existing element, one calls get. If remove is called with an Object, it re-
moves at most one element, selected by value. Another remove method shrinks
the logical size of the Vector by removing an element from an indicated loca-
tion. The set method is used to change a value in the Vector. Finally, two
methods provide feedback about the current logical size of the Vector: size
and isEmpty. The size method returns the number of values stored within
the Vector. As elements are added to the Vector, the size increases from zero
up to the capacity of the Vector. When the size is zero, then isEmpty returns
true. The result is a data structure that provides constant-time access to data
within the structure, without concern for determining explicit bounds on the
structure’s size.
There are several ways that a Vector is different than its array counterpart.
First, while both the array and Vector maintain a number of references to ob-
jects, the Vector typically grows with use and stores a non-null reference in
each entry. An array is a static structure whose entries may be initialized and
used in any order and are often null. Second, the Vector has an end where ele-
ments can be appended, while the array does not directly support the concept of
appending values. There are times, of course, when the append operation might
not be a feature desired in the structure; either the Vector or array would be a
suitable choice.
The interface for Vectors in the structure package was driven, almost ex-
clusively, by the interface for Java’s proprietary java.util.Vector class. Thus,
while we do not have access to the code for that class, any program written to
use Java’s Vector class can be made to use the Vector class described here;
their interfaces are consistent.
3.2 Example: The Word List Revisited
We now reconsider an implementation of the word list part of our Hangman
program of Section 1.6 implemented directly using Vectors:
Vector list;
String targetWord;
java.util.Random generator = new java.util.Random();
list = new Vector(10);
list.add("clarify");
list.add("entered");
list.add("clerk");
while (list.size() != 0)
{
{
// select a word from the list
int index = Math.abs(generator.nextInt())%list.size();
targetWord = (String)list.get(index);
}
// ... play the game using targetWord ...
list.remove(targetWord);
}
WordList
48
Vectors
Here, the operations of the Vector are seen to be very similar to the opera-
tions of the WordList program fragment shown on page 19. The Vector class,
however, does not have a selectAny method. Instead, the bracketed code ac-
complishes that task. Since only Strings are placed within the Vector, the
assignment of targetWord involves a cast from Object (the type of value re-
turned from the get method of Vector) to String. This cast is necessary for
Java to be reassured that you’re expecting an element of type String to be
returned.
If the cast were not provided, Java would complain that the types
involved in the assignment were incompatible.
Now that we have an implementation of the Hangman code in terms of both
the WordList and Vector structures, we can deduce an implementation of the
WordList structure in terms of the Vector class. In this implementation, the
WordList contains a Vector that is used to hold the various words, as well
as the random number generator (the variable generator in the code shown
above). To demonstrate the implementation, we look at the implementation of
the WordList’s constructor and selectAny method:
protected Vector theList;
protected java.util.Random generator;
public WordList(int n)
{
theList = new Vector(n);
generator = new java.util.Random();
}
public String selectAny()
{
int i = Math.abs(generator.nextInt())%theList.size();
return (String)theList.get(i);
}
Clearly, the use of a Vector within the WordList is an improvement over the
direct use of an array, just as the use of WordList is an improvement over the
complications of directly using a Vector in the Hangman program.
3.3 Example: Word Frequency
Suppose one day you read a book, and within the first few pages you read
“behemoth” twice. A mighty unusual writing style! Word frequencies within
documents can yield interesting information.2 Here is a little application for
computing the frequency of words appearing on the input:
public static void main(String args[])
WordFreq
2 Recently, using informal “literary forensics,” Don Foster has identified the author of the anony-
mously penned book Primary Colors and is responsible for new attributions of poetry to Shake-
speare. Foster also identified Major Henry Livingston Jr. as the true author of “The Night Before
Christmas.”
3.3 Example: Word Frequency
49
{
Vector vocab = new Vector(1000);
Scanner s = new Scanner(System.in);
int i;
// for each word on input
while (s.hasNext())
{
Association wordInfo; // word-frequency association
String vocabWord;
// word in the list
// read in and tally instance of a word
String word = s.next();
for (i = 0; i < vocab.size(); i++)
{
// get the association
wordInfo = (Association)vocab.get(i);
// get the word from the association
vocabWord = (String)wordInfo.getKey();
if (vocabWord.equals(word))
{
// match: increment integer in association
Integer f = (Integer)wordInfo.getValue();
wordInfo.setValue(new Integer(f.intValue() + 1));
break;
}
}
// mismatch: add new word, frequency 1.
if (i == vocab.size())
{
vocab.add(new Association(word,new Integer(1)));
}
}
// print out the accumulated word frequencies
for (i = 0; i < vocab.size(); i++)
{
Association wordInfo = (Association)vocab.get(i);
System.out.println(
wordInfo.getKey()+" occurs "+
wordInfo.getValue()+" times.");
}
}
First, for each word found on the input, we maintain an Association between
the word (a String) and its frequency (an Integer). Each element of the
Vector is such an Association. Now, the outer loop at the top reads in each
word. The inner loop scans through the Vector searching for matching words
that might have been read in. Matching words have their values updated. New
words cause the construction of a new Association. The second loop scans
through the Vector, printing out each of the Associations.
50
Vectors
Each of these applications demonstrates the most common use of Vectors—
keeping track of data when the number of entries is not known far in advance.
When considering the List data structure we will consider the efficiency of
these algorithms and, if necessary, seek improvements.
3.4 The Implementation
Clearly, the Vector must be able to store a large number of similar items. We
choose, then, to have the implementation of the Vector maintain an array of
Objects, along with an integer that describes its current size or extent. When
the size is about to exceed the capacity (the length of the underlying array), the
Vector’s capacity is increased to hold the growing number of elements.
The constructor is responsible for allocation of the space and initializing the
local variables. The number of elements initially allocated for expansion can be
specified by the user:
protected Object elementData[];
protected int elementCount;
// the data
// number of elements in vector
Vector
public Vector()
// post: constructs a vector with capacity for 10 elements
{
this(10); // call one-parameter constructor
}
public Vector(int initialCapacity)
// pre: initialCapacity >= 0
// post: constructs an empty vector with initialCapacity capacity
{
Assert.pre(initialCapacity >= 0, "Initial capacity should not be negative.");
elementData = new Object[initialCapacity];
elementCount = 0;
}
Unlike other languages, all arrays within Java must be explicitly allocated. At
the time the array is allocated, the number of elements is specified. Thus, in the
constructor, the new operator allocates the number of elements desired by the
user. Since the size of an array can be gleaned from the array itself (by asking
for elementData.length), the value does not need to be explicitly stored within
the Vector object.3
To access and modify elements within a Vector, we use the following oper-
ations:
public Object get(int index)
3 It could, of course, but explicitly storing it within the structure would mean that the implementor
would have to ensure that the stored value was always consistent with the value accessible through
the array’s length variable.
3.4 The Implementation
51
// pre: 0 <= index && index < size()
// post: returns the element stored in location index
{
return elementData[index];
}
public Object set(int index, Object obj)
// pre: 0 <= index && index < size()
// post: element value is changed to obj; old value is returned
{
Object previous = elementData[index];
elementData[index] = obj;
return previous;
}
The arguments to both methods identify the location of the desired element. Be-
cause the index should be within the range of available values, the precondition
states this fact.
For the accessor (get), the desired element is returned as the result. The set
method allows the Object reference to be changed to a new value and returns
the old value. These operations, effectively, translate operations on Vectors
into operations on arrays.
Now consider the addition of an element to the Vector. One way this can
be accomplished is through the use of the one-parameter add method. The task
requires extending the size of the Vector and then storing the element at the
location indexed by the current number of elements (this is the first free location
within the Vector). Here is the Java method:
public void add(Object obj)
// post: adds new element to end of possibly extended vector
{
ensureCapacity(elementCount+1);
elementData[elementCount] = obj;
elementCount++;
}
(We will discuss the method ensureCapacity later. Its purpose is simply to en-
sure that the data array actually has enough room to hold the indicated number
of values.) Notice that, as with many modern languages, arrays are indexed
starting at zero. There are many good reasons for doing this. There are prob-
ably just as many good reasons for not doing this, but the best defense is that
this is what programmers are currently used to.
Principle 6 Maintaining a consistent interface makes a structure useful.
If one is interested in inserting an element in the middle of the Vector, it is
necessary to use the two-parameter add method. The operation first creates an
unused location at the desired point by shifting elements out of the way. Once
the opening is created, the new element is inserted.
NNWSWSENEWSE52
Vectors
Figure 3.1
room for an inserted value.
The incorrect (a) and correct (b) way of moving values in an array to make
public void add(int index, Object obj)
// pre: 0 <= index <= size()
// post: inserts new value in vector with desired index,
//
moving elements from index to size()-1 to right
{
int i;
ensureCapacity(elementCount+1);
// must copy from right to left to avoid destroying data
for (i = elementCount; i > index; i--) {
elementData[i] = elementData[i-1];
}
// assertion: i == index and element[index] is available
elementData[index] = obj;
elementCount++;
}
Note that the loop that moves the elements higher in the array runs backward.
To see why, it is only necessary to see what happens if the loop runs forward (see
Figure 3.1a): the lowest element gets copied into higher and higher elements,
ultimately copying over the entire Vector to the right of the insertion point.
Figure 3.1b demonstrates the correct technique.
Removing an element from a specific location in the Vector is very similar,
reversing the effect of add. Here, using an argument similar to the previous one,
the loop moves in the forward direction:
public Object remove(int where)
// pre: 0 <= where && where < size()
// post: indicated element is removed, size decreases by 1
{
2345678900123456789090000000003456789000(a)1234567890123456780123456701088876543217(b)3.5 Extensibility: A Feature
53
Object result = get(where);
elementCount--;
while (where < elementCount) {
elementData[where] = elementData[where+1];
where++;
}
elementData[elementCount] = null; // free reference
return result;
}
We also allow the removal of a specific value from the Vector, by passing an
example Object to remove (not shown). Within this code, the equals method
of the value passed to the routine is used to compare it to values within the
Vector. When (and if) a match is found, it is removed using the technique just
described.
The methods having to do with size are relatively straightforward:
public boolean isEmpty()
// post: returns true iff there are no elements in the vector
{
return size() == 0;
}
public int size()
// post: returns the size of the vector
{
return elementCount;
}
The logical size of the Vector is the number of elements stored within the
Vector, and it is empty when this size is zero.
3.5 Extensibility: A Feature
Sometimes, our initial estimate of the maximum number of values is too small.
In this case, it is necessary to extend the capacity of the Vector, carefully main-
taining the values already stored within the Vector. Fortunately, because we
have packaged the implementation within an interface, it is only necessary to
extend the functionality of the existing operations and provide some additional
methods to describe the features.
A first approach might be to extend the Vector to include just as many
elements as needed. Every time an element is added to the Vector, the number
of elements is compared to the capacity of the array. If the capacity is used up,
an array that is one element longer is allocated. This reallocation also requires
copying of the existing data from one array to the other. Of course, for really
long arrays, these copying operations would take a proportionally long time.
Over time, as the array grows to n elements, the array data get copied many
times. At the beginning, the array holds a single element, but it is expanded to
54
Vectors
hold two. The original element must be copied to the new space to complete
the operation. When a third is added, the first two must be copied. The result
is that
1 + 2 + 3 + · · · + (n − 1) =
n(n − 1)
2
elements are copied as the array grows to size n. (Proving this last formula is
the core of Problem 3.8.) This is expensive since, if in the beginning we had
just allocated the Vector with a capacity of n elements, none of the data items
would have to be copied during extension!
It turns out there is a happy medium: every time you extend the array, just
double its capacity. Now, if we reconsider the number of times that an item
gets copied during the extension process, the result is dramatically different.
Suppose, for neatness only, that n is a power of 2, and that the Vector started
with a capacity of 1. What do we know? When the Vector was extended from
capacity 1 to capacity 2, one element was copied. When the array was extended
from capacity 2 to capacity 4, two elements were copied. When the array was
extended from capacity 4 to capacity 8, four elements were copied. This contin-
ues until the last extension, when the Vector had its capacity extended from n
2
to n: then n
2 elements had to be preserved. The total number of times elements
were copied is
1 + 2 + 4 + · · · +
= n − 1
n
2
Thus, extension by doubling allows unlimited growth of the Vector with an
overhead that is proportional to the ultimate length of the array. Another way
to think about it is that there is a constant overhead in supporting each element
of a Vector extended in this way.
The Java language specifies a Vector interface that allows the user to specify
how the Vector is to be extended if its capacity is not sufficient for the current
operation. When the Vector is constructed, a capacityIncrement is specified.
This is simply the number of elements to be added to the underlying array
when extension is required. A nonzero value for this increment leads to the n2
behavior we saw before, but it may be useful if, for example, one does not have
the luxury of being able to double the size of a large array. If the increment is
zero, the doubling strategy is used.
Our design, then, demands another protected value to hold the increment;
we call this capacityIncrement. This value is specified in a special constructor
and is not changed during the life of the Vector:
protected int capacityIncrement;
// the rate of growth for vector
public Vector(int initialCapacity, int capacityIncr)
// pre: initialCapacity >= 0, capacityIncr >= 0
// post: constructs an empty vector with initialCapacity capacity
that extends capacity by capacityIncr, or doubles if 0
//
{
Assert.pre(initialCapacity >= 0 && capacityIncr >= 0,
"Neither capacity nor increment should be negative.");
3.5 Extensibility: A Feature
55
elementData = new Object[initialCapacity];
elementCount = 0;
capacityIncrement = capacityIncr;
}
We are now prepared to investigate ensureCapacity, a method that, if nec-
essary, resizes Vector to have a capacity of at least minCapacity:
public void ensureCapacity(int minCapacity)
// post: the capacity of this vector is at least minCapacity
{
if (elementData.length < minCapacity) {
int newLength = elementData.length; // initial guess
if (capacityIncrement == 0) {
// increment of 0 suggests doubling (default)
if (newLength == 0) newLength = 1;
while (newLength < minCapacity) {
newLength *= 2;
}
} else {
// increment != 0 suggests incremental increase
while (newLength < minCapacity)
{
newLength += capacityIncrement;
}
}
// assertion: newLength > elementData.length.
Object newElementData[] = new Object[newLength];
int i;
// copy old data to array
for (i = 0; i < elementCount; i++) {
newElementData[i] = elementData[i];
}
elementData = newElementData;
// garbage collector will (eventually) pick up old elementData
}
// assertion: capacity is at least minCapacity
}
This code deserves careful investigation. If the current length of the underlying
array is already sufficient to provide minCapacity elements, then the method
does nothing. On the other hand, if the Vector is too short, it must be ex-
tended. We use a loop here that determines the new capacity by doubling (if
capacityIncrement is zero) or by directly incrementing if capacityIncrement
is nonzero. In either case, by the time the loop is finished, the desired capacity
is determined. At that point, an array of the appropriate size is allocated, the
old values are copied over, and the old array is dereferenced in favor of the new.
56
Vectors
3.6 Example: L-Systems
In the late 1960s biologists began to develop computational models for growth.
One of the most successful models, L-systems, was developed by Aristid Lin-
denmayer. An L-system consists of a seed or start string of symbols derived
from an alphabet, along with a number of rules for changing or rewriting the
symbols, called productions. To simulate an interval of growth, strings are com-
pletely rewritten using the productions. When the rewriting begins with the
start string, it is possible to iteratively simulate the growth of a simple organ-
ism. To demonstrate the complexity of this approach, we can use an alphabet
of two characters—S (for stem) and L (for leaf). If the two productions
Before After
S
L
L
SL
are used, we can generate the following strings over 6 time steps:
Time
0
1
2
3
4
5
6
String
S
L
SL
LSL
SLLSL
LSLSLLSL
SLLSLLSLSLLSL
Although there are some observations that might be made (there are never two
consecutive Ss), any notion of a pattern in this string quickly breaks down. Still,
many organisms display patterns that are motivated by the seemingly simple
production system.
We can use Vectors to help us perform this rewriting process. By construct-
ing two Character objects, L and S, we can store patterns in a Vector of refer-
ences. The rewriting process involves constructing a new result Vector. Here is
a program that would verify the growth pattern suggested in the table:
public class LSystem
{
// constants that define the alphabet
final static Character L = new Character(’L’);
final static Character S = new Character(’S’);
LSystem
public static Vector rewrite(Vector s)
// pre: s is a string of L and S values
// post: returns a string rewritten by productions
{
Vector result = new Vector();
for (int pos = 0; pos < s.size(); pos++)
{
3.7 Example: Vector-Based Sets
57
// rewrite according to two different rules
if (S == s.get(pos)) {
result.add(L);
} else if (L == s.get(pos)) {
result.add(S); result.add(L);
}
}
return result;
}
public static void main(String[] args)
{
Vector string = new Vector();
string.add(S);
// determine the number of strings
Scanner s = new Scanner(System.in);
int count = s.nextInt();
// write out the start string
System.out.println(string);
for (int i = 1; i <= count; i++)
{
string = rewrite(string);
System.out.println(string); // print it out
// rewrite the string
}
}
}
L-systems are an interesting example of a grammar system. The power of a
grammar to generate complex structures—including languages and, biologi-
cally, plants—is of great interest to theoretical computer scientists.
3.7 Example: Vector-Based Sets
In Section 1.8 we discussed Java’s interface for a Set. Mathematically, it is an
unordered collection of unique values. The set abstraction is an important fea-
ture of many algorithms that appear in computer science, and so it is important
that we actually consider a simple implementation before we go much further.
As we recall, the Set is an extension of the Structure interface. It demands
that the programmer implement not only the basic Structure methods (add,
contains, remove, etc.), but also the following methods of a Set. Here is the
interface associated with a Vector-based implementation of a Set:
public class SetVector extends AbstractSet
{
public SetVector()
// post: constructs a new, empty set
SetVector
58
Vectors
public SetVector(Structure other)
// post: constructs a new set with elements from other
public void clear()
// post: elements of set are removed
public boolean isEmpty()
// post: returns true iff set is empty
public void add(Object e)
// pre: e is non-null object
// post: adds element e to set
public Object remove(Object e)
// pre: e is non-null object
// post: e is removed from set, value returned
public boolean contains(Object e)
// pre: e is non-null
// post: returns true iff e is in set
public boolean containsAll(Structure other)
// pre: other is non-null reference to set
// post: returns true iff this set is subset of other
public Object clone()
// post: returns a copy of set
public void addAll(Structure other)
// pre: other is a non-null structure
// post: add all elements of other to set, if needed
public void retainAll(Structure other)
// pre: other is non-null reference to set
// post: returns set containing intersection of this and other
public void removeAll(Structure other)
// pre: other is non-null reference to set
// post: returns set containing difference of this and other
public Iterator iterator()
// post: returns traversal to traverse the elements of set
public int size()
// post: returns number of elements in set
}
A SetVector might take the approach begun by the WordList implementation
3.7 Example: Vector-Based Sets
59
we have seen in Section 3.2: each element of the Set would be stored in a
location in the Vector. Whenever a new value is to be added to the Set, it
is only added if the Set does not already contain the value. When values are
removed from the Set, the structure contracts. At all times, we are free to keep
the order of the data in the Vector hidden from the user since the ordering of
the values is not part of the abstraction.
We construct a SetVector using the following constructors, which initialize
a protected Vector:
protected Vector data; // the underlying vector
public SetVector()
// post: constructs a new, empty set
{
data = new Vector();
}
public SetVector(Structure other)
// post: constructs a new set with elements from other
{
this();
addAll(other);
}
The second constructor is a copy constructor that makes use of the union op-
erator, addAll. Since the initial set is empty (the call to this() calls the first
constructor), the SetVector essentially picks up all the values found in the other
structure.
Most methods of the Set are adopted from the underlying Vector class. For
example, the remove method simply calls the remove method of the Vector:
public Object remove(Object e)
// pre: e is non-null object
// post: e is removed from set, value returned
{
return data.remove(e);
}
The add method, though, is responsible for ensuring that duplicate values are
not added to the Set. It must first check to see if the value is already a member:
public void add(Object e)
// pre: e is non-null object
// post: adds element e to set
{
if (!data.contains(e)) data.add(e);
}
60
Vectors
To perform the more complex Set-specific operations (addAll and others), we
must perform the specified operation for all the values of the other set. To ac-
complish this, we make use of an Iterator, a mechanism we will not study
until Chapter 8, but which is nonetheless simple to understand. Here, for ex-
ample, is the implementation of addAll, which attempts to add all the values
found in the other structure:
public void addAll(Structure other)
// pre: other is a non-null structure
// post: add all elements of other to set, if needed
{
Iterator yourElements = other.iterator();
while (yourElements.hasNext())
{
add(yourElements.next());
}
}
Other methods are defined in a straightforward manner.
3.8 Example: The Matrix Class
One application of the Vector class is to support a two-dimensional Vector-like
object: the matrix. Matrices are used in applications where two dimensions of
data are needed. Our Matrix class has the following methods:
public class Matrix
{
Matrix
public Matrix(int h, int w)
// pre: h >= 0, w >= 0;
// post: constructs an h row by w column matrix
public Object get(int row, int col)
// pre: 0 <= row < height(), 0 <= col < width()
// post: returns object at (row, col)
public void set(int row, int col, Object value)
// pre: 0 <= row < height(), 0 <= col < width()
// post: changes location (row, col) to value
public void addRow(int r)
// pre: 0 <= r < height()
// post: inserts row of null values to be row r
public void addCol(int c)
// pre: 0 <= c < width()
// post: inserts column of null values to be column c
3.8 Example: The Matrix Class
61
Figure 3.2
Vector of references to Objects. Elements are labeled with their indices.
The Matrix class is represented as a Vector of rows, each of which is a
public Vector removeRow(int r)
// pre: 0 <= r < height()
// post: removes row r and returns it as a Vector
public Vector removeCol(int c)
// pre: 0 <= c < width()
// post: removes column c and returns it as a vector
public int width()
// post: returns number of columns in matrix
public int height()
// post: returns number of rows in matrix
}
The two-parameter constructor specifies the width and height of the Matrix. El-
ements of the Matrix are initially null, but may be reset with the set method.
This method, along with the get method, accepts two parameters that identify
the row and the column of the value. To expand and shrink the Matrix, it is
possible to insert and remove both rows and columns at any location. When a
row or column is removed, a Vector of removed values is returned. The meth-
ods height and width return the number of rows and columns found within
the Matrix, respectively.
To support this interface, we imagine that a Matrix is a Vector of rows,
which are, themselves, Vectors of values (see Figure 3.2). While it is not strictly
necessary, we explicitly keep track of the height and width of the Matrix (if we
determine at some later date that keeping this information is unnecessary, the
interface would hide the removal of these fields). Here, then, is the constructor
for the Matrix class:
protected int height, width; // size of matrix
Rows(0,0)(0,1)(0,2)(0,3)(1,0)(1,1)(1,2)(1,3)(2,0)(2,1)(2,2)(2,3)(3,0)(3,1)(3,2)(3,3)(4,0)(4,1)(4,2)(4,3)01230123462
Vectors
protected Vector rows;
// vector of row vectors
public Matrix(int h, int w)
// pre: h >= 0, w >= 0;
// post: constructs an h row by w column matrix
{
// initialize height and width
height = h;
width = w;
// allocate a vector of rows
rows = new Vector(height);
for (int r = 0; r < height; r++)
{
// each row is allocated and filled with nulls
Vector theRow = new Vector(width);
rows.add(theRow);
for (int c = 0; c < width; c++)
{
theRow.add(null);
}
}
}
We allocate a Vector for holding the desired number of rows, and then, for each
row, we construct a new Vector of the appropriate width. All the elements are
initialized to null. It’s not strictly necessary to do this initialization, but it’s a
good habit to get into.
The process of manipulating individual elements of the matrix is demon-
strated by the get and set methods:
public Object get(int row, int col)
// pre: 0 <= row < height(), 0 <= col < width()
// post: returns object at (row, col)
{
Assert.pre(0 <= row && row < height, "Row in bounds.");
Assert.pre(0 <= col && col < width, "Col in bounds.");
Vector theRow = (Vector)rows.get(row);
return theRow.get(col);
}
public void set(int row, int col, Object value)
// pre: 0 <= row < height(), 0 <= col < width()
// post: changes location (row, col) to value
{
Assert.pre(0 <= row && row < height, "Row in bounds.");
Assert.pre(0 <= col && col < width, "Col in bounds.");
Vector theRow = (Vector)rows.get(row);
theRow.set(col,value);
}
The process of manipulating an element requires looking up a row within the
rows table and finding the element within the row. It is also important to notice
3.8 Example: The Matrix Class
63
Figure 3.3
associated with matrix before addRow. Compare with Figure 3.2.
The insertion of a new row (gray) into an existing matrix. Indices are those
that in the set method, the row is found using the get method, while the
element within the row is changed using the set method. Although the element
within the row changes, the row itself is represented by the same vector.
Many of the same memory management issues discussed in reference to
Vectors hold as well for the Matrix class. When a row or column needs to be
expanded to make room for new elements (see Figure 3.3), it is vital that the
management of the arrays within the Vector class be hidden. Still, with the
addition of a row into the Matrix, it is necessary to allocate the new row object
and to initialize each of the elements of the row to null:
public void addRow(int r)
// pre: 0 <= r < height()
// post: inserts row of null values to be row r
{
Assert.pre(0 <= r && r < width, "Row in bounds.");
height++;
Vector theRow = new Vector(width);
for (int c = 0; c < width; c++)
{
theRow.add(null);
}
rows.add(r,theRow);
}
We leave it to the reader to investigate the implementation of other Matrix
methods. In addition, a number of problems consider common extensions to
the Matrix class.
(3,1)(3,2)(3,3)(3,0)(0,0)(0,1)(0,2)(0,3)(1,0)(1,1)(1,2)(1,3)(2,0)(2,1)(2,2)(2,3)0123012345(4,0)(4,1)(4,2)(4,3)Rows64
Vectors
3.9 Conclusions
Most applications that accept data are made more versatile by not imposing
constraints on the number of values processed by the application. Because the
size of an array is fixed at the time it is allocated, programmers find it difficult
to create size-independent code without the use of extensible data structures.
The Vector and Matrix classes are examples of extensible structures.
Initially Vectors are empty, but can be easily expanded when necessary.
When a programmer knows the upper bound on the Vector size, this infor-
mation can be used to minimize the amount of copying necessary during the
entire expansion process. When a bound is not known, we saw that doubling
the allocated storage at expansion time can reduce the overall copying cost.
The implementation of Vector and Matrix classes is not trivial. Data ab-
straction hides many important housekeeping details. Fortunately, while these
details are complex for the implementor, they can considerably reduce the com-
plexity of applications that make use of the Vector and Matrix structures.
Self Check Problems
Solutions to these problems begin on page 442.
3.1
How are arrays and Vectors the same? How do they differ?
What is the difference between the add(v) and add(i,v) methods of
3.2
Vector?
What is the difference between the add(i,v) method and the set(i,v)
3.3
method?
What is the difference between the remove(v) method (v is an Object
3.4
value), and the remove(i) (i is an int)?
3.5
What is the distinction between the capacity and size of a Vector?
Why is the use of a Vector an improvement over the use of an array in
3.6
the implementation of Hangman in Section 3.2?
3.7
ments to the right starting at the high end of the Vector? (See Figure 3.1.)
When inserting a value into a Vector why is it necessary to shift ele-
By default, when the size first exceeds the capacity, the capacity of the
3.8
Vector is doubled. Why?
3.9
What is the purpose of the following code?
elementData = new Object[initialCapacity];
What can be said about the values found in elementData after this code is
executed?
3.10 When there is more than one constructor for a class, when and how do
we indicate the appropriate method to use? Compare, for example,
3.9 Conclusions
65
Vector v = new Vector();
Vector w = new Vector(1000);
Is the row index of the Matrix bounded by the matrix height or width?
3.11
When indexing a Matrix which is provided first, the row or the column?
Problems
Solutions to the odd-numbered problems begin on page 457.
Explain the difference between the size and capacity of a vector. Which
3.1
is more important to the user?
The default capacity of a Vector in a structure package implementa-
3.2
tion is 10. It could have been one million. How would you determine a suitable
value?
The implementation of java.util.Vector provides a method trimTo-
3.3
Size. This method ensures that the capacity of the Vector is the same as its
size. Why is this useful? Is it possible to trim the capacity of a Vector without
using this method?
The implementation of java.util.Vector provides a method setSize.
3.4
This method explicitly sets the size of the Vector. Why is this useful? Is it
possible to set the size of the Vector without using this method?
Write a Vector method, indexOf, that returns the index of an object in
3.5
the Vector. What should the method return if no object that is equals to this
object can be found? What does java.lang.Vector do in this case? How long
does this operation take to perform, on average?
Write a class called BitVector that has an interface similar to Vector,
3.6
but the values stored within the BitVector are all known to be boolean (the
primitive type). What is the primary advantage of having a special-purpose
vector, like BitVector?
Suppose we desire to implement a method reverse for the Vector class.
3.7
One approach would be to remove location 0 and to use add near the end or tail
of the Vector. Defend or reject this suggested implementation. In either case,
write the best method you can.
Suppose that a precisely sized array is used to hold data, and that each
3.8
time the array size is to be increased, it is increased by exactly one and the data
are copied over. Prove that, in the process of growing an array incrementally
from size 0 to size n, approximately n2 values must be copied.
What is the maximum length array of Strings you can allocate on your
3.9
machine? (You needn’t initialize the array.) What is the maximum length array
of boolean you can allocate on your machine? What is to be learned from the
ratio of these two values?
3.10
Implement the Object-based remove method for the Vector class.
66
Vectors
In our discussion of L-systems, the resulting strings are always linear.
3.11
Plants, however, often branch. Modify the LSystem program so that it includes
the following five productions:
Before After
Before After
S
T
T
U
U
V
V
W
Before After
[S]U
W
where [S] is represented by a new Vector that contains a single S. (To test to
see if an Object, x, is a Vector, use the test x instanceof Vector.)
Finish the two-dimensional Vector-like structure Matrix. Each element
3.12
of the Matrix is indexed by two integers that identify the row and column
containing the value. Your class should support a constructor, methods addRow
and addCol that append a row or column, the get and set methods, and width
and height methods. In addition, you should be able to use the removeRow and
removeCol methods.
3.13 Write Matrix methods for add and multiply. These methods should
implement the standard matrix operations from linear algebra. What are the
preconditions that are necessary for these methods?
A Matrix is useful for nonmathematical applications. Suppose, for ex-
3.14
ample, that the owners of cars parked in a rectangular parking lot are stored
in a Matrix. How would you design a new Matrix method to return the lo-
cation of a particular value in the Matrix? (Such an extension implements an
associative memory. We will discuss associative structures when we consider
Dictionarys.)
An m × n Matrix could be implemented using a single Vector with
3.15
mn locations. Assuming that this choice was made, implement the get method.
What are the advantages and disadvantages of this implementation over the
Vector of Vectors approach?
A triangular matrix is a two-dimensional structure with n rows. Row i
3.16
has i + 1 columns (numbered 0 through i) in row i. Design a class that supports
all the Matrix operations, except addRow, removeRow, addCol, and removeCol.
You should also note that when a row and column must be specified, the row
must be greater than or equal to the column.
A symmetric matrix is a two-dimensional Matrix-like structure such that
3.17
the element at [i][j] is the same element found at [j][i]. How would you imple-
ment each of the Matrix operations? The triangular matrix of Problem 3.16
may be useful here. Symmetric matrices are useful in implementing undirected
graph structures.
Sometimes it is useful to keep an unordered list of characters (with
3.18
ASCII codes 0 through 127), with no duplicates. Java, for example, has a
Implement a class, CharSet, using
CharSet class in the java.util package.
a Vector. Your class should support (1) the creation of an empty set, (2) the
addition of a single character to the set, (3) the check for a character in the set,
(4) the union of two sets, and (5) a test for set equality.
3.10 Laboratory: The Silver Dollar Game
Objective. To implement a simple game using Vectors or arrays.
Discussion. The Silver Dollar Game is played between two players. An arbitrar-
ily long strip of paper is marked off into squares:
The game begins by placing silver dollars in a few of the squares. Each square
holds at most one coin. Interesting games begin with some pairs of coins sepa-
rated by one or more empty squares.
The goal is to move all the n coins to the leftmost n squares of the paper.
This is accomplished by players alternately moving a single coin, constrained by
the following rules:
1. Coins move only to the left.
2. No coin may pass another.
3. No square may hold more than one coin.
The last person to move is the winner.
Procedure. Write a program to facilitate playing the Silver Dollar Game. When
the game starts, the computer has set up a random strip with 3 or more coins.
Two players are then alternately presented with the current game state and are
allowed to enter moves. If the coins are labeled 0 through n−1 from left to right,
a move could be specified by a coin number and the number of squares to move
the coin to the left. If the move is illegal, the player is repeatedly prompted to
enter a revised move. Between turns the computer checks the board state to
determine if the game has been won.
Here is one way to approach the problem:
1. Decide on an internal representation of the strip of coins. Does your rep-
resentation store all the information necessary to play the game? Does
your representation store more information than is necessary? Is it easy to
test for a legal move? Is it easy to test for a win?
2. Develop a new class, CoinStrip, that keeps track of the state of the play-
ing strip. There should be a constructor, which generates a random board.
Another method, toString, returns a string representation of the coin
strip. What other operations seem to be necessary? How are moves per-
formed? How are rules enforced? How is a win detected?
68
Vectors
Hint: When
flipped, the
Belgian Euro is
heads
149 times
out of 250.
3. Implement an application whose main method controls the play of a single
game.
Thought Questions. Consider the following questions as you complete the lab:
1. How might one pick game sizes so that, say, one has a 50 percent chance
of a game with three coins, a 25 percent chance of a game with four coins,
a 12 1
2 percent chance of a game with five coins, and so on? Would your
technique bias your choice of underlying data structure?
2. How might one generate games that are not immediate wins? Suppose
you wanted to be guaranteed a game with the possibility of n moves?
3. Suppose the computer could occasionally provide good hints. What op-
portunities appear easy to recognize?
4. How might you write a method, computerPlay, where the computer plays
to win?
5. A similar game, called Welter’s Game (after C. P. Welter, who analyzed the
game), allows the coins to pass each other. Would this modification of the
rules change your implementation significantly?
Notes:
Chapter 4
Generics
Concepts:
. Motivation for Parameterized Types
.
.
Simple Parameterized Types
Parameterization in Vector
Thank God that
the people running this world
are not smart enough
to keep running it forever.
—Arlo Guthrie
THE MAIN PURPOSE OF A LANGUAGE is to convey information from one party
to another. Programming languages are no exception. Ideally, a language like
Java lets the programmer express the logic of an algorithm in a natural way,
and the language would identify errors of grammar (i.e., errors of syntax) and
meaning (semantics). When these errors are flagged as the program is com-
piled, we call them compile-time errors. Certain logical errors, of course, do not
appear until the program is run. When these errors are detected (for example,
a division-by-zero, or an object reference through a null pointer) Java generates
runtime errors.1 It is generally believed that the sooner one can detect an error,
the better. So, for example, it is useful to point out a syntax error (“You’ve for-
gotten to declare the variable, cheatCode, you use, here, on line 22.”) where
it occurs rather than a few lines later (“After compiling Lottery.java, we no-
ticed that you mentioned 4 undeclared variables: random, range, trapdoor, and
winner.”). Because a runtime error may occur far away and long after the pro-
gram was written, compile-time errors (that can be fixed by the programming
staff) are considered to be preferable to runtime errors (that must be fixed by
the public relations staff).
Java 5 provides some important language features that shift the detection of
certain important errors from runtime to compile-time. The notion of a generic
class is one of them. This chapter is about building generic classes.
1 Even after considering compile-time and runtime errors, there are many errors that even a run-
ning system cannot detect. For example, undesirable infinite loops (some are desirable) are often
the result of errors in logic that the computer cannot detect. The word “cannot” is quite strong here:
it is impossible to build an environment that correctly identifies any program that loops infinitely
(or even more broadly–fails to halt). This result is due to Alan Turing.
70
Generics
4.1 Motivation (in case we need some)
In Chapter 3 we constructed the Vector class, a general purpose array. Its pri-
mary advantage over arrays is that it is extensible: the Vector can be efficiently
It suffers one important disadvantage:
lengthened and shortened as needed.
it is impossible to declare a Vector that contains exactly one type of object.
Ponder this: we almost always declare variables and arrays to contain values of
one particular type, but Vectors hold Objects. As we have seen, an Object can
hold any reference type, so it is hardly a restriction. How can we construct a
Vector restricted to holding just String references?
Of course, you could depend on your naturally good behavior. As long as
the programs you write only add and remove Strings from the Vector, life will
be good. As an example, consider the following (silly) program:
LongWords
public static void main(String[] args)
{
Vector longWords = new Vector();
int i;
for (i = 0; i < args.length; i++) {
if (args[i].length() > 4) {
longWords.add(args[i]);
// line 12
}
}
...
for (i = 0; i < longWords.size(); i++) {
String word = (String)longWords.get(i); // line 31
System.out.println(word+", length "+word.length());
}
}
This main method builds a Vector of long arguments—Strings longer than 4
characters—and, sometime later, prints out the list of words along with their
respective lengths. If we type
java LongWords Fuzzy Wozzie was a bear
we expect to get
Fuzzy, length 5
Wozzie, length 6
But programming is rarely so successful. Suppose we had instead written
for (i = 0; i < args.length; i++) {
if (args[i].length() > 4) {
longWords.add(args);
// line 12
}
}
This mistake is
silly, but most
mistakes are.
On line 12 we are missing the index from the args array in the call to add.
Instead of adding the ith argument, we have added the entire argument array,
every time. Because we have no way to restrict the Vector class, the program
compiles correctly. We only notice the mistake when we type
4.1 Motivation (in case we need some)
71
java LongWords Fuzzy Wozzie had no hair
and get
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String;
at LongWords.main(LongWords.java:31)
The problem is eventually recognized (on line 31) when what we thought was
a String (but is actually an array) is removed as an Object (O.K.) and cast to a
String (not O.K.). Notice if our broken program is run with only short words:
java LongWords aye but what a bear that Fuzz was
no runtime error occurs because no words are added to the Vector.
4.1.1 Possible Solution: Specialization
Another solution to our problem would be to create a specialized class, a String-
Vector that was similar to the Vector class in every way, except that Objects
are replaced with Strings. The add method, for example, would be declared:
public class StringVector implements Cloneable
{
protected String elementData[];
protected int elementCount;
...
public void add(String obj)
{
// the data
// number of elements in vector
StringVector
ensureCapacity(elementCount+1);
elementData[elementCount] = obj;
elementCount++;
}
}
Compare this with the code that appears in Chapter 3 on page 51.
There are several things to note about this approach. First, it works. For
fun, we create a similar (erroneous) program, LongWords2, that makes use of
the StringVector:
public static void main(String[] args)
{
StringVector longWords = new StringVector();
int i;
for (i = 0; i < args.length; i++) {
if (args[i].length() > 4) {
longWords.add(args);
// line 12
}
}
...
for (i = 0; i < longWords.size(); i++) {
String word = longWords.get(i); // line 31
System.out.println(word+", length "+word.length());
}
}
LongWords2
72
Generics
Instead of using Vector we use StringVector. The compiler knows that the
add method must take a String, so passing an array of Strings to the add
method generates the error:
LongWords2.java:12: cannot find symbol
symbol
location: class StringVector
: method add(java.lang.String[])
longWords.add(args);
^
The success of this technique leads us to the following principle:
Principle 7 Write code to identify errors as soon as possible.
Here, (1) the actual source of the logical error was identified, and (2) it was
identified at compile-time.
To accomplish this, however, we had to write a new class and modify it
appropriately. This is, itself, an error-prone task. Also, we must rewrite the
class for every every new contained type. Soon we would be overwhelmed by
special-purpose classes. These classes are not related in any way and, it should
be noted, our original class, Vector, would never be used. Imagine the difficulty
of adding a new method to the Vector class that you wished to make available
to each of your specialized versions! A better solution is to create a generic class
or a class parameterized by the type of data it holds.
4.2 Implementing Generic Container Classes
Ideally, we would like to be able to construct container types, like Associations
and Vectors, that hold objects of one or more specific types. At the same time,
we would like to write each of these classes once, probably without prior knowl-
edge of their client applications. If this were possible, we could provide much of
the polymorphic utility of these classes as they were implemented in Chapters 1
and 3, and still identify errors in data types at compile-time. In Java 5, generic
or parameterized data types provide the programmer the necessarily flexibility.
4.2.1 Generic Associations
For simplicity, we first consider the signatures of the protected data and several
of the methods of the Association class, declared using parameterized types:2
package structure5;
public class Association
{
Association
2 The remainder of this text describes classes of the structure5 package. The structure package
provides Object-based implementation, for backward comparability with pre-Java5 compilers. Both
the structure and structure5 packages are available in a single jar file, bailey.jar, that may
be inserted in your CLASSPATH.
NNWSWSENEWSE4.2 Implementing Generic Container Classes
73
protected K theKey; // the key of the key-value pair
protected V theValue; // the value of the key-value pair
/*
for example:
Association personAttribute =
new Assocation("Age",34);
*/
public Association(K key, V value)
// pre: key is non-null
// post: constructs a key-value pair
public V getValue()
// post: returns value from association
public K getKey()
// post: returns key from association
public V setValue(V value)
// post: sets association’s value to value
}
At the time of their declaration, parameterized class name are followed by a list
of comma-separated type parameters in angle brackets. This book follows the
common convention that type parameters are indicated by single capital Latin
letters.3 In this example, K is a place holder for the type of the association’s
key, while V will be used to represent the actual type of the association’s value.
Within the class definition, these type variables may be used wherever class
names are used: declaring data variables and declaring method parameters and
return values. The one constraint is that
Generic types may not appear in array allocations.
The reason is technical and obviously not of concern in our declaration of
Associations.
The code associated with the generic implementation of the Association
class is, at this point, straightforward. For example, the setValue method would
be written:
public V setValue(V value)
{
V oldValue = theValue;
theValue = value;
return oldValue;
}
3 There is some dispute about this convention. See your instructor. Professional driver, closed
course. Your mileage may vary. Danger, Will Robinson. Some identifiers too small for three year
olds.
74
Generics
Notice that there are no casts in this code: since the value is declared to be of
type V, and since the return value for setValue is the same, no cast is necessary.
The removal of casts is a sign that type checking will happen in the compiler
and not while the program is running.
To make use of the generic version of a class, we must specify the actual
parameter types associated with the formal types demanded by Association.
For example, a new Association between a String and an Integer would be
declared:
Association personAttribute =
new Assocation("Age",34);
(Hotdoggers: the 34 here is, yes, autoboxed4 into an Integer.) Of course, if
the Association is declared within a class that is itself parameterized, formal
type parameters may be specified. We will see this on many occasions in future
chapters.
4.2.2 Parameterizing the Vector Class
We now consider the parameterization of a significant container class, Vector.
Because of the way that Vectors are implemented, there will be special techni-
cal details (associated with arrays) that pertain, for the most part, only to the
Vector class. Most other structures will avoid these difficulties because they
will make use of parameterized Vectors, and therefore build on the work we
do here. Still, for the purposes of efficiency, it may be useful to declare struc-
tures that make direct use of the array-based techniques described here.
(The reader may find it useful to review the implementation of the non-
generic Vector as declared in Chapter 3 before following along here, where the
technical details of the Vector implementation will not be reviewed.)
One thing seems fairly obvious: the parameterized Vector class should be
defined in terms of a single type—the type of each element within the Vector.
The declaration of the major method signatures of the Vector class is as follows:
public class Vector extends AbstractList implements Cloneable
{
private Object elementData[];
protected int elementCount;
protected int capacityIncrement;
protected E initialValue;
protected final static int defaultCapacity = 10; // def’t capacity, must be>0
// the data
// number of elements in vector
// the rate of growth for vector
// new elements have this value
public Vector()
// post: constructs a vector with capacity for 10 elements
4 Two syntactic features of Java 5 include autoboxing and its inverse, -unboxing. With autobox-
ing, primitive types are automatically “boxed” into their object equivalents if doing so would fix
a type incompatibility. In addition, the corresponding object types will be converted to primitive
equivalents, if necessary.
Yeah, and if we
eat our spinach
now, we won’t
get E. coli later.
Vector
4.2 Implementing Generic Container Classes
75
public Vector(int initialCapacity)
// pre: initialCapacity >= 0
// post: constructs an empty vector with initialCapacity capacity
public Vector(int initialCapacity, int capacityIncr)
// pre: initialCapacity >= 0, capacityIncr >= 0
// post: constructs an empty vector with initialCapacity capacity
that extends capacity by capacityIncr, or doubles if 0
//
public Vector(int initialCapacity, int capacityIncr, E initValue)
// pre: initialCapacity, capacityIncr >= 0
// post: constructs empty vector with capacity that begins at
//
//
initialCapacity and extends by capacityIncr or doubles
if 0.
New entries in vector are initialized to initValue.
public void add(E obj)
// post: adds new element to end of possibly extended vector
public E remove(E element)
// post: element equal to parameter is removed and returned
public boolean contains(E elem)
// post: returns true iff Vector contains the value
//
(could be faster, if orderedVector is used)
public E get(int index)
// pre: 0 <= index && index < size()
// post: returns the element stored in location index
public void insertElementAt(E obj, int index)
// pre: 0 <= index <= size()
// post: inserts new value in vector with desired index,
moving elements from index to size()-1 to right
//
public E remove(int where)
// pre: 0 <= where && where < size()
// post: indicated element is removed, size decreases by 1
public E set(int index, E obj)
// pre: 0 <= index && index < size()
// post: element value is changed to obj; old value is returned
}
First, it is important to note that the Vector holding objects of type E, the
Vector class, implements AbstractList. Anywhere where an Abstract-
List is required, Vector may be provided.5 The E in the Vector
identifies the formal type parameter, while the E of AbstractList is used
5 This reads like a law journal. A technical point important to note here is that a Vector
does not have any relation to Vector if S is a subclass of E. For example, you cannot assign a
76
Generics
to provide an actual type parameter to the AbstractList class. This, of course,
presumes that the AbstractList class has been implemented using a single
generic type parameter.
At the top of the declaration, we see the instance variables and constants
that are found in a Vector. The initialValue variable holds, for each object,
the value that is used to fill out the Vector as it is extended. Typically (and, in
most constructors, by default) this is null. Clearly, if it is non-null, it should
be a value of type E. The values elementCount, capacityIncrement, and the
constant defaultCapacity are all ints, unconstrained by the choice of the pa-
rameterized type.
The variable elementData is an extensible array (the array the supports the
Vector’s store): it should contain only objects of type E. Again, because of a
technical constraint in Java6, it is impossible to construct new arrays of any
type that involves a type parameter. We must know, at the time the class is
compiled, the exact type of the array constructed.
But this goes against our desire to construct a parameterized array of type E.
Our “workaround” is to construct an array of type Object, limit the type of the
elements to E with appropriate casts within mutator methods, and to declare the
array private eliminating the possibility that subclasses would be able to store
non-E values within the array.7 When values are retrieved from the array, we
can be confident that they are of the right type, and using casts (that we know
are trivial) we return values of type E.
Let’s look at the details. Here is the implementation of the most general
Vector constructor:
public Vector(int initialCapacity, int capacityIncr, E initValue)
{
Assert.pre(initialCapacity >= 0, "Nonnegative capacity.");
capacityIncrement = capacityIncr;
elementData = new Object[initialCapacity];
elementCount = 0;
initialValue = initValue;
}
The Vector constructor does not mention the type parameter in its name.
Why? The type parameter is obvious because the constructor declaration ap-
pears within the class Vector. When the value is constructed, of course,
Vector to a Vector, even though Integer values may be assigned to Numbers.
You might think about why this is the case: Why is this the case?
6 This constraint is related to the fact that every assignment to the array at runtime forces a runtime
check of the data type. This check is necessitated by the fact that, over time, alternative arrays—
arrays of different base types—can be assigned to a single array reference. Unfortunately Java 5
does not transmit parameterized type information to the running program. This means that param-
eterized types cannot be exactly checked at runtime, but they should be. The easiest way to enforce
correct checking is to make it impossible to construct arrays of parameterized types. We expect
future implementations of Java will address this issue, and that David Ortiz will be the American
League Most Valuable Player, but good things take time.
7 See more about privacy rights in Appendix B.8.
4.2 Implementing Generic Container Classes
77
the actual parameter appears within the new statement (see the example on
page 78). Within the constructor, the supporting array is allocated as a logically
empty array of Object, and the initial value (type E) is cached away within the
type. Is there any way that a non-E value can be stored in elementData, using
this method? No. The method ensures the safety of the types of Vector’s values.
If we ask this question for each mutator method, and the answer is uniformly
No, we can be sure the Vector contains only the correct types.
Now, consider a method that adds a value to the Vector:
public void add(E obj)
// post: adds new element to end of possibly extended vector
{
ensureCapacity(elementCount+1);
elementData[elementCount] = obj;
elementCount++;
}
The Vector is expanded and the new value is appended to the end of the array.
Is there a possibility of a type error, here? In a more extensive review, we might
check to make sure ensureCapacity allows only type E values in elementData.
(It does.) The second and third statements append an object of type E to the ar-
ray, so there is no opportunity for a non-E value to be assigned. Incidentally, one
might wonder what happens if the add method were called with a non-E type. It
is precisely what happens in the StringVector example at the beginning of the
chapter: an exception is raised at the site of the call, during compilation. We’ll
review this in a moment.
Next, we consider the get method. This accessor method retrieves a value
from within the array.
public E get(int index)
{
return (E)elementData[index];
}
By assumption (and eventually through proof) we believe the safety of the array
is intact to this point of access. Every element is of type E, even though it appears
in a more general array of Objects. The cast of the value, while required, will
always hold; runtime type errors will never occur at this point. The method,
then, returns a value of type E.
The set method is a combination of the prior two methods. It is useful to
convince yourself that (1) the method is type-safe, and (2) the cast is necessary
but always successful.
public E set(int index, E obj)
{
Assert.pre(0 <= index && index < elementCount,"index is within bounds");
E previous = (E)elementData[index];
elementData[index] = obj;
return previous;
}
78
Generics
It is, of course, possible to make type mistakes when using the Vector class.
They fall, however, into two distinct categories. Vector implementation mis-
takes may allow elements of types other than E into the array. These mistakes
are always possible, but they are the responsiblity of a single programmer. Good
design and testing will detect and elminate these compile-time and runtime er-
rors quickly. The second class of errors are abuses of the Vector class by
the client application. These errors are unfortunate, but they will be reported
at compile-time at the appropriate location, one that focuses the programmer’s
attention on the mistake.
We can now test our new implementation on working code and then attempt
to break it. In LongWords3, we consider, yet again, the implementation of our
program that checks for long argument words. The implementation makes use
of a Vector as follows:
LongWords3
public static void main(String[] args)
{
Vector longWords = new Vector();
int i;
for (i = 0; i < args.length; i++) {
if (args[i].length() > 4) {
longWords.add(args[i]);
// line 12
}
}
...
for (i = 0; i < longWords.size(); i++) {
String word = longWords.get(i); // line 31
System.out.println(word+", length "+word.length());
}
}
Because the Vector only contains String elements, there is no need
for a cast to a String on line 31. This makes the running program safer and
faster.
If the add statement were incorrectly written as:
longWords.add(args);
// line 12: woops!
the compiler would be confused—we’re passing an array of Strings to a class
that has only methods to add Strings. The resulting error is, again, a missing
symbol (i.e. a missing method) error:
LongWords3.java:12: cannot find symbol
symbol
location: class java.util.Vector
: method add(java.lang.String[])
longWords.add(args);
^
The compiler cannot find an appropriate add method.
By the way, you can get the behavior of an unparameterized Vector class by
simply using Vector