Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[In JAVA] Download the following interface: StackInterface.java Create a final class LinkedStack which implements this interface. The LinkedStack class has one instance variable topNode. It

[In JAVA] Download the following interface: StackInterface.java Create a "final" class "LinkedStack" which implements this interface. The LinkedStack class has one instance variable topNode. It has an inner class Node. public final class LinkedStack implements StackInterface { private Node topNode; // References the first node in the chain //default constructor ... // Implement the unimplemented methods private class Node { ... }// end Node }// end LinkedStack Test your code with the Diver class: Driver.java Required files:StackInterface.java EmptyStackException.java LinkedStack.java

StackInterface.java:

image text in transcribed

EmptyStackException.java:

image text in transcribed

Driver.java:

image text in transcribedimage text in transcribed

image text in transcribed

** An interface for the ADT stack. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 L*/ public interface StackInterface { /** Adds a new entry to the top of this stack. @param newEntry An object to be added to the stack. */ public void push (T newEntry); 6 7 8 9 10 11 12 13 14 15 16 17 18 19 CH /** Removes and returns this stack's top entry. @return The object at the top of the stack. @throws EmptyStackException if the stack is empty before the operation. */ public T pop(); HE /** Retrieves this stack's top entry. @return The object at the top of the stack. @throws EmptyStackException if the stack is empty. */ public T peek(); 20 21 22 /** Detects whether this stack is empty. @return True if the stack is empty. */ public boolean isEmpty(); 23 24 25 26 27 28 29 30 /** Removes all entries from this stack. */ public void clear(); } // end Stackinterface package LinkedListsolution; 1 2. 3 4 5 6 7 8 9 /** A class of runtime exceptions thrown by methods to indicate that a stack is empty. @author Frank M. Carrano @author Timothy M. Henry @version 4.0 - */ public class EmptyStackException extends RuntimeException [ { public EmptyStackexception() { this (null); } // end default constructor -7 D public EmptyStackexception (String message) { super (message); } // end constructor } // end EmptyStackexception 1 /** A driver that demonstrates the class LinkedStack. 3 @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public class Driver { public static void main(String[] args) A { testStackoperations(); System.out.println(" Done."); } // end main 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public static void testStackoperations() { System.out.println("Create a stack: "); StackInterface mystack = new LinkedStack(); System.out.println("isEmpty() returns + mystack.isEmpty()); A System.out.println(" Add to stack to get " + "Joe Jane Jill Jess Jim"); myStack.push("Jim"); mystack.push("Jess"); myStack.push("Jill"); myStack.push("Jane"); myStack.push("Joe"); System.out.println(" isEmpty() returns " + mystack.isEmpty()); System.out.println(" Testing peek and pop:"); D while (!mystack.isEmpty()) { String top = myStack.peek(); System.out.println(" " + top + " is at the top of the stack."); 5 6 7 8 9 0 1 2 3 top = mystack.pop(); System.out.println(top + " is removed from the stack."); } // end while System.out.print(" The stack should be empty: "); System.out.println("isEmpty() returns " + mystack.isEmpty()); System.out.println(" Add to stack to get " + "Jim Jess Joe "); myStack.push("Joe"); myStack.push("Jess"); mystack.push("Jim"); 5 6 7 8 9 0 1 2 3 4 5 6 7 System.out.println(" Testing clear:"); myStack.clear(); System.out.println("The stack should be empty: "); System.out.println(" isEmpty() returns + mystack.isEmpty()); 9 0 1 2 System.out.println(" myStack.peek() returns "); System.out.println(mystack.peek()); System.out.println(" myStack.pop() returns "); System.out.println(mystack.pop()); } // end testStackoperations // end Driver } /* Create a stack: isEmpty() returns true Add to stack to get Joe Jane Jill Jess Jim isEmpty() returns false Testing peek and pop: Joe is at the top of the stack. Joe is removed from the stack. Jane is at the top of the stack. Jane is removed from the stack. Jill is at the top of the stack. Jill is removed from the stack. Jess is at the top of the stack. Jess is removed from the stack. Jim is at the top of the stack. Jim is removed from the stack. The stack should be empty: isEmpty() returns true Add to stack to get Jim Jess Joe Testing clear: The stack should be empty: isEmpty() returns true myStack.peek() returns Exception in thread "main" java.util. Emptystackexception at Linkedstack.peek (Linkedstack.java:28) at Driver.testStackoperations (Driver.java:58) at Driver.main (Driver.java:12)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago