Modify the given IntegerQueue interface so that it is a generic Queue interface
ID: 3811946 • Letter: M
Question
Modify the given IntegerQueue interface so that it is a generic Queue interface (nameit IQueue) and write a generic class to implement it. You need to show therepresentation and some plausible code for the constructor and enqueue operations.No need to write contracts or any other assertions. This question says plausible codebecause no correspondence, etc. is given or needs to be written.
public class Queue3 implements IntegerQueue {
/**
* invariant 0 <= front <= contents.length;
* correspondence conceptual this = contents[front..contents.length – 1];
*/ Integer[] contents; int front; Queue3(int len) {
} Integer dequeue() {
}
}
Explanation / Answer
public class Queue3 implements IntegerQueue {
/**
* invariant 0 <= front <= contents.length;
* correspondence conceptual this = contents[front..contents.length – 1];
*/
Integer[ ] contents;
int front;
Queue3(int len) {
contents = new Integer[len];
front = 0;
}
Integer dequeue() {
if(front == 0)
return null;
Integer x = contents[front];
front = front + 1;
return x;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.