Question
How would you create an InfixExpressionEvaluator class in Java that consists of... public static double evaluateInfix(String infix, int[] values) which takes in an infix expression
How would you create an InfixExpressionEvaluator class in Java that consists of...
public static double evaluateInfix(String infix, int[] values)
which takes in an infix expression and an array of numeric values.
I have to implement the infix expression evaluator using the MyStack class which is:
import java.util.EmptyStackException; import java.util.NoSuchElementException;
public class MyStack
// default constructor public MyStack() {
} private Node head;
public MyStack(T[] list) { head = null; for (int i = 0; i < list.length; i++) { push(list[i]); } } /** Adds a new entry to the top of this stack. @param newEntry An object to be added to the stack. */ public void push(T newEntry) { Node t = new Node(); t.next = head; t.data = newEntry; head = t; } /** Removes and returns this stack's top entry. * @param size @return The object at the top of the stack. @throws EmptyStackException if the stack is empty before the operation. */ public T pop() throws EmptyStackException{ if(isEmpty()) throw new EmptyStackException(); T t = peek(); head = head.next;
return t; } /** Retrieves this stack's top entry. @return The object at the top of the stack. @throws EmptyStackException if the stack is empty. */ public T peek()throws EmptyStackException { if(isEmpty()) throw new EmptyStackException(); return head.data; } /** Detects whether this stack is empty. @return True if the stack is empty. */ public boolean isEmpty() { return head == null; } /** Removes all entries from this stack. */ @Override public void clear() { head = null; } /** returns integer size of stack */ } // end StackInterface
Implementation details for the InfixExpressionEvaluator class:
- Input. Several different infix expressions will be used to test your program. The input expressions are in symbolic form (using the letters a through f, for instance, a + b * c - 9, without the double quotes)
- Input. The characters, a through f will represent symbols with values provided for each symbol. The values array provides values for any variable names in the infix expression. Index 0 provides the value for a, 1 for b, etc.
- If the parentheses of a given expression are unbalanced, the program is expected to throw an IllegalStateException
- Output. The result of the expression.
- Input. Several different infix expressions will be used to test your program. The input expressions are in symbolic form (using the letters a through f, for instance, a + b * c - 9, without the double quotes)
- Input. The characters, a through f will represent symbols with values provided for each symbol. The values array provides values for any variable names in the infix expression. Index 0 provides the value for a, 1 for b, etc.
- If the parentheses of a given expression are unbalanced, the program is expected to throw an IllegalStateException
- Output. The result of the expression.
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