Assignment 1 Junit Test Solution



Part I: You are given the following file which includes the implementation of Stack using LinkedList data structure.

import java.util.NoSuchElementException;

public class LinkedStack<Item>{

    private int n;          // size of the stack

    private Node first;     // top of stack

    private class Node {

        private Item item;

        private Node next;


    //initializes an empty stack.

    public LinkedStack() {

        first = null;

        n = 0;


    //returns true if this stack is empty; false otherwise

    public boolean isEmpty() {

        return first == null;


    //returns the number of items in the stack.

    public int size() {

        return n;


    //inserts an item to the stack 

    public void push(Item item) {

        Node temp = first;

        first = new Node();

        first.item = item; = temp;



    //removes and returns the top element in the stack

    public Item pop()  {

        if (isEmpty()) throw new ArrayIndexOutOfBoundsException(“Stack underflow”);

        Item item = first.item

        first =;

        return item;


    //returns (but does not remove) the item most recently added to this stack.

    public Item peek() {

        if (isEmpty()) throw new ArrayIndexOutOfBoundsException(“Stack underflow”);

        return first.item;



You are supposed to use Junit5 to create a test class with test cases,, to test the operations of LinkedStack.

Here are some other requirements for the activity:

  • Create a and write test cases to test all the operations of
  • Pushing 10000 objects into a stack should not take longer than 5 miliseconds
  • Pop and peek methods should throw NoSuchElementException

if stack is empty

  • Provide the list of failing test cases and explain the reason for failure
  • Resolve the issues by modifying the original class in a separate class file and make sure all tests pass.

error: Content is protected !!