Question: I need help writing this Java code. Here is the instructions. Thanks! Here is the starting code Here is the text for the starter file:

I need help writing this Java code. Here is the instructions. Thanks! I need help writing this Java code. Here is the instructions. Thanks!

Here is the starting code Here is the text for the starter

Here is the starting code

file: import java.util.*; import java.io.*; public class Lab8 { public static void

Here is the text for the starter file:

import java.util.*; import java.io.*; public class Lab8 { public static void main( String[] args) { if ( args.length 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 

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: 20/10 30-4.6 expr operators: I/, operands 20 10, 30 6J 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 n 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. expr 1 1/2+3/4 operands 1 1 2 3 4 operators LOOP TO FIND PROCESS ALL OPS LEFT TO RIGHT scan operators for first occurance of or is found at index 0 so its operands are always at o and 1 in operands list erandlm1 at index 0 operand 2-1. at index 1 result operandi operand2 1.0 result overnerite operand at index 0 operand sets 0 operand remove 1 r the second one emove operator remove (0 remove the operator at index 0 after first operator processeda operands: 1 2 3 4 operators scan operators for first occurance of or is found at index 0 so its operands are always at 0 and 1 in operands list operandi 1 at index 0 operand 2 2 at index 1 result operandi /operand2 0.5 operand set 0, result overwrite operand at index 0 remove 1 remove operandi the second one operator remove (0); remove the operator at index 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!