1

I'm trying to understand how the code in this Polymorphism exercise works.

I think I understand the concept; however, I'm having trouble understanding how to apply it to the equation: 1 + 2 * 3

abstract class Node {
    abstract double evaluate();
}

public class ValueNode extends Node {
    double value;

    double evaluate() {
        return value;
    }
}

public abstract class OpNode extends Node {
    Node left;
    Node right;

    abstract double evaluate();
}

public class MultiplicationNode extends OpNode {
    double evaluate() {
        return left.evaluate() * right.evaluate();
    }
}

public class AdditionNode extends OpNode {
    double evaluate() {
        return left.evaluate() + right.evaluate();
    }
}
crush
  • 16,713
  • 9
  • 59
  • 100
digitalized
  • 59
  • 1
  • 10
  • possible duplicate of [When do you use Java's @Override annotation and why?](http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why) – crush Feb 04 '14 at 20:11
  • im just a little confused, and this isnt realy was the point of the question mr. crush. – digitalized Feb 04 '14 at 20:11
  • Try to make your questions clear and concise. It's unclear what you are asking if it's not about `@Override` annotation. Also, providing cleanly formatted code in your question will go a long ways towards people actually reading it. – crush Feb 04 '14 at 20:12
  • The (non-abstract) classes need constructors. If those were there, you might be able to come up with the answer more easily. – ajb Feb 04 '14 at 20:21

2 Answers2

3

Note It seems to me that Node would be more appropriately defined as an interface in this context. Are you sure that it's not meant to be an interface instead of an abstract class?


You are attempting to represent the equation

1 + 2 * 3

According to your code, you have two types of Node:

  • ValueNode - represents a value
  • OpNode - represents an operation

Therefore, according to your equation, you have 5 nodes:

ValueNode = 1
AdditionNode = +
ValueNode = 2
MultiplicationNode = *
ValueNode = 3

Note that AdditionNode and MultiplicationNode are types of OpNode.

Each OpNode refers to a left Node and a right Node. It's important to understand that either of these Node's can be a ValueNode or an OpNode.

Don't forget order of operations, though. Multiplication comes before Addition.

So, when you represent this relationship as a series of nodes, you need to keep that in mind.

If you had the proper constructors on your code (you don't), you'd do the following with Dependency Injection:

Node node = new AdditionNode(
    new ValueNode(1),
    new MultiplicationNode(
        new ValueNode(2),
        new ValueNode(3)
    )
);

Now, when you call

node.evaluate();

You will get the answer to the equation.


Your constructors should look like this:

class ValueNode {
    double value;

    public ValueNode(double value) {
        this.value = value;
    }
}

class OpNode {
    Node left;
    Node right;

    public OpNode(Node left, Node right) {
        this.left = left;
        this.right = right;
    }
}

Here is a working example: http://ideone.com/oCVR8S

crush
  • 16,713
  • 9
  • 59
  • 100
0

The @Override annotations should be above any methods that are overridden from a subclass' or interface's method.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64