Created
July 12, 2012 10:25
-
-
Save fffej/3097258 to your computer and use it in GitHub Desktop.
Expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Expression { | |
int evaluate(); | |
} | |
public class Constant implements Expression { | |
private final int value; | |
Constant(int value) { this.value = value; } | |
public int evaluate() { return value; } | |
} | |
public abstract class BinaryExpression implements Expression { | |
private final Expression lhs; | |
private final Expression rhs; | |
BinaryExpression(Expression lhs, Expression rhs) { | |
this.lhs = lhs; | |
this.rhs = rhs; | |
} | |
public int evaluate() { | |
return op(lhs.evaluate(),rhs.evaluate()); | |
} | |
protected abstract int op(int lhs, int rhs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment