Question: IN JAVA: Today, you are going to construct a symbol table adding the code and making new classes specified below. Assume that the following example
IN JAVA:
Today, you are going to construct a symbol table adding the code and making new classes specified below.
Assume that the following example is the typical expression that the program will process:
9 * 4 ^ 2 / 8.3 + 3.5 -7
We further assume the following:
- Operands can be real numbers;
- There may be spaces between operators and operands for readability;
- Operators allowed are in the following set [^,*,/,+,-];
- The expression may contain illegal characters.
Before we are going to evaluate the above expression, we must add code to:
1) Make certain that the expression does not contain any illegal character and then remove all blanks from the expression.
2) We build a symbol table and replace all operands with variables that start with A. Assuming that there will be no more than 26 symbols. We will call the resulting expression infix.
3) So, the infix for the expression above shall be A*B^C/D+E-F
and the symbol table should be similar to
A 9.0
B 4.0
C 2.0
D 8.3
E 3.5
F 7.0
Assignment:
1) Name you main class Main.java
2) Construct a java class Step1.java that will use the class SymbolTable that you are going to construct below. (Specs below)
3) SymbolTable contains a group of [variable, value] pair.
So, construct a class named Variable.java that consists of the following:
1) variable: type character;
2) value: double type;
3) One Constructor that accepts two parameters: variable and value;
4) Getters for variable and value (setter is not needed.)
5) toString() to print the values of variable and value fields.
Construct SymbolTable.java class that starts with the following:
1) Constructor that accepts the expression string like the one given in (1) in the beginning.
2) A method named scan that accepts the expression string and returns the valid expression string that has all blanks removed. Returns an empty string if it contains invalid character.
3) strOperator: A string that contains only the operators used in expression.
4) strVariable: A string that contains only the variables (which take the place of operands when symbol table is complete.
5) table: ArrayList of Variable object. This is the symbol table.
6) buildTable: method that constructs the symbol table from the valid expression (i.e., the output of scan method.)
7) buildInfix: method that returns the infix from strVariable and strOperator.
8) toString(): print the contents of the table.
Now you have the proper infix to work with the methods provided by the textbook to convert it to postfix and then evaluate it.
use String.split since this is the most convenient way to split all your operands to an array of string.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
