I have got it working in a way so that there is one superclass, MathObject
. In this implementation there is only a Constant
subclass. I was forced to implement an abstract MathObject add(MathObject that)
and an abstract MathObject add(Constant that)
for reasons.
I would like to get that abstract MathObject add(Constant)
out of MathObject
because its already in Constant
and I would have to update MathObject
for each object I add.
I'd like to focus on maintainability and expandability first before adding too much features (this is my third or so attempt).
public class MathMain{
public static void main(String[] args){
Constant constant = new Constant(1);
constant.add(constant);
MathObject mathObject = (MathObject)constant;
constant.add(mathObject);
constant.add((Constant)mathObject);
mathObject.add(constant);
}
}
public abstract class MathObject{
public abstract MathObject add(MathObject that);
public abstract MathObject add(Constant that);
public abstract MathObject substract(MathObject that);
public abstract MathObject substract(MathObject that);
public abstract double evaluate();
}
public class Constant extends MathObject{
public final double c;
public Constant(double c){
this.c = c;
}
public MathObject add(MathObject that){
return that.add(this);
}
public MathObject substract(MathObject that){
return that.substract(this);
}
public Constant add(Constant addend){
return new Constant(c + addend.c);
}
public Constant substract(Constant subtrahend){
return new Constant(c - subtrahend.c);
}
public double evaluate(){
return c;
}
}