Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*JAVA* Can somebody take a look at my current code and see if this is correct? I need to throw a different exception when a)(b

*JAVA*

Can somebody take a look at my current code and see if this is correct?

I need to throw a different exception when

a)(b

or similar expressions are entered.

The code is as follows.

Stack class:

public class ArrayStack {

private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10;

public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0; }

public void push(E value) { if (isFull() == true) { throw new ArrayIndexOutOfBoundsException("Stack is Full"); } else this.arrS[this.size++] = value;

}

public E pop() { if (isEmpty() == true) { throw new ArrayIndexOutOfBoundsException("Stack is Empty"); } return this.arrS[--this.size]; }

public boolean isFull() { return (this.size == MAX_STACK_SIZE) ? true : false;

}

public boolean isEmpty() { return (this.size == 0) ? true : false;

}

public int length() { return this.size; }

public E topValue() { if (isEmpty() == true) { throw new IllegalArgumentException(); } return this.arrS[this.size - 1]; }

public void clear() { this.size = 0;

}

public String show() {

StringBuilder sb = new StringBuilder(); sb.append(" "); for (int i = 0; i < this.size; i++) { sb.append(this.arrS[i]); sb.append(" ");

} sb.append(""); return sb.toString();

}

}

Main driver:

import java.util.Scanner;

public class a3main {

public static void main(String[] args) { int count = 0;

Scanner scan = new Scanner(System.in);

do { System.out.println("Enter an algebraic or arithmetic expression: ");

String arg = scan.nextLine();

ArrayStack stack = new ArrayStack(); boolean flag = true; for (int i = 0; i < arg.length(); i++) { count++; char current = arg.charAt(i);

if (current == '{' || current == '[' || current == '(') { try { stack.push(current); } catch (Exception e) { System.out.println("Excessive parenthesis combinations" + " "); flag = false; break;

} }

if ((current == '}' || current == ')' || current == ']')) { if (stack.isEmpty() == true) { System.out.println( "Encountered error on the right of the expression: Missing left parenthesis" + " "); flag = false; break; } char last = stack.topValue(); if (current == '}' && last == '{' || current == ')' && last == '(' || current == ']' && last == '[') stack.pop(); else {

flag = false;

System.out.println("Mismatch between right and left closing statements " + " "); break; } }

} if (flag) { if (stack.isEmpty() == true) { System.out.println("Correct parenthesis combinations: match" + " "); } else { System.out.println( "Encountered error on the left of the expression: Missing right parenthesis" + " "); } } } while (count != 0); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions