CSC220 Lab Number 5
Due: 11:59PM, Tuesday, October 13
Your task is to write an internface for a queue and a class that implements
the interface using an array.
Call the interface QueueInt. It has to have generic specification
in the declaration, that is, QueueInt < E >.
The file name does not need to have such specification.
-
The interface has to have three methods that are discussed in class:
offer,
peek, and
poll.
-
Also it has to have two additional methods:
sneak that takes an object of type E and adds that object in front
of the queue and
size that returns the number of elements in the queue.
-
Both sneak and offer will return no value, unlike the offer method in Queue.
-
For example, in the case when E is Integer, offer(1), offer(3), offer(5), poll, and sneak(9) are executed in this order on an initially empty queue,
the resulting queue will have elements 9, 3, 5 in this order.
Design a class MyQueue that implements the interface using an array.
Here are a few things you have be careful about:
-
Use the generic in class specification, e.g., MyQueue.
-
Since you cannot create an array of generic object type E, you an array of
Object, such as Object[] and Object[10].
To store an object in the array, you may simply use the assignment, such as
``ArrayName[position]=TheObject''.
-
When you are return an entry of the array, you need to downcast the object
to the generic E.
-
The offer method is supposed to return a boolean value.
You may implement it in such a way that true is always returned.
-
The algorithms for adding an object at the end of the queue,
returning the object at the head of the queue,
doubling the size have been discussed in class.
Add to the class MyQueue a main method for testing whether the required
methods are properly functioning.