Finish the class Evaluation that implements Visitor pattern . The class takes th
ID: 3866769 • Letter: F
Question
Finish the class Evaluation that implements Visitor pattern. The class takes the value of arguments as a Map, and evaluates an expression containing variables -- substituting the value of the variable as needed.
Given the map [ ‘x’:12, ‘y’:5] and the expression
2 * x + x / y
The new visitor would evaluate the expression and return the value 26.4
public class Evaluation extends Eval implements IVisitor {
private Map map;
public EvalWithVars(Map map) {
this.map = map;
}
@Override
public Object visit(Variable node) {
// complete this method
return 0.0;
}
}
Explanation / Answer
In order to complete, the Variable java class definition is needed. Since the question does not give the code for class Variable, I presume there is a method to return the variable's name such as getName() inside Variable class. I have used getName() to access the variable's name. If there is no such method, please use its equivalent.
Please post the code for the Variable class as a comment if you want to me to help change this code.
public class Evaluation extends Eval implements IVisitor {
private Map map;
public EvalWithVars(Map map) {
this.map = map;
}
@Override
public Object visit(Variable node) {
// complete this method
Object val = map.get(node.getName());
return val;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.