Question: Your SimpleCalc project #7 requires you evaluate whatever arithmetic expression is found in the expression text field. For your Lab #8 you will write this
Your SimpleCalc project #7 requires you evaluate whatever arithmetic expression is found in the expression text field. For your Lab #8 you will write this evaluation method as a standalone program that is not a GUI. You may then transplant your solution into your SimpleCalc program. Lab8.java starter file 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: expr: 20/10+30-4*6 Operators:[/, +, -, *] Operands:[20, 10, 30, 4, 6] 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.
Here is the starter code we are given:
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 operatorList = new ArrayList(); ArrayList operandList = new ArrayList(); // 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 operatorList, ArrayList operandList) { // 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 return 0.0; // just to make it compile. you should return the .get(0) of operands list } } // END LAB8 Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
