Grouping Symbol Matching in java

Problem:

Create a Grouping Symbol Matching app in java.

Output:

Enter arithmetic expression:
(6+5) + {3 - 4} *
Stack is full
Expression is valid



Solution:

//ArrayBasedStack.java
import java.util.Arrays;

public class ArrayBasedStack implements Stack{

private Object[] arr;
private int top;

public ArrayBasedStack(int capacity) {
arr = new Object[capacity];
top = -1;
Arrays.fill(arr, null);
}

@Override
public int size() {
return top+1;
}

@Override
public boolean isEmpty() {
return top==-1;
}

@Override
public void push(Object element) throws StackException {
if(size() == arr.length)
throw new StackException("Stack is full");
++top;
arr[top] = element;
}

@Override
public Object pop() throws StackException {
if(isEmpty())
throw new StackException("Stack is Empty");
Object toReturn = arr[top];
arr[top] = null;
top--;
return toReturn;
}

@Override
public Object top() throws StackException {
if(isEmpty())
throw new StackException("Stack is Empty");
return arr[top];
}
}
//GroupingSymbol.java
import java.util.Scanner;

public class GroupingSymbol {

private static String opening = "([{";
private static String closing = ")]}";

public static void main(String[] args) {
String expression;
Scanner scan = new Scanner(System.in);

System.out.println("Enter arithmetic expression: ");
expression = scan.nextLine();

boolean valid = validate(expression);

if(valid) System.out.println("Expression is valid");
else System.out.println("Expression is valid");
}

private static boolean validate(String expression) {
int index = 0;
boolean valid = true;
char current, top;
ArrayBasedStack temp = new ArrayBasedStack(expression.length());

while(valid && index < expression.length()) {
current = expression.charAt(index);
if (opening.indexOf(current) != -1) {
try {
temp.push(current);
} catch (StackException e) {
System.out.println(e.getMessage());
return false;
}
} else if(closing.indexOf(current) != -1) {
try {
top = (Character) temp.pop();
} catch (StackException e) {
System.out.println(e.getMessage());
return false;
}

if(opening.indexOf(top) != closing.indexOf(current))
valid = false;


}

}

if (!temp.isEmpty()) valid = false;





return valid;
}
}
//Stack.java
package com.ikallassi.groupingsymbolmatching;

public interface Stack {

public int size();
public boolean isEmpty();
public void push(Object elemnt) throws StackException;
public Object pop() throws StackException;
public Object top() throws StackException;



}
//StackException.java
package com.ikallassi.groupingsymbolmatching;

public class StackException extends Exception {

private static final long serialVersionUID = -5592227328424820584L;

public StackException(String msg) {
super(msg);
}
}


No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact