Assignment 1 Junit Test Solution

$25.00

Description

Part I: You are given the following LinkedStack.java 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;

        first.next = temp;

        n++;

    }

    //removes and returns the top element in the stack

    public Item pop()  {

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

        Item item = first.item

        first = first.next;

        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, LinkedStackTest.java, to test the operations of LinkedStack.

Here are some other requirements for the activity:

  • Create a LinkedStackTest.java and write test cases to test all the operations of LinkedStack.java
  • 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 LinkedStackFixed.java file and make sure all tests pass.

error: Content is protected !!