Question
int the Plus, Minus, Mult, and Divide class there is a toString method that is supposed to convert the given binary tree node into a
int the Plus, Minus, Mult, and Divide class there is a toString method that is supposed to convert the given binary tree node into a text equation, but it is just returning the reference addresses such as:
((Const@4d7e1886 * Const@3cd1a2f1) * (Const@1c20c684 + Const@1fb3ebeb)) = 560.0
how do I just print the values and not their reference?
NOTE: driver code is posted below
public class Node { public Node(){} public double eval() { System.out.println("Error: eval Node"); return 0; } } class Binop extends Node { protected Node lChild, rChild; public Binop(Node l, Node r) { lChild = l; rChild = r; } } class Plus extends Binop { public Plus(Node l, Node r) { super(l, r); } public double eval() { return lChild.eval() + rChild.eval(); } public String toString(){ String y = "("; y += lChild.toString() + " + " + rChild.toString(); y += ")"; return y; } }
class Minus extends Binop{ public Minus(Node l, Node r){ super(l,r); } public double eval(){ return lChild.eval() - rChild.eval(); }
public String toString(){ String y = "("; y += String.valueOf(rChild) + " - " + rChild; y += ")"; return y; } }
class Const extends Node { private double value;
public Const(double d) { value = d; } public double eval() { return value; } }
class Mult extends Binop{ public Mult(Node l, Node r){ super(l,r); } public double eval(){ return lChild.eval() * rChild.eval(); } public String toString(){ String y = "("; y += lChild.toString() + " * " + rChild.toString(); y += ")"; return y; } }
class Divide extends Binop{ public Divide(Node l, Node r){ super(l,r); } public double eval(){ return lChild.eval() / rChild.eval(); } public String toString(){ String y = "("; y += lChild.toString() + " / " + rChild.toString(); y += ")"; return y; } }
public static void main(String[] args){
Node n = new Mult(new Mult(new Const(1), new Const(2)), new Plus(new Const(3), new Const(4)))
}
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