Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java programming: JUnit test Create a simple stack class based on the provided stack interface implemented using an array. * Hint you need a constructor

Java programming: JUnit test

Create a simple stack class based on the provided stack interface implemented using an array.

* Hint you need a constructor that creates the array something like this: * stackItems = (Item[]) new Object[maxSize];

Add methods should look like ( in the Stack class): add unimplemented methods

Using the interface create a simple stack class -> Use an array as the underlying data structure. -> Try to pass all the included unit tests (hint : import java.util.EmptyStackException

-> Add the missing test (Test Coverage). Add methods should look like ( in the Stack class): add unimplemented methods

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

public class Stack implements StackInterface { @Override public void push(Item item) { // TODO Auto-generated method stub @Override public void pop() { // TODO Auto-generated method stub @Override public Item top() { // TODO Auto-generated method stub return null; @Override public Item topAndPop() { // TODO Auto-generated method stub return null; Stackinterface - Notepad File Edit Format View Help //No need to update this file public interface StackInterface { * Insert a new item into the stack. * @param item the item to insert. public void push(Item item); * Remove the most recently inserted item from the stack. public void pop(); * Get the most recently inserted item in the stack. Does not alter the stack. public Item top(); * Return and remove the most recently inserted item from the stack. * @return the most recently inserted item in the stack. Item topAndPop(); * Test if the stack is logically empty. * @return true if empty, false otherwise. public boolean isEmpty(); *Make the stack logically empty. public void makeEmpty(); *Return the size of the stack. public int size(); TestStack - Notepad File Edit Format View Help import static org. junit. Assert. assertEquals; import static org. junit. Assert.assert False: import static org. junit.Assert.assertTrue; import static org. junit.jupiter.api. Assertions.assert Throws; import java.util. EmptyStackexception; import org. junit. jupiter.api. Test; //No need to update this file class TestStack { @Test public void testSizeStackEmpty() { Stack Integer> stack = new Stack(); assertEquals(e, stack.size()); @Test public void testisEmptyStackEmpty() { Stack stack = new Stack(); assert True(stack.isEmpty(); @Test public void testTopEmptyStack() throws EmptyStackException { Stack Integer> stack = new Stack(); assert Throws (EmptyStackException.class, ()->stack.top(), "Exception not throw as expected"); Test public void testPopEmptyStack() throws EmptyStackException { Stack Integer> stack = new Stack(); assertThrows (EmptyStackException.class, ()->stack.pop(), "Exception not throw as expected"); @Test public void test TopAndPopEmptyStack() throws EmptyStackException Stack Integer> stack - new Stack Integer>(); assertThrows (EmptyStackexception.class, ()->stack.topAndPop(), "Exception not throw as expected"); @Test public void testisEmptyAfterOnePush() { Stack Integer> stack = new Stack(); stack.push(5); assertFalse(stack.isEmpty(); @Test public void testSizeAfterOnePush() { Stack Integer> stack - new Stack(); stack.push(5); assertEquals(1, stack.size()); @Test public void test TopAfterOnePush() { Stack Integer> stack = new Stack(); stack.push(5); Integer expected = 5; assertEquals(expected, stack.top(); @Test public void testTopAfterPushPop() throws EmptyStackException { Stack Integer> stack - new Stack(); stack.push(5); stack.pop(); assertThrows (EmptyStackException.class, () ->stack.top(), "Exception not throw as expected"); Test public void test TopAfterPushPushPop() { Stack stack = new Stack(); stack.push(9); stack.push(7); stack.pop(); Integer expected - 9; assertEquals(expected, stack.top(); Test public void testSizeAfterMultiplePush() { Stack stack - new Stack(); stack.push(5); stack.push(4); stack.push(); stack.push(3); assertEquals(4, stack.size()); Test public void testSizeAfterPushPop() { Stack stack = new Stack(); stack.push(5); stack.pop(); assertEquals(e, stack.size()); @Test public void testisEmptyAfterMultiplePush() { Stack stack - new Stack Integer>(); stack.push(5); stack.push(4); stack.push(8); stack.push(3); assert False(stack.isEmpty(); public class Stack implements StackInterface { @Override public void push(Item item) { // TODO Auto-generated method stub @Override public void pop() { // TODO Auto-generated method stub @Override public Item top() { // TODO Auto-generated method stub return null; @Override public Item topAndPop() { // TODO Auto-generated method stub return null; Stackinterface - Notepad File Edit Format View Help //No need to update this file public interface StackInterface { * Insert a new item into the stack. * @param item the item to insert. public void push(Item item); * Remove the most recently inserted item from the stack. public void pop(); * Get the most recently inserted item in the stack. Does not alter the stack. public Item top(); * Return and remove the most recently inserted item from the stack. * @return the most recently inserted item in the stack. Item topAndPop(); * Test if the stack is logically empty. * @return true if empty, false otherwise. public boolean isEmpty(); *Make the stack logically empty. public void makeEmpty(); *Return the size of the stack. public int size(); TestStack - Notepad File Edit Format View Help import static org. junit. Assert. assertEquals; import static org. junit. Assert.assert False: import static org. junit.Assert.assertTrue; import static org. junit.jupiter.api. Assertions.assert Throws; import java.util. EmptyStackexception; import org. junit. jupiter.api. Test; //No need to update this file class TestStack { @Test public void testSizeStackEmpty() { Stack Integer> stack = new Stack(); assertEquals(e, stack.size()); @Test public void testisEmptyStackEmpty() { Stack stack = new Stack(); assert True(stack.isEmpty(); @Test public void testTopEmptyStack() throws EmptyStackException { Stack Integer> stack = new Stack(); assert Throws (EmptyStackException.class, ()->stack.top(), "Exception not throw as expected"); Test public void testPopEmptyStack() throws EmptyStackException { Stack Integer> stack = new Stack(); assertThrows (EmptyStackException.class, ()->stack.pop(), "Exception not throw as expected"); @Test public void test TopAndPopEmptyStack() throws EmptyStackException Stack Integer> stack - new Stack Integer>(); assertThrows (EmptyStackexception.class, ()->stack.topAndPop(), "Exception not throw as expected"); @Test public void testisEmptyAfterOnePush() { Stack Integer> stack = new Stack(); stack.push(5); assertFalse(stack.isEmpty(); @Test public void testSizeAfterOnePush() { Stack Integer> stack - new Stack(); stack.push(5); assertEquals(1, stack.size()); @Test public void test TopAfterOnePush() { Stack Integer> stack = new Stack(); stack.push(5); Integer expected = 5; assertEquals(expected, stack.top(); @Test public void testTopAfterPushPop() throws EmptyStackException { Stack Integer> stack - new Stack(); stack.push(5); stack.pop(); assertThrows (EmptyStackException.class, () ->stack.top(), "Exception not throw as expected"); Test public void test TopAfterPushPushPop() { Stack stack = new Stack(); stack.push(9); stack.push(7); stack.pop(); Integer expected - 9; assertEquals(expected, stack.top(); Test public void testSizeAfterMultiplePush() { Stack stack - new Stack(); stack.push(5); stack.push(4); stack.push(); stack.push(3); assertEquals(4, stack.size()); Test public void testSizeAfterPushPop() { Stack stack = new Stack(); stack.push(5); stack.pop(); assertEquals(e, stack.size()); @Test public void testisEmptyAfterMultiplePush() { Stack stack - new Stack Integer>(); stack.push(5); stack.push(4); stack.push(8); stack.push(3); assert False(stack.isEmpty()

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_2

Step: 3

blur-text-image_3

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

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

What is a flexible budget?

Answered: 1 week ago