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