Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 variables, AstNode node) {

// 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 variables, AstNode node) {

// 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);

}

}

}

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
T-Mobile LTE 9:21 AM 100% 13 +2 7 2 17 4 >>> x + y >>> x := 1 10 >>xy 11 1 +y 12 13 | >>> y := 2 14 2 15 16>>> xy 17 3 The users type input into the lines that start with . The line that follows immediately after is the output This example demonstrates the two core features we can perform using this calculator: evaluate math expressions (line 1) and symbolically work with variables (lines 7 17) When we first typedin x y , we were unable to evaluate or simplify the expressions because the variables x and y are currently unknown

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_2

Step: 3

blur-text-image_3

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

Big Data With Hadoop MapReduce A Classroom Approach

Authors: Rathinaraja Jeyaraj ,Ganeshkumar Pugalendhi ,Anand Paul

1st Edition

1774634848, 978-1774634844

More Books

Students also viewed these Databases questions