Files
ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem.java

76 lines
2.2 KiB
Java

//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 06-17-20
//This is a base class for problems to use as a template
/*
Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.exceptions.InvalidResult;
public abstract class Problem{
//Variables
//Instance variables
protected final Stopwatch timer = new Stopwatch(); //To time how long it takes to run the algorithm
protected String result = null; //Holds the results of the problem
private final String description; //Holds the description of the problem
protected boolean solved; //Shows whether the problem has already been solved
//Constructor
public Problem(String description){
this.description = description;
}
//Gets
//Returns the description of the problem
public String getDescription(){
return description;
}
//Returns the result of solving the problem
public String getResult(){
if(!solved){
throw new Unsolved();
}
return result;
}
//Returns the time taken to run the problem as a string
public String getTime() throws InvalidResult{
if(!solved){
throw new Unsolved();
}
return timer.getStr();
}
//Returns the timer as a stopwatch
public Stopwatch getTimer(){
if(!solved){
throw new Unsolved();
}
return timer;
}
//Solve the problem
public abstract void solve() throws InvalidResult;
//Reset the problem so it can be run again
public void reset(){
timer.reset();
solved = false;
result = null;
}
}