Java Reference
In-Depth Information
Try This 8-1 Creating a Queue Interface
To see the power of interfaces in action, we will look at a practical example. In earlier
chapters, you developed a class called Queue that implemented a simple fixed-size queue
for characters. However, there are many ways to implement a queue. For example, the
queue can be of a fixed size or it can be “growable.” The queue can be linear, in which
case it can be used up, or it can be circular, in which case elements can be put in as long as
elements are being taken off. The queue can also be held in an array, a linked list, a binary
tree, and so on. No matter how the queue is implemented, the interface to the queue remains
the same, and the methods put( ) and get( ) define the interface to the queue independently
of the details of the implementation. Because the interface to a queue is separate from its
implementation, it is easy to define a queue interface, leaving it to each implementation to
define the specifics.
In this project, you will create an interface for a character queue and three implement-
ations. All three implementations will use an array to store the characters. One queue will
be the fixed-size, linear queue developed earlier. Another will be a circular queue. In a cir-
cular queue, when the end of the underlying array is encountered, the get and put indices
automatically loop back to the start. Thus, any number of items can be stored in a circular
queue as long as items are also being taken out. The final implementation creates a dynam-
ic queue, which grows as necessary when its size is exceeded.
1. Create a file called ICharQ.java and put into that file the following interface defini-
tion:
 
Search WWH ::




Custom Search