Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to complete the required 2 methods. Help me with this, package app; import java.io.*; import java.util.*; import java.util.regex.*; import structures.Stack; public class Expression

I want to complete the required 2 methods.

Help me with this,

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions