as a homework assigment, I am asked to build some sort of calculator with java. In order to make it easier to create expressions, I wanna add constructors that provide "shortcuts" for creating the Num and Var classes. For example, instead of writing new Plus(new Var("x"), new Num(5)) I would like to be able to write new Plus("x", 5) and get the same resulting expression. in order to not repeat code lines I've created an abstract class called BinaryExpression, inside it are all the possible combination I need for my constructors. I'm trying to figure out an elegant way to construct a plus class without the need to re-write the same code again.
public abstract class BinaryExpression implements Expression {
protected Expression x;
protected Expression y;
public BinaryExpression(Expression x, Expression y) {
this.x = x;
this.y = y;
}
public BinaryExpression(String x, Expression y) {
this(new Var(x),y);
}
public BinaryExpression(Double x, Expression y) {
this(new Num(x), y);
}
public BinaryExpression(Expression x, String y) {
this(x ,new Var(y));
}
public BinaryExpression(Expression x, Double y) {
this(x ,new Num(y));
}
public BinaryExpression(String x, String y) {
this(new Var(x) ,new Var(y));
}
public BinaryExpression(Double x, Double y) {
this(new Num(x) ,new Num(y));
}
public BinaryExpression(Double x ,String y){
this(new Num(x) ,new Var(y));
}
public BinaryExpression(String x ,Double y){
this(new String(x) ,new Num(y));
}
I searching for a solution like this:
public class Plus extends BinaryExpression {
public Plus(<String,Double, Expression> x, <String,Double, Expression> y) {
super(x, y);
}
so I only accept this class types that way they will fit themselves to their designated constructor inside BinaryExpression class. thanks :)
The problem is that you need some general container for objects which can be treated as if they were instances of Expression without them actually being instances of Expression.
(example: String, Double)
This answer assumes the following:
class Num implements Expression
class Var implements Expression
and that the following constructor exists, or that no constructor is defined for the class Expression
public Expression() {}
I think a good solution would be as @markspace suggested. You can make sure it compiles by using instanceof to determine what to cast the arguments to.
public Plus(Object a, Object b) {
Expression exprA = convertToExpression(a);
Expression exprB = convertToExpression(b);
if(exprA == null || exprB == null) {
// could error handle here or in the place below
}
// do initialization here
}
//
public Expression convertToExpression(Object obj) {
Expression exprObj = null;
if(obj instanceof String) {
exprObj = new Var(obj);
} else if(obj instanceof Double) {
exprObj = new Num(obj);
} else {
// error handle here or in the place above
}
return exprObj;
}
Also, is the last constructor incorrect in your code above? It seems like it should read like this:
public BinaryExpression(String x, Double y) {
this(new Var(x), new Num(y));
}