Question: I need some help writing some java code. Here is the guidlines. Your SimpleCalc project #7 requires you evaluate whatever arithmetic expression is found in

I need some help writing some java code. Here is the guidlines.

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.

 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 0 and 1 in operands list operand1=1 at index 0 operand2=1 at index 1 result = operand1*operand2 = 1.0 operand.set( 0, result ); overwrite operand at index 0 operand.remove( 1 ); // remove the second one operator.remove(0); // remove the * operator at index 0 after first operator processed: 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 operand1=1 at index 0 operand2=2 at index 1 result = operand1/operand2 = 0.5 operand.set( 0, result ); overwrite operand at index 0 operand.remove( 1 ); // remove the second one operator.remove(0); // remove the * operator at index 0 after second operator processed: operands: 0.5 3 4 operators + / ----------------------------------------------------------- scan operators for first occurance of * or / / is found at index=1 so its operands are always at 1 and 2 in operands list operand1=3 at index 1 operand2=4 at index 2 result = operand1/operand2 = 0.75 operand.set( 1, result ); overwrite operand at index 1 with result operand.remove( 2 ); // remove the second one operator.remove(1 ); // remove the * operator at index 1 after third operator processed: operands: 0.5 .75 operators + ----------------------------------------------------------- NO * or / OPERATORS REMAIN. NOW DO SIMILAR LOOP FOR + - 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 operand1=0.5 at index 0 operand2=0.75 at index 2 result = operand1+operand2 = 1.25 operand.set( 0, result ); overwrite operand at index 0 with result operand.remove( 1 ); // remove the second one operator.remove(0 ); // remove the * operator at index 0 after fourth operator processed: operands: 1.25 operators We are done. Answer is in operands.get(0) so we return 1.25 

Here is 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 
Command Prompt C:\Users\tim Desktop\lab-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 Operands 1, 1, 1, 1, 2, 1, 6, 1, 24, 1, 120, 1, 720, 1, 5040, 1, 403 20, 1, 36 2880] 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\tim Desktop Vlab-08 solution java Lab8 22/7 expr 22/7 Operators Operands: 22, 71 The expression 22/7 evalu to 3.142857142857143 C:\Users\tim Desktop\lab-08\solution java Lab8 22/7+44/11*2/8 expr 22/7 44/11 2/8 Operators CA, operands 122, 7, 44, 11, 2, 8] The expression 22/7+44/11 2/8 evalute s to 4.142857142857142 C:\Users\tim DesktopVlab-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 C: Usersltim Desktop Vlab-08 Vsolution java Lab8 1.2 expr 1 2 Operators operands 1, 2] The expression 1 2 evalutes to 2.0 C:\Users\tim Desktop\lab-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.0 C: Users\tim Desktop Vlab-08\solution

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!