Question: I need help filling this code package app; import java.io.*; import java.util.*; import java.util.regex.*; import structures.Stack; public class Expression { public static String delims =

I need help filling this code

package app;

import java.io.*;

import java.util.*;

import java.util.regex.*;

import structures.Stack;

public class Expression {

public static String delims = " \t*+-/()[]";

/**

* Populates the vars list with simple variables, and arrays lists with arrays

* in the expression. For every variable (simple or array), a SINGLE instance is created

* and stored, even if it appears more than once in the expression.

* At this time, values for all variables and all array items are set to

* zero - they will be loaded from a file in the loadVariableValues method.

*

* @param expr The expression

* @param vars The variables array list - already created by the caller

* @param arrays The arrays array list - already created by the caller

*/

public static void

makeVariableLists(String expr, ArrayList vars, ArrayList arrays) {

/** COMPLETE THIS METHOD **/

/** DO NOT create new vars and arrays - they are already created before being sent in

** to this method - you just need to fill them in.

**/

}

/**

* Loads values for variables and arrays in the expression

*

* @param sc Scanner for values input

* @throws IOException If there is a problem with the input

* @param vars The variables array list, previously populated by makeVariableLists

* @param arrays The arrays array list - previously populated by makeVariableLists

*/

public static void

loadVariableValues(Scanner sc, ArrayList vars, ArrayList arrays)

throws IOException {

while (sc.hasNextLine()) {

StringTokenizer st = new StringTokenizer(sc.nextLine().trim());

int numTokens = st.countTokens();

String tok = st.nextToken();

Variable var = new Variable(tok);

Array arr = new Array(tok);

int vari = vars.indexOf(var);

int arri = arrays.indexOf(arr);

if (vari == -1 && arri == -1) {

continue;

}

int num = Integer.parseInt(st.nextToken());

if (numTokens == 2) { // scalar symbol

vars.get(vari).value = num;

} else { // array symbol

arr = arrays.get(arri);

arr.values = new int[num];

// following are (index,val) pairs

while (st.hasMoreTokens()) {

tok = st.nextToken();

StringTokenizer stt = new StringTokenizer(tok," (,)");

int index = Integer.parseInt(stt.nextToken());

int val = Integer.parseInt(stt.nextToken());

arr.values[index] = val;

}

}

}

}

/**

* Evaluates the expression.

*

* @param vars The variables array list, with values for all variables in the expression

* @param arrays The arrays array list, with values for all array items

* @return Result of evaluation

*/

public static float

evaluate(String expr, ArrayList vars, ArrayList arrays) {

/** COMPLETE THIS METHOD **/

// following line just a placeholder for compilation

return 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!