Question
Hi! I wanted to make a java calculator. However, I'm a bit confused. in the if (name.equals(+)) { // TODO: your code here return toDoubleHelper(variables,
Hi!
I wanted to make a java calculator. However, I'm a bit confused. in the
if (name.equals("+")) {
// TODO: your code here
return toDoubleHelper(variables, node.getChildren().get(0)) + toDoubleHelper(variables, node.getChildren().get(1));
This should be okay since we want to add the two childrenNodes after the "+" operator node. Can somebody please help me? Below is the related methods.
/**
* Takes the given AstNode node and attempts to convert it into a double.
*
* Returns a number AstNode containing the computed double.
*
* @throws EvaluationError if any of the expressions contains an undefined variable.
* @throws EvaluationError if any of the expressions uses an unknown operation.
*/
public static AstNode toDouble(Environment env, AstNode node) {
// To help you get started, we've implemented this method for you.
// You should fill in the TODOs in the 'toDoubleHelper' method.
return new AstNode(toDoubleHelper(env.getVariables(), node));
}
private static double toDoubleHelper(IDictionary
// There are three types of nodes, so we have three cases.
if (node.isNumber()) {
// TODO: your code here
return node.getNumericValue();
} else if (node.isVariable()) {
if (!variables.containsKey(node.getName())) {
// If the expression contains an undefined variable, we give up.
throw new EvaluationError("Undefined variable: " + node.getName());
}
// TODO: your code here
return (variables.get(node.getName())).getNumericValue();
} else {
String name = node.getName();
// TODO: your code here
if (name.equals("+")) {
// TODO: your code here
return toDoubleHelper(variables, node.getChildren().get(0)) + toDoubleHelper(variables, node.getChildren().get(1));
} else if (name.equals("-")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("*")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("/")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("^")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("negate")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("sin")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("cos")) {
// TODO: your code here
throw new NotYetImplementedException();
} else {
throw new EvaluationError("Unknown operation: " + name);
}
}
}
public static AstNode simplify(Environment env, AstNode node) {
// Try writing this one on your own!
// Hint 1: Your code will likely be structured roughly similarly
// to your "toDouble" method
// Hint 2: When you're implementing constant folding, you may want
// to call your "toDouble" method in some way
// TODO: Your code here
return new AstNode(simplifyHelper(env.getVariables(), node));
}
private static double simplifyHelper(IDictionary
// There are three types of nodes, so we have three cases.
if (node.isNumber()) {
// TODO: your code here
return node.getNumericValue();
} else if (node.isVariable()) {
if (!variables.containsKey(node.getName())) {
// If the expression contains an undefined variable, we give up.
throw new EvaluationError("Undefined variable: " + node.getName());
}
// TODO: your code here
return (variables.get(node.getName())).getNumericValue();
} else {
String name = node.getName();
// TODO: your code here
if (name.equals("+")) {
// TODO: your code here
return simplifyHelper(variables, node.getChildren().get(0)) + simplifyHelper(variables, node.getChildren().get(1));
} else if (name.equals("-")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("*")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("/")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("^")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("negate")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("sin")) {
// TODO: your code here
throw new NotYetImplementedException();
} else if (name.equals("cos")) {
// TODO: your code here
throw new NotYetImplementedException();
} else {
throw new EvaluationError("Unknown operation: " + name);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started