Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

8. Explain the contact hypothesis.

Answered: 1 week ago

Question

2. Define the grand narrative.

Answered: 1 week ago