Problem 2 (20 points) A dozen stack is a stack with a fixed size of 12 elements.
ID: 3747955 • Letter: P
Question
Problem 2 (20 points)A dozen stack is a stack with a fixed size of 12 elements. If a dozen stack is full, then the element that has been on the stack, the longest, is removed to make room for a new new element. For this problem, you are required to design a class named DozenStack.java, which extends DozenStackAbstract.java, and implements push method to captures this specification of a dozen stack
Problem 2 (20 points)
A dozen stack is a stack with a fixed size of 12 elements. If a dozen stack is full, then the element that has been on the stack, the longest, is removed to make room for a new new element. For this problem, you are required to design a class named DozenStack.java, which extends DozenStackAbstract.java, and implements push method to captures this specification of a dozen stack
Problem 2 (20 points)
A dozen stack is a stack with a fixed size of 12 elements. If a dozen stack is full, then the element that has been on the stack, the longest, is removed to make room for a new new element. For this problem, you are required to design a class named DozenStack.java, which extends DozenStackAbstract.java, and implements push method to captures this specification of a dozen stack
Explanation / Answer
import java.util.*;
class DozenStack extends DozenStackAbstract{
int count=0;
Deque<Integer> d = new ArrayDeque<Integer>(12); // create a arraydeque with size 12
void push(int e){
this.count++;
if(count>=13){ //check for count
d.removeLast(); // then remove element from stack which we add first
}
d.addFirst(e); // add element to the stack
}
public static void main(String args[]){
DozenStack st = new DozenStack();
for(int i=0;i<12;i++)
st.push(i);
st.push(23);
for(Iterator itr = st.d.iterator(); itr.hasNext();)
{
System.out.println(itr.next());
}
}
}