Question: Part 1 Create a simple stack class based on the provided stack interface implemented using an array. Pass All the unit test that are provided

Part 1
Create a simple stack class based on the provided stack interface
implemented using an array.
Pass All the unit test that are provided for it.
Part 2
Create a method that checks if a string has balanced brackets, parentheses
square braces using your stack.
Create Unit Tests for it.
Pass All those tests
//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();
}
import java.util.HashMap;
//1. scroll down to update this file (implement balanced method)
//2. You need to create MainTest.jave that test all cases of this class.
public class Main {
public static void main(String[] args){
String input ="{()}";
String leftBrackets ="[{(";
String rightBrackets ="]})";
if(balanced(input,leftBrackets,rightBrackets))
{
System.out.println("Balanced");
}
else
{
System.out.println("Unbalanced");
}
}
public static boolean balanced(String checkBalanced, String leftBrackets, String rightBrackets )
{
//implement this method
return false;
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.EmptyStackException;
import org.junit.jupiter.api.Test;
//No need to update this file
class TestStack {
@Test
public void testSizeStackEmpty(){
Stack stack = new Stack();
assertEquals(0, stack.size());
}
@Test
public void testisEmptyStackEmpty(){
Stack stack = new Stack();
assertTrue(stack.isEmpty());
}
@Test
public void testTopEmptyStack() throws EmptyStackException {
Stack stack = new Stack();
assertThrows(EmptyStackException.class, ()->stack.top(), "Exception not throw as expected");
}
@Test
public void testPopEmptyStack() throws EmptyStackException {
Stack stack = new Stack();
assertThrows(EmptyStackException.class, ()->stack.pop(), "Exception not throw as expected");
}
@Test
public void testTopAndPopEmptyStack() throws EmptyStackException {
Stack stack = new Stack();
assertThrows(EmptyStackException.class, ()->stack.topAndPop(), "Exception not throw as expected");
}
@Test
public void testisEmptyAfterOnePush(){
Stack stack = new Stack();
stack.push(5);
assertFalse(stack.isEmpty());
}
@Test
public void testSizeAfterOnePush(){
Stack stack = new Stack();
stack.push(5);
assertEquals(1, stack.size());
}
@Test
public void testTopAfterOnePush(){
Stack stack = new Stack();
stack.push(5);
Integer expected =5;
assertEquals(expected, stack.top());
}
@Test
public void testTopAfterPushPop() throws EmptyStackException {
Stack stack = new Stack();
stack.push(5);
stack.pop();
assertThrows(EmptyStackException.class, ()->stack.top(), "Exception not throw as expected");
}
@Test
public void testTopAfterPushPushPop(){
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(8);
stack.push(3);
assertEquals(4, stack.size());
}
@Test
public void testSizeAfterPushPop(){
Stack stack = new Stack();
stack.push(5);
stack.pop();
assertEquals(0, stack.size());
}
@Test
public void testisEmptyAfterMultiplePush(){
Stack stack = new Stack();
stack.push(5);
stack.push(4);
stack.push(8);
stack.push(3);
assertFalse(stack.isEmpty());
}
@Test
public void testTopAfterMultiplePush(){
Stack stack = new Stack();
stack.push(5);
stack.push(4);
stack.push(8);
stack.push(3);
Integer expected =3;
assertEquals(expected, stack.top());
}
@Test
publi

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!