Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi! I wanted to make a java calculator. However, I\'m a bit confused. in the if

ID: 3588059 • Letter: H

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, 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<String, AstNode> 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<String, AstNode> 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);

}

}

}

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

Explanation / Answer

struct LinkedNode
{
Type info;
LinkedNode *next;
};




template
class LinkedList
{
public:
LinkedList();
LinkedList(const LinkedList& other);
~LinkedList();


LinkedList& operator=(const LinkedList& other);


int insert(const Type&);
Type* find(int) const;
Type* get(int) const;
int remove(int);
void clear();
int size() const;
void print() const;
};


template
LinkedList::LinkedList()
{

}


template
LinkedList::LinkedList(const LinkedList& other)
{
}


template
LinkedList::~LinkedList()
{
}


template
LinkedList& LinkedList::operator=(const LinkedList& other)
{
return

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote