The interface ExtendedStack provides the usual stack methods, as well as the met
ID: 3803285 • Letter: T
Question
The interface ExtendedStack provides the usual stack methods, as well as the methods count, which returns the number of elements currently in the stack, and the method reverse, which reverses the elements in the stack. The interface definition is provided below. public interface Extended Stack {boolean isEmpty(); void push (String element); String pop(); String peek(); int count(); void reverse();} Someone has partially implemented this interface in the class ExtendedStackImplementation. Specifically, methods isEmtpy, push and pop are already (correctly) implemented. ExtendedStackImplementation has only one constructor, with no parameter. This implementation can accommodate any number of elements. Without making any assumptions on how ExtendedStackImplementation stores the stack (in particular, you cannot assume that the implementation uses an array) and using only the methods of ExtendedStack that have already been implemented (isEmtpy, push and pop), provide an implementation of the method peek. The method peek returns the top element of the stack without removing it. More precisely, after a call to the method peek, the content of the stack must be the same as it was before the call. You don't need to handle the error situations, in particular, you can assume that peek will not be called if the stack is empty.Explanation / Answer
ExtendedStack.java :
__________________
/*ExtendedStack interface */
public interface ExtendedStack {
boolean isEmpty();
void push(String element);
String pop();
String peak();
int count();
void reverse();
}
ExtendedStackImplementation.java :
_______________________________
/*ExtendedStackImplementation class*/
public class ExtendedStackImplementation implements ExtendedStack{
private String stack[];
private int top = -1;
public boolean isEmpty() {
if(stack.length==-1)
return true;
return false;
}
public void push(String element) {
stack[top]= element;
top++;
}
public String pop() {
String element = stack[top];
top--;
return stack[top];
}
public String peak() {
return stack[top];
}
public int count() {
return stack.length;
}
public void reverse() {
for(int i=0;i<stack.length;i++)
System.out.println(stack[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.