For this question, there is an interface called Queue and its implementation, a
ID: 3821613 • Letter: F
Question
For this question, there is an interface called Queue and its implementation, a class called Mystery Queue. public interface Queue {public abstract boolean isEmpty (); public abstract void enqueue (E value); public abstract E dequeue ();} The class MysteryQueue implements the interface Queue. The constructor (public MysteryQueue ()) creates an empty queue. The implementation can hold an arbitrarily large number of elements. Make no other assumption about this implementation; in particular, you cannot assume that it uses an array or a linked-list. For the class Utils below, write a class (static) method that returns true if the parameters ql and q2 designate queues having the same elements in the same order, and false otherwise. Furthermore, the queues designated by the parameters q1 and q2 must not be changed (i.e. after the method call, they must contain the same elements, in the same order, as before the call). public class Utils {public static Boolean eq (Queue q1, Queue q1) {Explanation / Answer
Please find the below solution as asked in the problem statement:
I have coded as per the hierarchy asked in the problem statement
interface Queue<E>
**************
package com.sagar.oracle;
public interface Queue<E> {
public abstract boolean isEmpty();
public abstract void enqueue(E value);
public abstract E dequeue();
}
*****************
class MysteryQueue<E>
package com.sagar.oracle;
public class MysteryQueue<E> implements Queue<E> {
public MysteryQueue() {
// TODO Auto-generated constructor stub
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
public void enqueue(E value) {
// TODO Auto-generated method stub
}
public E dequeue() {
// TODO Auto-generated method stub
return null;
}
}
Utils.java
**************
package com.sagar.oracle;
import java.util.HashSet;
import java.util.Queue;
public class Utils {
public static <E> boolean eq(Queue<E> q1,Queue<E> q2){
try {
if(q1.equals(q2)){
System.out.println("The elements are in the order");
}else{
System.out.println("The elements are not in the order");
}
} catch (Exception e) {
// TODO: handle exception
}
return true;
}
}
***********
P.S In the utils class we are using equals method for comparing elements for the list in order and other use is as follows
The java string equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.