Java Reference
In-Depth Information
Design Decision: You might wonder about some of the decisions we made while defining
the class ResizableArrayBag , with questions such as the following:
Why is the method add a boolean method and not a void method? It always returns true!
Why did we bother to define isFull ? The bag is never full!
Why did we define the private method ensureCapacity ? Only one method, add , calls it!
The answers to the first two questions are the same: The class implements the interface
BagInterface , so we followed its specifications. As a result, we have two different implementations,
ArrayBag and ResizableArrayBag , each of which can be used by the same client. Our answer to the
third question reflects our approach to problem solving. To implement add , we needed to answer two
questions: When is an array full, and how do we expand a full array? Rather than risking the distrac-
tion of answering these questions while we were defining the method add , we chose to specify a pri-
vate method to provide those answers. Admittedly, the definition of this private method turned out to
be short. We could now integrate the body of the private method into that of add , but we have no
pressing reason to do so.
Question 20 What is the definition of a constructor that you could add to the class
ResizableArrayBag to initialize the bag to the contents of a given array?
Question 21 In the definition of the constructor described in the previous question, is it
necessary to copy the entries from the argument array to the array bag , or would a simple
assignment ( bag = contents ) be sufficient?
Question 22 What is an advantage of using an array to organize data? What is a disadvantage?
2.40
Testing the class. A program that tests the class ResizableArrayBag can create a bag whose initial
capacity is small—3, for example. This choice allows us to easily test the bag's ability to increase its
capacity. For instance, when the fourth item is added, the bag's capacity is doubled to 6. At the seventh
addition, the capacity is doubled again, this time to 12. Such a program, ResizableArrayBagDemo , is
available online at the topic's website.
Programming Tip: A class implementing a single interface that declares the operations
of an ADT should define the methods declared in the interface as its only public methods.
However, the class can also define private methods and protected methods.
The Pros and Cons of Using an Array to Implement the ADT Bag
2.41
This chapter discussed two implementations of the ADT bag that use an array to store a bag's entries.
An array is simple to use and enables you to access any element immediately, if you know its index.
Since we know the index of the last entry in the array, removing it is easy and fast. Similarly, adding
an entry at the end of the array is equally easy and fast. On the other hand, removing a particular entry,
if it occurs between other entries, requires us to avoid a gap within the array. To do so, we replace the
removed entry with the last entry in the array. This is an insignificant increase in execution time, as it
 
 
Search WWH ::




Custom Search