Say I want to solve a bunch of Project Euler problems in Java, and rather than give each problem class the boilerplate of its own main
method, inherit it and have a "solve" method instead.
Is there a way of getting the problem class's name to print out along with the solution?
What I have:
abstract class Problem {
private static String problemName = ???
public static void main(String[] args) {
// If I could summon an instance of the sub-class then it would be easy
// Problem p = new ... what?
System.out.println(problemName + ": " + solve());
}
abstract static long solve();
// oops, you can't do abstract static in Java :(
}
then
public class Euler_001 extends Problem {
static long solve() {...}
}
The problem is that the various hacks to get the class name given in this answer return "Problem", not "Euler_001".
I guess I have to instantiate the problem class, but then how do I do that?