I'm building a Challenge24Solver class in Java. The logic itself works and finds the solutions as would be expected (with an arbitrary number of arguments). Anyway that part of the projects is works as I expect.
The question comes from problems with the representation of the solutions. It's fair to say that I've completed this project in Python and decided to attempt in Java as a sort of introduction, which may be the problem, that I'm trying to do this too like Python.
Here are some of my classes:
abstract class Operation { \\ Basic operation class
static String token;
abstract public double operate (double x, double y);
}
class AddOp extends Operation {
static String token = "+";
public double operate (double x, double y) {
return x+y;
}
}
//other operation classes SubOp, MulOp, DivOp
class Challenge24Step { // represents one step in a solution
Operation operator;
double x, y; //operands
double value; //the value of the step (x operator y)
public Challenge24Step (Operation operator, double x, double y) {
this.operator = operator;
// constructor code;
}
public String toString () {
return String.format("%s %s %s = %s", this.x, this.operator.token, this.y,
this.value);
}
}
The problem is that it still gets token from the Operation class: "null"
I understand that this is probably because operator is declared as Operation, but I don't understand why it doesn't go through standard inheritance: instance, class, each superclass
How would I rearrange the script so that the more specific operation classes' tokens are used?