Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3809341 • Letter: I

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 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:

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 file:

Command Prompt C:Users im Desktoplab-08solution 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 im 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 im Desktoplab-08solution 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 im DesktopVlab-08solution 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 im Desktoplab-08solution 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 im Desktop Vlab-08solution

Explanation / Answer

import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

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)
// ............................................................................................
  
static String applyOperator(String op1, String op2, String operator)
{
double num1 = Double.parseDouble(op1);
double num2 = Double.parseDouble(op2);
double result = 0;
switch(operator)
{
case "*":
result = num1*num2;
break;
case "/":
result = num1/num2;
break;
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
}
return String.valueOf(result);
}
  
// TAKES THE LIST Of OPERATORS ANd OPERANDS RETURNS RESULT AS A DOUBLE
static double evaluate( ArrayList<String> operatorList, ArrayList<String> operandList)
{
// STEP #1 SUGGEST YOU COPY/CONVERT THE OPERANDS LIST INTO A LIST OF DOUBLES
int i = 0;
while(true)
{
if (i == operatorList.size()) break;
if (operatorList.get(i).equals("*") || operatorList.get(i).equals("/"))
{
String result = applyOperator(operandList.get(i), operandList.get(i+1), operatorList.get(i));
operatorList.remove(i);
operandList.remove(i+1);
operandList.set(i, result);
}
else
{
i++;
}
}
  
for(i = 0; i < operatorList.size(); )
{
String result = applyOperator(operandList.get(i), operandList.get(i+1), operatorList.get(i));
operatorList.remove(i);
operandList.remove(i+1);
operandList.set(i, result);
}
  
return Double.parseDouble(operandList.get(0));
// 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