Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The software I use is InteIIij IDEA Complete or modify MyStack.java to enable MyStackJUnitTest.java to run MyStack.java package utils; import org.w3c.dom.Node; import java.util.EmptyStackException; /***************************************************************** *

The software I use is InteIIij IDEA

Complete or modify MyStack.java to enable MyStackJUnitTest.java to run

MyStack.java

package utils; import org.w3c.dom.Node; import java.util.EmptyStackException; /***************************************************************** * This class MyStack implements a LIFO data structure using a * Singly Linked List (SLL). *****************************************************************/ public class MyStack { private Node first; private Node last; private int size; public MyStack() { first = null; last = null; size = 0; } //TODO : Complete Body with Data Fields, Methods and Classes public boolean isEmpty() { return last == null && size == 0; } public E peek() { if (isEmpty()) { return (E) "Empty"; } else { return last.data; } } private void detach() { Node oldLast = last; last = last.next; oldLast.next = null; } public E pop() { E lastItem = last.data; detach(); size--; return lastItem; } public E push(E item) { Node oldLast = last; Node newNode = new Node<>(oldLast, item); last = newNode; if (isEmpty()) first = newNode; size++; return item; } public int size() { return size; } public String toString() { if (isEmpty()) { return "[]"; } else { StringBuilder result = new StringBuilder("[" + last.data); for (Node node = last.next; node != null; node = node.next) result.append(", ").append(node.data); return result + "]"; } } static class Node { E data; Node next; public Node(E data) { this(null, data); } public Node(Node next, E data) { this.data = data; this.next = next; } } }

MyStackJUnitTest.java

package tests.junit; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; import utils.MyStack; import java.util.EmptyStackException; public class MyStackJUnitTest { @Test void testMyStack() { MyStack stack = new MyStack<>(); System.out.println("---------------- Test Default Constructor( ) ----------------"); System.out.println("size before: " + stack.size()); System.out.println("Stack Object Created "); assertEquals(0, stack.size()); assertTrue(stack.isEmpty()); System.out.println(stack); System.out.println("size after: " + stack.size()); System.out.println(); } @Test void testPushE() { MyStack stack = new MyStack<>(); System.out.println("---------------- Test push(item) ----------------"); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); System.out.println(stack); System.out.println("size: " + stack.size()); assertEquals(7, stack.size()); System.out.println(); } @Test void testIsEmpty() { System.out.println(); MyStack stack = new MyStack<>(); System.out.println("-------------- Test isEmpty() --------------"); System.out.println(stack); assertEquals(0, stack.size()); assertTrue(stack.isEmpty()); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); assertEquals(7, stack.size()); assertFalse(stack.isEmpty()); System.out.println(stack); } @Test void testPeekE() { System.out.println(); MyStack stack = new MyStack<>(); System.out.println("---------------- Test peek() ----------------"); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); System.out.println(stack); System.out.println("size before: " + stack.size()); assertEquals("Violet", stack.peek()); assertEquals("Violet", stack.pop()); assertEquals("Indigo", stack.peek()); assertEquals("Indigo", stack.pop()); assertEquals("Blue" , stack.peek()); assertEquals("Blue" , stack.pop()); System.out.println(stack); System.out.println("size after: " + stack.size()); assertEquals("Green" , stack.pop()); assertEquals("Yellow", stack.pop()); assertEquals("Orange", stack.pop()); assertEquals("Red" , stack.pop()); assertThrows(EmptyStackException.class, stack::pop); System.out.println(); } @Test void testPopE() { System.out.println(); MyStack stack = new MyStack<>(); System.out.println("---------------- Test pop() ----------------"); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); System.out.println(stack); System.out.println("size before: " + stack.size()); assertEquals("Violet", stack.pop()); assertEquals("Indigo", stack.pop()); assertEquals("Blue" , stack.pop()); assertEquals("Green" , stack.pop()); assertEquals("Yellow", stack.pop()); assertEquals("Orange", stack.pop()); assertEquals("Red" , stack.pop()); System.out.println(stack); System.out.println("size after: " + stack.size()); //Throws Exception if Empty. assertThrows(EmptyStackException.class, stack::pop); System.out.println(); } @Test void testSize() { MyStack stack = new MyStack<>(); System.out.println("---------------- Test size() ----------------"); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); System.out.println(stack); assertEquals(7, stack.size()); System.out.println("size end: " + stack.size()); System.out.println(); } @Test void testToString() { MyStack stack = new MyStack<>(); System.out.println("---------------- Test toString Display() ----------------"); assertEquals("Red" , stack.push("Red")); assertEquals("Orange", stack.push("Orange")); assertEquals("Yellow", stack.push("Yellow")); assertEquals("Green" , stack.push("Green")); assertEquals("Blue" , stack.push("Blue")); assertEquals("Indigo", stack.push("Indigo")); assertEquals("Violet", stack.push("Violet")); System.out.println(); System.out.println(); System.out.println("==== T E S T L I F O ==="); System.out.println(); //THIS IS DONE USING FIX (SEE LATER, IF NOT IMPLEMENTED IN THIS LAB) String value = "[Red, Orange, Yellow, Green, Blue, Indigo, Violet]"; assertEquals(value , stack.toString()); System.out.println(stack); assertEquals("Violet", stack.pop()); assertEquals("[Red, Orange, Yellow, Green, Blue, Indigo]" , stack.toString()); System.out.println(stack); assertEquals("Indigo", stack.pop()); assertEquals("[Red, Orange, Yellow, Green, Blue]" , stack.toString()); System.out.println(stack); assertEquals("Blue" , stack.pop()); assertEquals("[Red, Orange, Yellow, Green]" , stack.toString()); System.out.println(stack); assertEquals("Green" , stack.pop()); assertEquals("[Red, Orange, Yellow]" , stack.toString()); System.out.println(stack); assertEquals("Yellow", stack.pop()); assertEquals("[Red, Orange]" , stack.toString()); System.out.println(stack); assertEquals("Orange", stack.pop()); assertEquals("[Red]" , stack.toString()); System.out.println(stack); assertEquals("" , stack.pop()); @ assertEquals ("[]" , stack.toString()); @@ .println(); @ } }

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

Systems analysis and design

Authors: kenneth e. kendall, julie e. kendall

8th Edition

135094909, 013608916X, 9780135094907, 978-0136089162

More Books

Students also viewed these Programming questions