Queue Interface: public interface QueueInterface<T> { /** Task: Adds a new entry
ID: 3714829 • Letter: Q
Question
Queue Interface:
public interface QueueInterface<T>
{
/** Task: Adds a new entry to the back of the queue.
* @param newEntry an object to be added */
public void enqueue(T newEntry);
/** Task: Removes and returns the entry at the front of the queue.
* @return either the object at the front of the queue or, if the
* queue is empty before the operation, null */
public T dequeue();
/** Task: Retrieves the entry at the front of the queue.
* @return either the object at the front of the queue or, if the
* queue is empty, null */
public T getFront();
/** Task: Detects whether the queue is empty.
* @return true if the queue is empty, or false otherwise */
public boolean isEmpty();
/** Task: Removes all entries from the queue. */
public void clear();
} // end QueueInterface
Question: Write a splice method from the client perspective.
Because this is the client perspective, you do not know how the queues are implemented. Use only on the methods in QueueInterface.
The secondQueue should not be altered when the method completes. If you destroy the queue, be sure to rebuild it.
Explanation / Answer
Code
import java.util.LinkedList;
import java.util.Queue;
public class AddTwoQueue {
public static void main(String[] args) {
Queue<String> firstQueue=new LinkedList<String>();
Queue<String> secondQueue=new LinkedList<String>();
firstQueue.add("A"); //add elements to the queue
firstQueue.add("B");
firstQueue.add("C");
secondQueue.add("D");
secondQueue.add("E");
secondQueue.add("F");
System.out.println("First Queue:");
for(String s : firstQueue) {
System.out.println(s.toString()); //print the element of first queue
}
System.out.println(" Second Queue");
for(String s : secondQueue) {
System.out.println(s.toString()); //print the element of second queue
}
AddTwoQueue obj=new AddTwoQueue();
obj.splice(firstQueue, secondQueue); //call the function splice and pass the both queue
System.out.println(" After adding the queues First Queue:");
for(String s : firstQueue) {
System.out.println(s.toString()); //print the element of first queue
}
System.out.println(" After adding the queues Second Queue:");
for(String s : secondQueue) {
System.out.println(s.toString()); //print the element of second queue
}
}
public void splice(Queue<String> firstQueue,Queue<String> secondQueue) {
for(String s:secondQueue) {
firstQueue.add(s);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.