Java Reference
In-Depth Information
E XERCISES
1.
If you add the objects x , y , and z to an initially empty queue, in what order will three dequeue operations remove
them from the queue?
2.
If you add the objects x , y , and z to an initially empty deque, in what order will three removeBack operations
remove them from the deque?
3.
After the following statements execute, what are the contents of the queue?
QueueInterface<String> myQueue = new LinkedQueue<String>();
myQueue.enqueue("Jane");
myQueue.enqueue("Jess");
myQueue.enqueue("Jill");
myQueue.enqueue(myQueue.dequeue());
myQueue.enqueue(myQueue.getFront());
myQueue.enqueue("Jim");
String name = myQueue.dequeue();
myQueue.enqueue(myQueue.getFront());
4.
After the following statements execute, what are the contents of the deque?
DequeInterface<String> myDeque = new LinkedDeque<String>();
myDeque.addToFront("Jim");
myDeque.addToFront("Jess");
myDeque.addToBack("Jill");
myDeque.addToBack("Jane");
String name = myDeque.removeFront();
myDeque.addToBack(name);
myDeque.addToBack(myDeque.getFront());
myDeque.addToFront(myDeque.removeBack());
myDeque.addToFront(myDeque.getBack());
5.
After the following statements execute, what are the contents of the priority queue?
PriorityQueueInterface<String> myPriorityQueue = new LinkedPriorityQueue<String>();
myPriorityQueue.add("Jim");
myPriorityQueue.add("Jess");
myPriorityQueue.add("Jill");
myPriorityQueue.add("Jane");
String name = myPriorityQueue.remove();
myPriorityQueue.add(name);
myPriorityQueue.add(myPriorityQueue.peek());
myPriorityQueue.add("Jim");
myPriorityQueue.remove();
6.
Consider strings that can be split so that their first half is the same as their second half (ignoring blanks, punctuation, and
case). For example, the string "booboo" can be split into "boo" and "boo" . Another example is "hello, hello" . After
ignoring blanks and the comma, the two halves of the string are the same. However, the string "rattan" has unequal
halves, as does the string "abcab" . Describe how you could use a queue to test whether a string has this property.
7.
Complete the simulation begun in Figure 10-6. Let Customer 6 enter the line at time 10 with a transaction time of 2.
8.
Assume that customerLine is an instance of the class WaitLine, as given in Segment 10.8. The invocation
customerLine.simulate(15, 0.5, 5) produces the following random events:
Customer 1 enters the line at time 6 with a transaction time of 3.
Customer 2 enters the line at time 8 with a transaction time of 3.
Customer 3 enters the line at time 10 with a transaction time of 1.
Customer 4 enters the line at time 11 with a transaction time of 5.
During the simulation, how many customers are served, and what is their average waiting time?
Search WWH ::




Custom Search