Java Reference
In-Depth Information
public boolean contains(T anEntry);
/** Creates an array of all entries that are in this bag.
@return a newly allocated array of all the entries in the bag */
public T[] toArray();
} // end BagInterface
1.14
After specifying an ADT and writing a Java interface for its operations, you should write some Java
statements that use the ADT. Although we cannot execute these statements yet—after all, we have
not written a class that implements BagInterface —we can use them to confirm or revise both our
decisions about the design of the methods and the accompanying documentation. In this way, you
check both the suitability and your understanding of the specifications. It is better to revise the
design or documentation of the ADT now, instead of after you have written its implementation. An
added benefit of doing this task carefully is that you can use these same Java statements later to test
your implementation.
Question 6 Given the full bag aBag that you created in Question 1, write some Java state-
ments that display all of the strings in aBag . Do not alter the contents of aBag .
Programming Tip Write a test program before you implement a class
Writing Java statements that test a class's methods will help you to fully understand the spec-
ifications for the methods. Obviously, you must understand a method before you can imple-
ment it correctly. If you are also the class designer, your use of the class might help you see
desirable changes to your design or its documentation. You will save time if you make these
revisions before you have implemented the class. Since you must write a program that tests
your implementation sometime, why not get additional benefits from the task by writing it
now instead of later?
Note: Although we said that the entries in a bag belong to the same class, those entries can
also belong to classes related by inheritance. For example, assume Bag is a class that imple-
ments the interface BagInterface . If we create a bag of class C objects by writing
BagInterface<C> aBag = new Bag<C>();
aBag can contain objects of class C , as well as objects of any subclass of C .
The following section looks at two examples that use a bag. Later, these examples can be part
of a test of your implementation.
Using the ADT Bag
1.15
Imagine that we hire a programmer to implement the ADT bag in Java, given the interface and
specifications that we have developed so far. If we assume that these specifications are clear
enough for the programmer to complete the implementation, we can use the ADT's operations in a
 
 
Search WWH ::




Custom Search