# CSE 231: Assignment on Stacks Write a program that evaluates arithmetic expressions in infix notation that are not necessarily fully parenthesized. An arithmetic operation +, -, * or / has its usual precedence and associativity. You can assume that numbers are floating point numbers such as 10.23. Your program shall repeatedly request the user to input an expression and displays the value of the expression until the user does not want to continue. Your program needs to detect as many syntax errors as possible. You should also detect and capture the division by zero error. You are required to use the stack class provided by Java Application Programming Interface; please download Java documentation from java.sun.com and read about the Stack class provided by Java. You are also required to document your program so that it can be understood by others such as the grader and the instructor. Please submit 1. Analysis: test data; 2. Design: Class diagrams showing representation of data; A class invariant for each class; Pre/Post conditions for required operations; Algorithms for required operations. Algorithms can be described in English, flow diagrams, or sequence diagrams; 3. Code; 4. Screen snapshots of test runs.

CSE 231: Assignment on Stacks – Stack-Based Evaluation of Infix Arithmetic Expressions

1. Analysis:

For this assignment, we are required to write a program that evaluates arithmetic expressions in infix notation. The program should be able to handle arithmetic operations such as addition (+), subtraction (-), multiplication (*), and division (/). The expressions may not be fully parenthesized. Floating-point numbers, such as 10.23, are used in the expressions.

The program should repeatedly request the user to input an expression and display the value of the expression until the user decides not to continue. Apart from evaluating the expressions, the program also needs to detect and capture as many syntax errors as possible. Additionally, it should handle the division by zero error.

In order to accomplish this task, we are required to use the Stack class provided by the Java Application Programming Interface (API). The official Java documentation available on java.sun.com contains information about the Stack class, and we should refer to it for a better understanding of how to use the Stack class in our program.

2. Design:

Class Diagrams:

We need to represent the data in our program using appropriate classes. The main classes involved in this program are:

a) Stack: This is the class provided by the Java API. We will be using this class for implementing our stack-based evaluation of arithmetic expressions.

b) Expression: This class will represent an arithmetic expression entered by the user.

– Properties:

– expressionString: A string that stores the entered expression.

– Methods:

– getValue(): This method will evaluate the expression and return its corresponding value.

– validateSyntax(): This method will check if the expression has any syntax errors.

Class Invariants:

a) Stack:

– The Stack class always maintains the property that the top element of the stack is the last element added to it.

b) Expression:

– The expressionString property of the Expression class always contains a valid arithmetic expression.

Pre/Post Conditions:

a) getValue():

– Pre-condition: The expressionString property of the Expression object is a valid arithmetic expression.

– Post-condition: The method returns the evaluated value of the expression or throws an exception if there is an error.

b) validateSyntax():

– Pre-condition: The expressionString property of the Expression object is a valid arithmetic expression.

– Post-condition: The method checks if the expression has any syntax errors and either returns true if the expression is valid or throws an exception if there is an error.

Algorithms:

a) To evaluate the arithmetic expressions, we will use the stack-based evaluation algorithm:

– Read the expression one token at a time.

– If the token is an operand, push it onto the stack.

– If the token is an operator, pop the required number of operands from the stack and perform the operation. Push the result back onto the stack.

– Repeat until all tokens have been processed.

– The final result will be the value left on the stack.

b) To validate the syntax of the expression, we will use recursive descent parsing algorithm:

– Start reading the expression one token at a time.

– Evaluate each token and recursively validate the syntax.

– Check for any syntax errors and report if found.

3. Code:

TODO: Implement the code and provide it in this section.

4. Screen Snapshots:

TODO: Include screen snapshots of test runs in this section.

### Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.

Click Here to Make an Order Click Here to Hire a Writer