public int search(Object o) //Returns teh 1-based postion where an object is on
ID: 3529177 • Letter: P
Question
public int search(Object o) //Returns teh 1-based postion where an object is on this stack. //The topmost item on the stack is considered to be at distance 1. Add this method to the StackListBased implementation given in this chapter. pubilc class StackListBased implements StackInterface { private ListInterface list; public StackListBased() { list = new ListReferenceBased(); } //end default constructor public boolean isEmpty() { return list.isEmpty(); } //end isEmpty public void push(Object newItem) { list.add(0, newItem); } //end push public Object pop() throws StackException { if(!list.isEmpty()) { Object temp = list.get(0); list.remove(0); return temp; } else { throw new StackException("StackException on " + "pop: stack empty"); } //end if } //end pop public void popAll() { list.removeAll(); } //end popAll public Object peek() throws StackException { if (!isEmpty()) { return list.get(0); } else { throw new StackException("StackException on " + "peek: stack empty"); } //end if } //end peek } //end StackListBasedExplanation / Answer
This code is called animplementationof the Stack ADT. In general, an implementation is a set of methods that satisfy the syntactic and semantic requirements of an interface.AStackobject contains an attribute nameditemsthat is a list of items in the stack. The initialization method setsitemsto the empty list.To push a new item onto the stack,pushappends it ontoitems. To pop an item off the stack,popuses the homonymous
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.