mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Added exception handling to match library
This commit is contained in:
@@ -28,6 +28,7 @@ import java.util.Scanner;
|
||||
|
||||
import mattrixwv.Stopwatch;
|
||||
import mattrixwv.ProjectEuler.Problems.Problem;
|
||||
import mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
|
||||
public class Benchmark{
|
||||
@@ -35,7 +36,7 @@ public class Benchmark{
|
||||
private static enum BenchmarkOptions{runSpecific, runAllShort, runAll, exit, size};
|
||||
private static final ArrayList<Integer> tooLong = new ArrayList<Integer>(Arrays.asList(15, 23, 24, 27));
|
||||
//The driver function for the benchmark selection
|
||||
public static void benchmarkMenu(){
|
||||
public static void benchmarkMenu() throws InvalidResult{
|
||||
BenchmarkOptions selection = BenchmarkOptions.size;
|
||||
|
||||
printMenu();
|
||||
@@ -91,7 +92,7 @@ public class Benchmark{
|
||||
return sel;
|
||||
}
|
||||
//Determines which problem user wants to run and runs it
|
||||
private static void runSpecific(){
|
||||
private static void runSpecific() throws InvalidResult{
|
||||
//Ask which problem the user wants to run
|
||||
int problemNumber = ProblemSelection.getProblemNumber();
|
||||
//Ask how many times to run the problem
|
||||
@@ -108,7 +109,7 @@ public class Benchmark{
|
||||
System.out.println(getBenchmarkResults(problem, totalTime, timesToRun));
|
||||
}
|
||||
//Runs all problems except a few that are specified because of run length
|
||||
private static void runAllShort(){
|
||||
private static void runAllShort() throws InvalidResult{
|
||||
//Ask how many times to run the problems
|
||||
int timesToRun = getNumberOfTimesToRun();
|
||||
|
||||
@@ -133,7 +134,7 @@ public class Benchmark{
|
||||
}
|
||||
}
|
||||
//Runs all problems
|
||||
private static void runAll(){
|
||||
private static void runAll() throws InvalidResult{
|
||||
//Ask how many times to run the problem
|
||||
int timesToRun = getNumberOfTimesToRun();
|
||||
|
||||
@@ -165,7 +166,7 @@ public class Benchmark{
|
||||
return numOfTimesToRun;
|
||||
}
|
||||
//Runs the problem the given number of times
|
||||
private static double runProblem(Problem problem, int timesToRun){
|
||||
private static double runProblem(Problem problem, int timesToRun) throws InvalidResult{
|
||||
double totalTime = 0;
|
||||
System.out.print("Solving");
|
||||
for(int cnt = 0;cnt < timesToRun;++cnt){
|
||||
@@ -180,7 +181,7 @@ public class Benchmark{
|
||||
return totalTime;
|
||||
}
|
||||
//Prints the benchmark results of a problem
|
||||
private static String getBenchmarkResults(Problem problem, double totalTime, int timesRun){
|
||||
private static String getBenchmarkResults(Problem problem, double totalTime, int timesRun) throws InvalidResult{
|
||||
//Calculate the average run time of the problem
|
||||
totalTime /= timesRun;
|
||||
String timeResults = Stopwatch.getStr(totalTime);
|
||||
|
||||
@@ -24,6 +24,8 @@ package mattrixwv.ProjectEuler;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
|
||||
public class Driver{
|
||||
//An enum to hold the possible menu selections
|
||||
@@ -31,7 +33,7 @@ public class Driver{
|
||||
private static final Scanner input = new Scanner(System.in);
|
||||
|
||||
//Drives the program
|
||||
public static void main(String[] args){
|
||||
public static void main(String[] args) throws InvalidResult{
|
||||
SELECTIONS selection = SELECTIONS.SIZE; //Holds the menu selection of the user
|
||||
do{
|
||||
//Print the menu and prompt the user to select an action
|
||||
@@ -97,7 +99,7 @@ public class Driver{
|
||||
System.out.println("That is an invalid selection!");
|
||||
}
|
||||
//Handle what happens when a user wants to solve a problem
|
||||
private static void solveMenu(){
|
||||
private static void solveMenu() throws InvalidResult{
|
||||
Integer problemNumber = ProblemSelection.getProblemNumber();
|
||||
//This selection solves all problems in order
|
||||
if(problemNumber.equals(0)){
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
import mattrixwv.ProjectEuler.Problems.*;
|
||||
import mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
|
||||
public class ProblemSelection{
|
||||
@@ -85,7 +86,7 @@ public class ProblemSelection{
|
||||
System.out.println(problem.getDescription());
|
||||
}
|
||||
//Solve a problem
|
||||
public static void solveProblem(Integer problemNumber){
|
||||
public static void solveProblem(Integer problemNumber) throws InvalidResult{
|
||||
//Get the problem
|
||||
Problem problem = getProblem(problemNumber);
|
||||
//Print the problem description
|
||||
|
||||
@@ -23,6 +23,7 @@ package mattrixwv.ProjectEuler.Problems;
|
||||
|
||||
import mattrixwv.Stopwatch;
|
||||
import mattrixwv.ProjectEuler.Unsolved;
|
||||
import mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
public abstract class Problem{
|
||||
//Variables
|
||||
@@ -50,7 +51,7 @@ public abstract class Problem{
|
||||
return result;
|
||||
}
|
||||
//Returns the time taken to run the problem as a string
|
||||
public String getTime(){
|
||||
public String getTime() throws InvalidResult{
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
@@ -64,7 +65,7 @@ public abstract class Problem{
|
||||
return timer;
|
||||
}
|
||||
//Solve the problem
|
||||
public abstract void solve();
|
||||
public abstract void solve() throws InvalidResult;
|
||||
//Reset the problem so it can be run again
|
||||
public void reset(){
|
||||
timer.reset();
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.ArrayList;
|
||||
|
||||
import mattrixwv.Algorithms;
|
||||
import mattrixwv.ProjectEuler.Unsolved;
|
||||
import mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
|
||||
public class Problem3 extends Problem{
|
||||
@@ -44,7 +45,7 @@ public class Problem3 extends Problem{
|
||||
}
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
public void solve(){
|
||||
public void solve() throws InvalidResult{
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user