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