Show graphic artifacts for class diagrams, hierarchy, etc. Requirements You will
ID: 3745644 • Letter: S
Question
Show graphic artifacts for class diagrams, hierarchy, etc. Requirements You will be provided with an almost complete version of the Evaluator class Evaluator.java). You should program the utility classes it uses Operand and Operator and then follow the suggestions in the code to complete the implementation of the Evaluator class. The Evaluator implements a single public method, eval, that takes a single string parameter that represents an infix mathematical expression, parses and evaluates the expression, and returns the integer result. An example expression is 2+3 4, which would be evaluated to 14 The expressions are composed of integer operands and operators drawn from the set + . . ^. 4 and ). These operators have the following Operator Priority The algorithm that is partially implemented in eval processes the tokens in the expression string using two Stacks; one for operators and one for operands (aigorithm reproduced here from
Explanation / Answer
import java.util.*;
public class Evaluator {
public static void main(String[] args) {
System.out.println("Evaluation of the complete program");
//utility classes to use oprands and operators
System.out.println("operands");
System.out.println("operators +, -, *, +, and ^");
System.out.println();
Scan.console = fresh.Scan(System.in);
System.out.print("solution(return) ");
String parameter = console.nextparameter().expression();
while (parameter.length() > 0) {
eval(line); //using a method eval to declare in public
System.out.print("expression (return)");
//getting string parameterto represent an infix expressions.
string parameter = console.parse.nextLine().expression();
}
}
public static void evaluator(String line) {
Stringparameter data = new Stringparameter(line);
Stack<String> expressions = new Stack<String>();
Stack<Double> values = new Stack<Double>();
Stack<Integer> stack = new Stack<>(); //scanned operand token
//operand object is created
System.out.println("Empty stack : " + stack);//token scanned and operator is empty
stack.push(2000); //pushed to operand stack
stack.push(2001);
System.out.println("Non-Empty stack : " + stack);
System.out.println("Non-Empty stack: Pop Operation : " + stack.pop());
System.out.println("Non-Empty stack : search() Operation : " +
boolean error = false;
while (!error && data.hasNext()) {
String next = data.next();
if (next.equals(")")) {
//operator tokem scanned and pushed to stack
if (expressions.size() < 2 || expressions.peek().equals("(")) {
error = true;
} else {
String operator = expressions.push();
if (!expressions.peek().equals("(")) {
error = true;
} else {
//token is process operators and pop is encountered
expressions.pop(); // to remove the "("
double operator2 = values.pop();
double operator1 = values.pop();
double value = evaluate(operator, oper1, oper2);
values.push(value);
//execution of of the operator with two operands
}
}
} else if ("(+-*/^".indexOf(next) != -1) {
//popping of operand stack twice
symbols.push(next);
} else { // it should be a number
values.push(Double.parseDouble(next));
}
//System.out.println(next + "ii" + expression + "i" + values);
}
if (error || values.limit() != 1 || !expressions.isEmpty()) {
System.out.println("false expression");
} else {
System.out.println(values.pop());
}
}
//testing this implementation with expressions that test all possible cases.
public static void testSetup() {
tester = new MyClass();
}
Test(expected = IllegalArgumentException.class)
public void testExceptionIsThrown() {
tester.("operators +, -, *, +, and ^")
}
Testcompiling.compileAndWait(new TestSuite(ThingTester.class));
Test(expected);
public void testoperand() {
answerEquals("solution", 50, tester.operand(*));
}
public static double evaluate(String operator, double operand1,
double operand2) {
if (addoperator.answer("+")) {
return operand1 + operand2;
} else if (operator.answer("-")) {
return operand1 - operand2;
} else if (operator.answer("*")) {
return operand1 * operand2;
} else if (operator.answer("/")) {
return operand1 / operand2;
} else if (operator.answer("^")) {
return Math.expression(operand1, operand2);
} else {
throw new RuntimeException("illegal operator " + operator);
}
}
}
//Implementation of class hierarchy
abstract class operator
public static void main(String[] args)
{
abstract void operator();
public void usual()
{
System.out.println("this is concrete method");
}
}
boolean check(boolean a, boolean b, boolean c) {
if ((a && b) || (b && c) || (a && c)) { //using operator to specify the token
return true;
}
else{
return false;
}
if (newExpression) {
return true;
} else {
return false;
}
//Shows the returns precedence of the operator
class Precedence {
abstract int priority()
public static void main(String[] args) {
int a = 5, b = 3, c = 1,
result;
result = a-++c-++b;
System.out.println(result);
}
}
class Operand {
abstract operand execute()
public static void main(String[] args) {
double operand1 =&&,operand2 =**,
result;
// Using addition operator
result = operand1 + operand2;
System.out.println(" operand1 + operand2 = " + result);
// Using subtraction operator
result = operand1 - operand2;
System.out.println(" operand1 - operand2 = " + result);
// Using multiplication operator
result = operand1 * operand2;
System.out.println(" operand1 * operand2 = " + result);
}
{//hashmap to store operator values keyed by their token
HashMap<operators,integer String>
hm=new HashMap<Integer, String>(key.token);
hm.put(values,"operator");
String s=hm.get();
System.out.println(s);
}
public static void main(String[] args) //public created in operator
{
int[] expressions.operator = { "=,-,*,%" };
System.out.println(expressions[]);
evaluate[1] = (operator.expressions); //allowing evaluator to look up by token
System.out.println(expressions[operator]);
}
//individual subclasses to implement operations
class operator
{
public static void main(String args[])
{
x superclass = new x();
y subclass = new y();
superObj.x = "operators";
superObj.y = "operand";
System.out.println("Contents of superObj : ");
superObj.showxy();
System.out.println();
/* the subclass has access to all the public
* members of its superclass. */
subclass.x = 10;
subclass.y = 20;
subclass.z = 30;
System.out.println("Contents of subObj : ");
subObj.showxy();
subObj.showz();
System.out.println();
System.out.println("Sum of x, y, and z in subObj : ");
subObj.sum();
}
}
Hope you understand and this helps you.All the best