# complete 2 methods to evaluate arithmetic expressions in java

The assignment is to complete 2 methods:

makeVariableLists - This method populates the vars and arrays lists with Variable and Array objects, respectively, for the simple variable and arrays that appear in the expression.

evaluate - This method evaluates the expression.

Everything else except for these 2 methods is already implemented and there is no need to modify it. The project includes all helper methods required, as well as 2 test files to make sure the program works. The details are in the PDF, please read carefully. I think using stacks is one of the recommended methods to go about this, in which case the stack class is already provided. If possible, please make comments within the code, thank you.

eclipse zip + pdf: https://drive.google.com/drive/folders/10EiCAkr-iBeIlvQ4qC6PKbsjCojbOPhT?usp=sharing

## Get Help With a similar task to - complete 2 methods to evaluate arithmetic expressions in java

##### Additional Instructions:

Expressions Here are some sample expressions of the kind your program will evaluate: 3 Xyz 3-4*5 a-(b+A[B[2]])*d+3 A[2*(a+b)] (varx + vary*varz[(vara+varb[(a+b)*33])])/55 The expressions will be restricted to the following components: Integer constants Simple (non-array) variables with integer values Arrays of integers, indexed with a constant or a subexpression Addi�on, subtrac�on, mul�plica�on, and division operators, i.e. '+','-','*','/' Parenthesized subexpressions Note the following: Subexpressions (including indexes into arrays between '[' and ']') may be nested to any level Mul�plica�on and division have higher precedence than addi�on and subtrac�on Variable names (either simple variables or arrays) will be made up of one or more le�ers ONLY (nothing but le�ers a-z and A-Z), are case sensi�ve (Xyz is different from xyz) and will be unique. Integer constants may have mul�ple digits There may any number of spaces or tabs between any pair of tokens in the expression. Tokens are variable names, constants, parentheses, square brackets, and operators. Implementa�on and Grading On the Autolab assignment page, you will you will see a file named expression_project.zip, which is an Eclipse project file. Download it to your computer, then follow the instruc�ons on the Eclipse page under the sec�on "Impor�ng a Zipped Project into Eclipse" to get the en�re project into your Eclipse workspace. You will see a project called Expression Evaluation with the following classes in package app: Variable This class represents a simple variable with a single value. Your implementa�on will create one Variable object for every simple variable in the expression (even if there are mul�ple occurrences of the same variable). You don't have to implement anything in this class, so do not make any changes to it. Array This class represents an array of integer values. Your implementa�on will create one Array object for every array in the expression (even if there are mul�ple occurrences of the same array). You don't have to implement anything in this class, so do not make any changes to it. Expression This class consists of methods for various steps of the evalua�on process: 1. 20 pts: makeVariableLists - This method populates the vars and arrays lists with Variable and Array objects, respec�vely, for the simple variable and arrays that appear in the expression. You will fill in the implementa�on of this method. Make sure to read the comments above the method header to get more details. 2. loadVariableValues - This method reads values for all simple variables and arrays arrays from a file, into the Variable and Array objects stored in the vars and arrays array lists. This method is already implemented, do not make any changes. 3. 60 pts: evaluate - This method evaluates the expression. You will fill in the implementa�on of this method. Evaluator, the applica�on driver, which calls methods in Expression. You may use this to test your implementa�on. There are two sample test files etest1.txt and etest2.txt, appearing directly under the project folder. You are also given the following class in package structures: Stack, to be (op�onally) used in the evalua�on process Do not add any other classes. In par�cular, if you wish to use stacks in your evalua�on implementa�on do NOT use your own stack class, ONLY use the one you are given. The reason is, we will be using this same Stack class when we test your implementa�on. Notes on tokenizing the expression You will need to separate out ("tokenize") the components of the expression in makeVariableLists and evaluate. Tokens include operands (variables and constants), operators ('+','-','*','/'), parentheses and square brackets. It may be helpful (but you are not required) to use java.util.StringTokenizer to tokenize the expression. The delims field in the Expression class may be used in the tokenizing process. The documenta�on of the StringTokenizer class says this: "StringTokenizer is a legacy class that is retained for compa�bility reasons although its use is discouraged in new code. It is recommended that anyone seeking this func�onality use the split method of String or the java.util.regex package instead." For the purpose of this assignment, you may use StringTokenizer without issue. Alterna�vely, you may use the split method of the String class, or the Pattern and Matcher classes in the package java.util.regex. Or, you may simply parse the expression by scanning it a character at a �me. Rules of implementa�on!: You may NOT modify any of the files except Expression.java in ANY way. You may NOT make ANY modifica�ons to Expression.java EXCEPT: Write in the bodies of the methods you are asked to implement, Add private helper methods as needed (including the recursive evaluate method discussed below.) Note that the java.io.*, java.util.*, and java.util.regex.* import statements at the top of the file allow for using ANY class in java.io, java.util, and java.util.regex without addi�onal specifica�on or qualifica�on. Guidelines and recommenda�ons for implemen�ng evaluate https://docs.oracle.com/javase/8/docs/api/java/util/StringTokenizer.html Recursion (op�onal) for sub-expressions in parentheses While recursion is op�onal for this assignment, using it to evaluate subexpressions will make it a LOT easier to write working code. (This is a great opportunity to learn how to use recursion in a realis�c situa�on!!) There are a couple of coding op�ons if you want to use recursion: One op�on is to make the public evaluate method itself recursive. So, for instance, if the main expression is a-(b+A[B[2]])*d+3 01234567891111111 (these are the positions of the characters in the expression) 0123456 then, to recursively evaluate the subexpression in parentheses, you may call the recursive evaluate method like this: float res = evaluate(expr.substring(3,11), vars, arrays); Another op�on is to write a separate private recursive evaluate method, with two indexes that mark the start and end of the subexpression in the main expression. Then, for the above example, you can call the recursive method like this: float res = evaluate(expr, 3, 11, vars, arrays); (The expr parameter is the original expression for every call.) And, to start with, you may call the recursive evaluate method from the public evaluate method like this: return evaluate(expr, 0, expr.length()-1, vars, arrays); which is the en�re expression. You will need to use this second op�on if you want to include other parameters in your recursive evaluate. In either case, the auto grader will call the public evaluate method. Recursion (op�onal) for array index expressions (within '[ and ']'), using the same approach as above. A stack may be used to store the values of operands as well as the results from evalua�ng subexpressions - see next point. Since * and / have precedence over + and -, it would help to store operators in another stack. (Think of how you would evaluate a+b*c with operands/intermediate results on one stack and operators on the other.) When you implement the evaluate method, you may want to test as you go, implemen�ng code for and tes�ng simple expressions, then building up to more complex expressions. The following is an example sequence of the kinds of expressions you may want to build with: 3 a 3+4 a+b 3+4*5 a+b*c Then introduce parentheses Then try nested parentheses Then introduce array subscripts, but no parentheses Then try nested subscripts, but no parentheses Then try using parentheses as well as array subscripts Then try mixing arrays within parentheses, parentheses within array subscripts, etc. Correctness of expression and input files All input expressions will be correctly forma�ed All input files with values for variables and arrays will be correctly forma�ed, and will be guaranteed to have values for all variables in the expression that is being evaluated So you don't need to do any checking for correctness of inputs in any of the methods. Running the evaluator You can test your implementa�on by running the Evaluator driver on various expressions and input variable values file. When crea�ng your own variable values files for tes�ng, make sure they are directly under the project folder, alongside etest1.txt and etest2.txt. Since you are not going to turn in the Evaluator.java file, you may introduce debugging statements and other methods (such as prin�ng out the variables or arrays array lists) as needed. No variables Enter the expression, or hit return to quit => 3 Enter variable values file name, or hit return if no variables => Value of expression = 3.0 Enter the expression, or hit return to quit => 3-4*5 Enter variable values file name, or hit return if no variables => Value of expression = -17.0 Enter the expression, or hit return to quit => Neither of the expressions above have variables, so just hit return when asked for the variable values file name. Variables, values loaded from file Enter the expression, or hit return to quit => a Enter variable values file name, or hit return if no variables => etest1.txt Value of expression = 3.0 Enter the expression, or hit return to quit => Since the expression has a variable, a, the evaluator needs to be supplied with a file that has a value for it. Here's what etest1.txt looks like: a 3 b 2 A 5 (2,3) (4,5) B 3 (2,1) d 56 Each line of the file begins with a variable name. For simple variables, the name is followed by the variable's integer value. For arrays, the name is followed by the array's length, which is followed by a series of (index,integer value) pairs. Note: The index and integer value pairs must be wri�en with no spaces around the index or integer value. So, for instance, (2, 3) or ( 2,3) or (2 ,3) are all incorrect. Make sure you adhere to this requirement when you create your own input files for tes�ng. If the value at a par�cular array index is not explicitly listed, it is set to 0 by default. So, in the example above, A = [0,0,3,0,5] and B = [0,0,1] Note that the variable values file can have values for any number of variables, so that it can be used as input for several expressions that contain one or more of the variables in the file. Here are a couple more evalua�ons of expressions for which the variable values are loaded from etest1.txt: Enter the expression, or hit return to quit => (a + A[a*2-b]) Enter variable values file name, or hit return if no variables => etest1.txt Value of expression = 8.0 Enter the expression, or hit return to quit => a - (b+A[B[2]])*d + 3 Enter variable values file name, or hit return if no variables => etest1.txt Value of expression = -106.0 Enter the expression, or hit return to quit => For a change of pace, here's etest2.txt, which has the following variables and values: varx 6 vary 5 arrayA 10 (3,5) (8,12) (9,1) And here are evalua�ons using this file: Enter the expression, or hit return to quit => arrayA[arrayA[9]*(arrayA[3]+2)+1]-varx Enter variable values file name, or hit return if no variables => etest2.txt Value of expression = 6.0 Enter the expression, or hit return to quit => Submission Submit your Expression.java source file in Autolab. As before, an ini�al grading will be done 48 hours before the Mar 11 deadline, with a report of whether your program compiled, and if it did, what score you got. Subsequently, a final grading will be done a�er the Mar 13 late submission deadline has passed. This will give a full report of your score on all test cases, and the test cases will be posted so you can verify. You can submit as many �mes as you wish, but for either grading cycle, your latest submission will be graded when your turn comes up with the grader. So if your last submission happens to be on Mar 13, it will be treated as a late submission for the final grading cycle, and graded with penalty, even if you happen to have submissions on any of the days before that. There won't be any excep�ons to this, so be aware that we will *not* entertain requests to grade an earlier submission. Frequently Asked Ques�ons Q: Are array names all uppercase? A: No. Arrays could have lower case le�ers in their names. You can tell if a variable is an array if it is followed by an opening square bracket. See, for example, the last example in the "Expression" sec�on, in which varb and varz are arrays: (varx + vary*varz[(vara+varb[(a+b)*33])])/55 Q: Can we delete spaces from the expression? A: Sure. Q: Will the expression contain nega�ve numbers? A: No. The expression will NOT have things like a*-3 or x+(-y). It will ONLY have the BINARY operators +, -, /, and *. In other words, each of these operators will need two values (operands) to work on. (The - in front of 3 in a*-3 is called a UNARY minus. UNARY operators will NOT appear in the input expression.) However, it is possible that in the process of evalua�ng the expression, you come across nega�ve values, either because they appear in the input file, or because they are the result of evalua�on. For instance, when evalua�ng a+b, a=6 and b=-9 as input values, and a result of -3 is a perfectly legi�mate scenario. Q: What if an array index evaluates to a non-integer such as 5/2? A: Truncate it and use the resul�ng integer as the index. Q: Could an array index evaluate to a nega�ve integer? A: No, you will not be given any input expression or values that would result in a nega�ve integer value for an array index. In other words, you will not need to account for this situa�on in your code. Q: What should I do on divide by zero? A: You don't need to check for this situa�on. Q: Could an array name be the same as a simple variable? A: No. All variable names, for both simple variables and arrays, are unique. Q: Should the expression "()" be reported as an error? A: You don't have to do any error checking on the legality of the expression in the makeVariableLists or evaluate methods. When these methods are called, you may assume that the expression is correctly constructed. Which means you will not encounter an expression without at least one constant or variable, and all parens and brackets will be correctly forma�ed. Q: Can I convert the expression to postfix, then evaluate the postfix expression? A: NO!!! You have to work with the given tradi�onal/infix form of the expression.

## Related Questions

Tutlance Experts offer help in a wide range of topics. Here are some of our top services:

- Math homework help
- Nursing homework help
- Statistics homework help
- Nursing coursework help
- Capstone project writing services
- Essay writers for hire
- Case study writing help
- Buy college papers online
- Buy college research papers
- College homework help
- Professional resume writing services
- Programming homework help
- Coursework writing help
- Term paper writing help
- Biology homework help
- Do my physics homework
- Dissertation data analysis help
- PhD Dissertation writing services
- Chemistry homework help

Post your project now for free and watch professional experts outbid each other in just a few minutes.