Question
Write a JAVA method that uses the stack class to implement a postfix evaluator. Prompt the user for the name of an input file which
Write a JAVA method that uses the stack class to implement a postfix evaluator. Prompt the user for the name of an input file which will contain the postfix expressions. Each term and operator will be separated by spaces and the expression will be terminated by @. The expressions will use the operators +, -, *, % and /. All operations should be carried out as integer operations. For example, the result of the postfix expression 7 2 / is 3 by integer division.
You may assume that the data file contains only numeric terms, legal operator symbols (+, -, *, /, %) and the terminator @. If the expression is not legal, an appropriate message should be displayed. Sample input and output are shown below.
Hint: Input the items in the expression as strings and convert terms that arent operators (+, -, etc).
test1.txt
4 5 7 2 + - * @
13 4 / 3 + @
12 5 3 % + @
0 3 10 + @
-3 10 + @
+ 10 @
1 2 * 3 @
Sample Session user input is underlined
Please enter the file name: test1.txt
The expression 4 5 7 2 + - * evaluates to -16
The expression 13 4 / 3 + evaluates to 6
The expression 12 5 3 % + evaluates to 14
The expression 0 3 10 + evaluates to 7
The expression -3 10 + evaluates to 7
The expression + 10 is not legal
The expression 1 2 * 3 is not legal
Requirements
This work is to become familiar with the use of the JAVA framework stack class. Your program (main and related functions) may only use a stack and simple variables--no arrays or other data structures.
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