In: Computer Science
Write a Java application "WellFormedExpression.java". Start with
the file attached here.
Please read the comments and complete the function
'isWellFormed()'. It also has the main() to test the function with
several input strings. Feel free to change and add more tests with
different kinds of input data for correct implementation.
Note that you need MyStack.java and Stackinterface.java in the same
package.
WellFormedExpression.java
package apps;
public class WellFormedExpression {
static boolean isWellFormed(String s) {
/**** Create a stack for Characters
****/
char ch;
for(int i=0; i<s.length();i++)
{
ch =
s.charAt(i);
//
System.out.println(ch);
/************************************************
if ch is a
opening bracket, then push it to the stack
else if ch is a
closing bracket
if stack is empty, return
false
if stack.pop() does not match
the closing bracket, return false
else ignore (do
nothing)
Assume that only
three kinds of brackets are used - (), {}, []
************************************************/
}
// Here all characters are read.
The stack should be empty to return true.
return false; // change this to
your need.
}
public static void main(String[] args) {
// Example of using MyStack for
Character
// MyStack<Character> st = new
MyStack<Character>();
// st.push('a');
// st.push('b');
//
System.out.println(st.pop());
//
System.out.println(st.isEmpty());
// System.out.println(st.pop() ==
'a');
System.out.println(isWellFormed("(ab)")); // good
//
System.out.println(isWellFormed("(1+2)[]{3* 4}")); // good
//
System.out.println(isWellFormed("([]){}")); // good
//
System.out.println(isWellFormed("(((())))")); // good
//
System.out.println(isWellFormed("]")); // bad
//
System.out.println(isWellFormed("( xx [xxx) xx]")); // bad
//
System.out.println(isWellFormed("(()")); // bad
//
System.out.println(isWellFormed("())")); // bad
}
}
StackInterface.java
package apps;
public interface StackInterface<E> {
void push(E e);
E pop();
E peek();
boolean isFull();
boolean isEmpty();
}
MyStack.java
package apps;
public class MyStack<E> implements
StackInterface<E>{
E[] elements;
int top;
public MyStack() {
elements = (E[]) new
Object[5];
top = 0;
}
private void enlarge() {
E[] new_elements = (E[]) new
Object[elements.length*2];
for(int i=0; i<top; i++)
new_elements[i]
= elements[i];
elements = new_elements;
}
public void push(E e) {
if(isFull())
enlarge();
elements[top++] = e;
}
public E pop() {
if(isEmpty())
return
null;
return elements[top-- -1];
}
public E peek() {
if(isEmpty())
return
null;
return elements[top-1];
}
public boolean isFull() { return top ==
elements.length;}
public boolean isEmpty() { return top == 0;}
}
package apps;
public class WellFormedExpression {
static boolean isWellFormed(String s) {
/**** Create a stack for Characters ****/
MyStack<Character> st = new MyStack<Character>();
char ch;
for (int i = 0; i < s.length(); i++) {
ch = s.charAt(i);
// System.out.println(ch);
/************************************************
* if ch is a opening bracket, then push it to the stack
*
* else if ch is a closing bracket if stack is empty, return false if
* stack.pop() does not match the closing bracket, return false else ignore (do
* nothing)
*
* Assume that only three kinds of brackets are used - (), {}, []
************************************************/
/** if ch is open bracket push to stack */
if (ch == '(' || ch == '[' || ch == '{') {
st.push(ch);
}
/** if ch is closed bracket check if st.pop has its respective opening bracket */
else if (ch == ')') {
if (st.isEmpty() || st.pop() != '(') {
return false;
}
}
else if (ch == ']') {
if (st.isEmpty() || st.pop() != '[') {
return false;
}
}
else if (ch == '}') {
if (st.isEmpty() || st.pop() != '{') {
return false;
}
}
}
// Here all characters are read. The stack should be empty to return true.
if (st.isEmpty()) { // stk will be empty only if all open and closed brackets are correctly matched
return true;
}
return false; // change this to your need.
}
public static void main(String[] args) {
// Example of using MyStack for Character
// MyStack<Character> st = new MyStack<Character>();
// st.push('a');
// st.push('b');
// System.out.println(st.pop());
// System.out.println(st.isEmpty());
// System.out.println(st.pop() == 'a');
/** output is true if it is well formed and false if it is not */
System.out.println(isWellFormed("(ab)")); // good
System.out.println(isWellFormed("(1+2)[]{3* 4}")); // good
System.out.println(isWellFormed("([]){}")); // good
System.out.println(isWellFormed("(((())))")); // good
System.out.println(isWellFormed("]")); // bad
System.out.println(isWellFormed("( xx [xxx) xx]")); // bad
System.out.println(isWellFormed("(()")); // bad
System.out.println(isWellFormed("())")); // bad
}
}