Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN JAVA. I don't know how to write code for Expression.java and Expression.java. The output has to look like below and also run on the

IN JAVA. I don't know how to write code for Expression.java and Expression.java. The output has to look like below and also run on the command terminal. Make sure to use the Stack ADT.

image text in transcribed

image text in transcribedimage text in transcribed

We are used to writing arithmetic expressions with the operator between the two operands a +b or c/d. If we write a+bc, however, we must apply precedence odders to avoid ambiguous evaluation. This type of expression is called infix expression. There are two other types of different but equivalent ways of writing expressions. Infix: X+Y: Operators are written in-between their operands. Infix expression needs extra infommation to make the odder of evaluation of the operators clear. precedence and associativity. brackets ( ). For example, A(B+C)/D. Postfix: X Y +: Operators are written after their operands. The above infix expression can be written using postfix notation a ABC+D/ Prefix: + X Y: Operators are written before their operands. The above infix expression can be written using prefix notation as /A+BCD You are to implement two class methods that can convert an infix expression to its equivalent postfix and evaluate the postfix expression. A tern will be an integer literal. The parameter to your methods will be an array of Strings and so is the retum value. Fach element of the array can cither be an integer literal or a binary operator +// (where is the power operator, xy is xy ) You are to implement the class Exprersion which includes the algorithms comerrToPporfix and evaluanePoryfix. They are class methods. Class name: Expression Conversion of Infix to Postfix: Method name: String[l] comvertToPostfix (String[] infixExpression) infexExpression - each element is a token that can be an operator or an integer literal. Operater can be +,,+,/, or . This method will comvert an infix to a postfix expression. Throw a RevrimeErception/iusg) if ingexErpression is not well-formed or not an integer literal. Conversion of Infix to Postfix: Method name: int evaluatePostfix (String] posfixiexpression) Throw RundweErception/msg) when posifirE Exceptiow is not well-formed or not an integer literal. You must implement your own generic Stack ADT for this project. What to test? Think of your own cases correct input, without parentheses or with matched parentheses; and incorrect input (with unmatched parentheses). Your program should catch the error with unmatched parentheses and when one of the operands is missing. Make sure that you deal with all error conditions, for example, pop/top on an empty stack. Write an application named Expression Test that will take an integer infix expression(s) from the command line. Using the Expression class to convert and output the equivalent postfix express, and the evaluation of the postfix expression. Linked Implementation of a Stack /** A class of stacks whose entries are stored in a chain of nodes. */ public final class LinkedStack implements StackInterface \{ private Node topNode; // References the first node in the chain public LinkedStack() \{ topNode = null; \}// end default constructor // // private class Node \{ private T data; // Entry in stack private Node next; // Link to next node // \} // end Node \} // end LinkedStack /** An interface for the ADT stack. */ public interface StackInterface T> \{ /** 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); /** Removes and returns this stack's top entry. @return The object at the top of the stack. @throws EmptyStackException if the stack is empty before the operation. */ public T pop(); /** 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(); /** Detects whether this stack is empty. @return True if the stack is empty. / public boolean isEmpty(); / Removes all entries from this stack. */ public void clear(); \}// end StackInterface

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Define Administration?

Answered: 1 week ago

Question

Define Decision making

Answered: 1 week ago

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago