Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I use Eclipse IDE and Java Part I: Create an implementation of a stack for the f

ID: 3735603 • Letter: I

Question

I use Eclipse IDE and Java

Part I:

Create an implementation of a stack for the following interface:

/**

* A stack of items.

*/

public interface ItemStack<E> {

      /**

       * Push an item onto the stack

       *

       * @param item – item to be pushed

       */

public void pushItem(E item);

/**

       * Pop an item of the stack

       *

       * @return the popped item

       * @throw NoSuchElementException if stack is empty

       */

public E popItem();

/**

       * Return the item on the top of the stack without removing it

       *

       * @return the item on the top of the stack

       * @throw NoSuchElementException if stack is empty

       */

public E peekItem();

/**

       * Test if the stack is empty

       *

       * @return true if the stack is empty, otherwise false

       */

public boolean isEmpty();

/**

       * Clear the stack, removes all items.

       */

public void clear();

}

Part II:

Write JUnit test program to thoroughly test all of the methods of your ItemStack class

Explanation / Answer

import java.util.EmptyStackException;

/**

*

* A stack of items.

*

*/

interface ItemStack<E> {

   /**

   *

   * Push an item onto the stack

   *

   *

   *

   * @param item

   * – item to be pushed

   *

   */

   public void pushItem(E item);

   /**

   *

   * Pop an item of the stack

   *

   *

   *

   * @return the popped item

   *

   * @throw NoSuchElementException if stack is empty

   *

   */

   public E popItem();

   /**

   *

   * Return the item on the top of the stack without removing it

   *

   *

   *

   * @return the item on the top of the stack

   *

   * @throw NoSuchElementException if stack is empty

   *

   */

   public E peekItem();

   /**

   *

   * Test if the stack is empty

   *

   *

   *

   * @return true if the stack is empty, otherwise false

   *

   */

   public boolean isEmpty();

   /**

   *

   * Clear the stack, removes all items.

   *

   */

   public void clear();

}

/**

* A class of stacks whose entries are stored in a chain of nodes.

*

*/

public final class LinkedStack<E> implements ItemStack<E> {

   private Node topNode; // References the first node in the chain

   public LinkedStack() {

       topNode = null;

   } // end default constructor

   public void pushItem(E newEntry) {

       topNode = new Node(newEntry, topNode);

       // Node newNode = new Node(newEntry, topNode);

       // topNode = newNode;

   } // end push

   public E peekItem() {

       if (isEmpty())

           throw new EmptyStackException();

       else

           return topNode.getData();

   } // end peek

   public E popItem() {

       E top = peekItem(); // Might throw EmptyStackException

       assert (topNode != null);

       topNode = topNode.getNextNode();

       return top;

   } // end pop

   /*

   * // Question 1, Chapter 6: Does not call peek public T pop() { if

   * (isEmpty()) throw new EmptyStackException(); else { assert (topNode !=

   * null); top = topNode.getData(); topNode = topNode.getNextNode(); } // end

   * if

   *

   * return top; } // end pop

   */

   public boolean isEmpty() {

       return topNode == null;

   } // end isEmpty

   public void clear() {

       topNode = null; // Causes deallocation of nodes in the chain

   } // end clear

   public void displayUtil(Node topNode) {

       if (topNode == null)

           return;

       displayUtil(topNode.getNextNode());

       System.out.print(topNode.getData() + " ");

   }

   public void display() {

       // YOUR CODE HERE!

       if (isEmpty())

           System.out.println("The stack is empty.");

       else {

           System.out.print("BOTTOM ");

           displayUtil(topNode);

           System.out.print("TOP");

       }

   }

   private class Node {

       private E data; // Entry in stack

       private Node next; // Link to next node

       private Node(E dataPortion) {

           this(dataPortion, null);

       } // end constructor

       private Node(E dataPortion, Node linkPortion) {

           data = dataPortion;

           next = linkPortion;

       } // end constructor

       private E getData() {

           return data;

       } // end getData

       private void setData(E newData) {

           data = newData;

       } // end setData

       private Node getNextNode() {

           return next;

       } // end getNextNode

       private void setNextNode(Node nextNode) {

           next = nextNode;

       } // end setNextNode

   } // end Node

} // end LinkedStack