*Create a toString method for the LinkedStack class. This method should create a
ID: 3640707 • Letter: #
Question
*Create a toString method for the LinkedStack class. This method should create and return a string that correctly represents the current stcak.Such a mthod could prove useful for testing and debugging the LinkedStack class and for testing and debugging applications that use the LinkedStack class.
*We decide to add a new operation to our Stack ADT called popTop, We add the following code to our StackInterface interface:
public Object popTop() throws StackUnderflowException;
//Throws StackUnderflowException if this stack is empty otherwise removes and returns top element from this stack.
An operation like this is often included for stacks. Implement the popTop method for the LinkedStack class.
*Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIs is :
public int sizeIs()
Write the code for sizeIs for the LinkedStack class (do not add any instance variable to the clas; each time sizeIs is called you must "walk" through the stack and count the nodes).
*In the "driver" application, exercise all new methods.
Demonstrate the proper handling of an exception by invoking the top() or popTop() method on a stack that is empty and on stack that is not empty.
Create an instance of the LinkedStack class and store Book objects in the LinkedStack. Create a number of BOOK objects. Exercise LinkedStack methods to prove they operate correctly with Book objects.
Linked stack
public class LinkedStack implements StackInterface { protected LLObjectNode top; // reference to the top of this stack
public LinkedStack() { top = null; }
public void push(Object element) { // Places element at the top of this stack. LLObjectNode newNode = new LLObjectNode(element); newNode.setLink(top); top = newNode; }
public void pop() { // Throws StackUnderflowException if this stack is empty, // otherwise removes top element from this stack. if (!isEmpty()) { top = top.getLink(); } else { throw new StackUnderflowException("Pop attempted on an empty stack."); } }
public Object top() { // Throws StackUnderflowException if this stack is empty, // otherwise returns top element from this stack. if (!isEmpty()) return top.getInfo(); else throw new StackUnderflowException("Top attempted on an empty stack."); }
public boolean isEmpty() { // Returns true if this stack is empty, otherwise returns false. if (top == null) return true; else return false; }}
StackInterface
public interface StackInterface { void pop() throws StackUnderflowException; // Throws StackUnderflowException if this stack is empty, // otherwise removes top element from this stack. Object top() throws StackUnderflowException; // Throws StackUnderflowException if this stack is empty, // otherwise returns top element from this stack.
void push(Object element); // Places element at the top of this stack. boolean isEmpty(); // Returns true if this stack is empty, otherwise returns false.
}
StackUnderFlowException
public class StackUnderflowException extends RuntimeException { public StackUnderflowException() { super(); }
public StackUnderflowException(String message) { super(message); }}
LLObjectNode
public class LLObjectNode { private LLObjectNode link; private Object info; public LLObjectNode(Object info) { this.info = info; link = null; } public void setInfo(Object info) { // Sets info Object of this LLObjectNode. this.info = info; }
public Object getInfo() { // Returns info Object of this LLObjectNode. return info; } public void setLink(LLObjectNode link) { // Sets link of this LLObjectNode. this.link = link; }
public LLObjectNode getLink() { // Returns link of this LLObjectNode. return link; }}
Explanation / Answer
I did not include the driver application as it is extremely similar to what I did before and you definitely can do it on your own. Here are the classes: public class LinkedStack implements StackInterface { protected LLObjectNode top; // reference to the top of this stack public LinkedStack() { top = null; } public void push(Object element) { // Places element at the top of this // stack. LLObjectNode newNode = new LLObjectNode(element); newNode.setLink(top); top = newNode; } public Object popTop() throws StackUnderflowException{ if (!isEmpty()) { Object ret = top.getLink(); top = top.getLink(); return ret; } else { throw new StackUnderflowException("Pop attempted on an empty stack."); } } // //Throws StackUnderflowException if this stack is empty otherwise removes and returns top element from this stack. public void pop() { // Throws StackUnderflowException if this stack is // empty, // otherwise removes top element from this // stack. if (!isEmpty()) { top = top.getLink(); } else { throw new StackUnderflowException("Pop attempted on an empty stack."); } } public Object top() { // Throws StackUnderflowException if this stack is // empty, // otherwise returns top element from // this stack. if (!isEmpty()) return top.getInfo(); else throw new StackUnderflowException("Top attempted on an empty stack."); } public boolean isEmpty() { // Returns true if this stack is empty, // otherwise returns false. if (top == null) return true; else return false; } public int sizeIs(){ LLObjectNode tmp = top; int count = 0; while(tmp != null){ count++; tmp = tmp.getLink(); } return count; } @Override public String toString(){ String ret = ""; LLObjectNode tmp = top; while(tmp != null){ ret += tmp.getInfo().toString() + " "; tmp = tmp.getLink(); } return ret; } } public class StackUnderflowException extends RuntimeException { public StackUnderflowException() { super(); } public StackUnderflowException(String message) { super(message); } } class LLObjectNode { private LLObjectNode link; private Object info; public LLObjectNode(Object info) { this.info = info; link = null; } public void setInfo(Object info) { // Sets info Object of this // LLObjectNode. this.info = info; } public Object getInfo() { // Returns info Object of this LLObjectNode. return info; } public void setLink(LLObjectNode link) { // Sets link of this // LLObjectNode. this.link = link; } public LLObjectNode getLink() { // Returns link of this LLObjectNode. return link; } } public interface StackInterface { void pop() throws StackUnderflowException; // Throws StackUnderflowException // if this stack is empty, // // otherwise removes top element // from this stack. Object top() // throws // StackUnderflowException; // // Throws // StackUnderflowException if // this stack is empty, // // otherwise returns top element // from this stack. void push(Object element); // Places element at the top of this stack. // boolean isEmpty(); // Returns true if this // stack is empty, otherwise returns false. public Object popTop() throws StackUnderflowException; public int sizeIs(); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.