Question
Using java: part 1: -Create a simple stack class based on the provided stack interface implemented using an array. -Pass ALL the unit test that
Using java:
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 this new method
-Pass All of the tests
Below I've pasted the code required for this.
Main:
import java.util.HashMap; import java.util.Stack; //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 ) { Stack st = new Stack(); int n = leftBrackets.length(); int m = rightBrackets.length(); for(int i=0; i st.push(leftBrackets.charAt(i)); } for(int j=0; j char temp = rightBrackets.charAt(j); if(!st.isEmpty()){ char temp2 = st.pop(); if(temp==']' && temp2 =='['){ continue; } else if(temp ==')' && temp2 == '('){ continue; } else if(temp =='}' && temp2 == '{'){ continue; } return false; } else{ return false; } } if(st.isEmpty()){ return true; } else{ return false; }
} }
StackInterface:
//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(); }
Leave a comment and I'll post the last class as it is too big to post in this one post!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started