Question
Write a main class name StackDrive.java to test the program. In the code provided below some of the function is not fully functional code them
Write a main class name StackDrive.java to test the program. In the code provided below some of the function is not fully functional code them and make it work. Code Isempyt and Display Function. Revise pop function to handle empty stack. When you pop when the stack is empty it should say \" THE STACK IS EMPTY. POPPED ELEMENT IS NULL.\"
PLEASE MAKE SURE THAT THE CODE MEETS ALL THE CRITERIA PROVIDE BELOW:
HERE IS THE CODE THAT NEEDS TO BE MODIFIED.
LinearNode.java
package jsjf;
/** * Represents a node in a linked list. * * @author Java Foundations * @version 4.0 */ public class LinearNode { private LinearNode next; private T element;
/** * Creates an empty node. */ public LinearNode() { next = null; element = null; }
/** * Creates a node storing the specified element. * @param elem element to be stored */ public LinearNode(T elem) { next = null; element = elem; }
/** * Returns the node that follows this one. * @return reference to next node */ public LinearNode getNext() { return next; }
/** * Sets the node that follows this one. * @param node node to follow this one */ public void setNext(LinearNode node) { next = node; }
/** * Returns the element stored in this node. * @return element stored at the node */ public T getElement() { return element; }
/** * Sets the element stored in this node. * @param elem element to be stored at this node */ public void setElement(T elem) { element = elem; } }
LinkedStack.java
package jsjf;
import jsjf.exceptions.*;
/** * Represents a linked implementation of a stack. * * @author Java Foundations * @version 4.0 */ public class LinkedStack implements StackADT { private int count; private LinearNode top;
/** * Creates an empty stack. */ public LinkedStack() { count = 0; top = null; }
/** * Adds the specified element to the top of this stack. * @param element element to be pushed on stack */ public void push(T element) { LinearNode temp = new LinearNode(element);
temp.setNext(top); top = temp; count++; }
/** * Removes the element at the top of this stack and returns a * reference to it. * @return element from top of stack * @throws EmptyCollectionException if the stack is empty */ public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException(\"stack\");
T result = top.getElement(); top = top.getNext(); count--;
return result; }
/** * Returns a reference to the element at the top of this stack. * The element is not removed from the stack. * @return element on top of stack * @throws EmptyCollectionException if the stack is empty */ public T peek() throws EmptyCollectionException { // To be completed as a Programming Project return null; // temp }
/** * Returns true if this stack is empty and false otherwise. * @return true if stack is empty */ public boolean isEmpty() { // To be completed as a Programming Project return true; // temp }
/** * Returns the number of elements in this stack. * @return number of elements in the stack */ public int size() { // To be completed as a Programming Project return 0; // temp }
/** * Returns a string representation of this stack. * @return string representation of the stack */ public String toString() { // To be completed as a Programming Project return \"\"; // temp } } StackADT.java
package jsjf;
/** * Defines the interface to a stack collection. * * @author Java Foundations * @version 4.0 */ public interface StackADT { /** * Adds the specified element to the top of this stack. * @param element element to be pushed onto the stack */ public void push(T element);
/** * Removes and returns the top element from this stack. * @return the element removed from the stack */ public T pop();
/** * Returns without removing the top element of this stack. * @return the element on top of the stack */ public T peek();
/** * Returns true if this stack contains no elements. * @return true if the stack is empty */ public boolean isEmpty();
/** * Returns the number of elements in this stack. * @return the number of elements in the stack */ public int size();
/** * Returns a string representation of this stack. * @return a string representation of the stack */ public String toString(); } EmptyCollectionException.java
package jsjf.exceptions;
/** * Represents the situation in which a collection is empty. * * @author Java Foundations * @version 4.0 */ public class EmptyCollectionException extends RuntimeException { /** * Sets up this exception with an appropriate message. * @param collection the name of the collection */ public EmptyCollectionException(String collection) { super(\"The \" + collection + \" is empty.\"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started