Java Reference
In-Depth Information
1.21
The ADT
set
is a bag that does not allow duplicate entries. Although we leave the specification and imple-
mentation of the set as exercises in this and subsequent chapters, we do want to present the standard inter-
face
Set
, which belongs to the package
java.util
within the Java Class Library. Sets that adhere to the
specifications of this interface do not contain a pair of objects
x
and
y
such that
x.equals(y)
is true.
The following method headers declared in the interface
Set
are similar to the methods within
our
BagInterface
. The differences between a method in
Set
and a corresponding method in
BagInterface
are highlighted.
public boolean
add(T newEntry)
public boolean
remove(Object anEntry)
public void
clear()
public boolean
contains(Object anEntry)
public boolean
isEmpty()
public int
size()
public
Object[] toArray()
Each of the interfaces
Set
and
BagInterface
declare methods that are not in the other.
C
HAPTER
S
UMMARY
An abstract data type, or ADT, is a specification of a data set and the operations on that data. This specifica-
tion does not indicate how to store the data or how to implement the operations, and it is independent of any
programming language.
●
When you use data abstraction to design an ADT, you focus on what you want to do with or to the data with-
out worrying about how you will accomplish these tasks. That is, you ignore the details of how you repre-
sent data and how you manipulate it.
●
The manifestation of the ADT in a programming language encapsulates the data and operations. As a result,
the particular data representations and method implementations are hidden from the client.
●
A collection is an object that holds a group of other objects.
●
A bag is a finite collection whose entries are in no particular order.
●
A client manipulates or accesses a bag's entries by using only the operations defined for the ADT bag.
●
When you add an object to a bag, you cannot indicate where in the bag it will be placed.
●
You can remove from a bag an object having either a given value or one that is unspecified. You also can
remove all objects from a bag.
●
A bag can report whether it contains a given object. It can also report the number of times a given object
occurs within its contents.
●
A bag can tell you the number of objects it currently contains and can provide an array of those objects.
●
Carefully specify the methods for a proposed class before you begin to implement them, using tools such as
CRC cards and UML notation.
●
After designing a draft of an ADT, confirm your understanding of the operations and their design by writing
some pseudocode that uses the ADT.
●
You should specify the action a method should take if it encounters an unusual situation.
●
Writing a Java interface is a way to organize a specification for an ADT.
●
Writing a program that tests a class before it is defined is a way to see whether you fully understand and are
satisfied with the specification of the class's methods.
●