Fill in the code for the evaluate() method in the Lab8.java starter file. Don\'t
ID: 3808690 • Letter: F
Question
Fill in the code for the evaluate() method in the Lab8.java starter file. Don't change main.
Suppose your expression is the following String: "20/10+30-4*6"
If you run this String through the tokenizer code given to you at the bottom of the SimpleCalc assignment page, you would end up with an ArrayList of operands and operators that looks something like this:
A simple strategy to evaluate this is to repeatedly search the operands array looking for only the * and / operators since they are the high priority operators. Each time you find a * or / at index i, you do the following:
copy the first * or / operator you find (at index i) and save into a String var called operator
pull out the operands at index i and i+1 of the operands array.
depending on whether operator is * or / perform that operation on the two operands and save to a double named result
replace (overwrite) the operand at index i with the result, i.e. operands.set(i, result ))
operands.remove(i+1) which was the second operand
lastly remove the operator at index i using operators.remove( i )
Repeat the above until you have processed every * / operator.
Then you process the + and - operators in the same manner as above.
When you are done the operators will be empty and the operands will have only one value in it which is your answer to the evaluation. If the operands list has more or less than 1 value in it, the expression was invalid -OR- your algorithm is broken. Also the operators list should be empty.
Command Prompt C:Users im DesktopVlab-08 solution java Lab8 1+1/1+1/2+1/6+1/24 +1/120+1/720+1/5040+1/40320+1/362880 expr 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/ 362880 operators: l+, /s /s +s /s Operands [1, 1, 1, 1, 2, 1, 6, 1, 24, 1, 120, 1, 720, 1, 5040, 1, 403 20, 1, 362 880 The expression: 1-1/1-1/2-1/6-1/24-1/ 120+1/720+1/5040+1/40320+1/362880 evalutes to 2.7182815255731922 C:Users im DesktopVlab-08 solution java Lab8 22/7 expr 22/7 Operators operands 22, 7] The expression: 22/7 evalutes to 3.142857142857143 C Users im DesktopVlab-08solution java Lab8 22/7+44/11 2/8 expr 22/7+44/11 2/8 Operators operands 22, 7, 44, 11, 2, 8] The expression 22/7+44/11 2/8 evalutes to 4.142857142857142 C Users tim Desktopllab-08 solution java Lab8 1+2+3+4+5+6+7+8+9+10 expr 1+2+3+4+5+6+7+8+9+10 Operators operands 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] The expression: 1+2+3+4+5+6+7+8+9+10 evalutes to 55.0 C Users im DesktopVlab-08solution java Lab8 1 2 expr: 1 2 operators: operands 1, 2] The expression 1 2 evalutes to 2.0 C: Users im Desktopllab-08 solution java Lab8 1 2 3 4 5 6 7*8 9 10 expr: 1 2 3 4 5 6 7 8 9 10 Operators operands 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] The expression: 1 2 3 4 5 6 7 8 9 10 evalutes to 3628800.0Explanation / Answer
PROGRAM CODE:
package expressions;
import java.util.*;
import java.io.*;
public class Lab8
{
public static void main( String[] args)
{
if ( args.length<1) { System.out.println("FATAL ERROR: Missing expression on command line example: java Lab8 3+13/5-16*3 "); System.exit(0); }
// Stolen directly from stackoverflow with just a few mods :)
String expr= args[0]; // i.e. somethinig like "4+5-12/3.5-5.4*3.14";
System.out.println( "expr: " + expr );
ArrayList<String> operatorList = new ArrayList<String>();
ArrayList<String> operandList = new ArrayList<String>();
// StringTokenizer is like an infile and calling .hasNext() that splits on + - / or *
StringTokenizer st = new StringTokenizer( expr,"+-*/", true );
while (st.hasMoreTokens())
{
String token = st.nextToken();
if ("+-/*".contains(token))
operatorList.add(token);
else
operandList.add(token);
}
System.out.println("Operators:" + operatorList);
System.out.println("Operands:" + operandList);
double result = evaluate( operatorList, operandList );
System.out.println("The expression: " + expr + " evalutes to " + result + " ");
} // END MAIN
// ............................................................................................
// Y O U W R I T E T H I S M E T H O D (WHCIH YOU MAY TRANSPLANT INTO SIMPLE CALC)
// ............................................................................................
// TAKES THE LIST Of OPERATORS ANd OPERANDS RETURNS RESULT AS A DOUBLE
static double evaluate( ArrayList<String> operatorList, ArrayList<String> operandList)
{
ArrayList<Double> operands = new ArrayList<>();
for(String value: operandList)
operands.add(Double.valueOf(value));
// STEP #1 SUGGEST YOU COPY/CONVERT THE OPERANDS LIST INTO A LIST OF DOUBLES
// NOW YOU HAVE AN ARRAYLIST OF STRINGS (OPERATORS) AND ANOTHER OF DOUBLES (OPERANDS)
// FIRST PROCESS ALL * and / operators FROM THE LIST
// SECOND PROCESS ALL + and - operators FROM THE LIST
// return operands.get(0); // IT SHOULD BE THE ONLY THING LEFT IN OPERANDS
for(int i=0; i<operatorList.size(); i++)
{
if(operatorList.get(i).equals("/") || operatorList.get(i).equals("*"))
{
String operator = operatorList.get(i);
double operand1 = operands.get(i);
double operand2 = operands.get(i+1);
double result = 0.0;
if(operator.equals("/"))
{
result = operand1/operand2;
}
else
{result = operand1 * operand2;
}
operatorList.remove(i);
operands.set(i, result);
operands.remove(i+1);
}
}
while(!operatorList.isEmpty())
{
int i=0;
if(operatorList.get(i).equals("+") || operatorList.get(i).equals("-"))
{
String operator = operatorList.get(i);
double operand1 = operands.get(i);
double operand2 = operands.get(i+1);
double result = 0.0;
if(operator.equals("+"))
{
result = operand1+operand2;
}
else
{result = operand1 - operand2;
}
operatorList.remove(i);
operands.set(i, result);
operands.remove(i+1);
}
else i++;
}
return operands.get(0); // just to make it compile. you should return the .get(0) of operands list
}
} // END LAB8
OUTPUT:
expr: 3+13/5-16*3
Operators:[+, /, -, *]
Operands:[3, 13, 5, 16, 3]
The expression: 3+13/5-16*3 evalutes to -42.4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.