Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step: 3

blur-text-image

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

The World Wide Web And Databases International Workshop Webdb 98 Valencia Spain March 27 28 1998 Selected Papers Lncs 1590

Authors: Paolo Atzeni ,Alberto Mendelzon ,Giansalvatore Mecca

1st Edition

3540658904, 978-3540658900

More Books

Students also viewed these Databases questions