Added some exception handling

This commit is contained in:
2020-08-24 14:50:04 -04:00
parent 019ed3244b
commit bf97b78219
5 changed files with 59 additions and 22 deletions

View File

@@ -30,6 +30,8 @@ import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collections;
import mattrixwv.exceptions.InvalidResult;
public class Algorithms{
//This function returns a list with all the prime numbers <= goalNumber
@@ -305,7 +307,7 @@ public class Algorithms{
return primes;
}
//This function returns all factors of goalNumber
public static ArrayList<Integer> getFactors(Integer goalNumber){
public static ArrayList<Integer> getFactors(Integer goalNumber) throws InvalidResult{
//You need to get all the primes that could be factors of this number so you can test them
Double topPossiblePrime = Math.ceil(Math.sqrt(goalNumber));
ArrayList<Integer> primes = getPrimes(topPossiblePrime.intValue());
@@ -331,12 +333,15 @@ public class Algorithms{
goalNumber /= goalNumber;
}
//TODO: If for some reason the goalNumber is not 1 throw an error
//If for some reason the goalNumber is not 1 throw an error
if(goalNumber != 1){
throw new InvalidResult("The factor was not 1: " + goalNumber);
}
//Return the list of factors
return factors;
}
public static ArrayList<Long> getFactors(Long goalNumber){
public static ArrayList<Long> getFactors(Long goalNumber) throws InvalidResult{
//You need to get all the primes that could be factors of this number so you can test them
Double topPossiblePrime = Math.ceil(Math.sqrt(goalNumber));
ArrayList<Long> primes = getPrimes(topPossiblePrime.longValue());
@@ -362,12 +367,15 @@ public class Algorithms{
goalNumber /= goalNumber;
}
//TODO: If for some reason the goalNumber is not 1 throw an error
//If for some reason the goalNumber is not 1 throw an error
if(goalNumber != 1){
throw new InvalidResult("The factor was not 1: " + goalNumber);
}
//Return the list of factors
return factors;
}
public static ArrayList<BigInteger> getFactors(BigInteger goalNumber){
public static ArrayList<BigInteger> getFactors(BigInteger goalNumber) throws InvalidResult{
//You need to get all the primes that could be factors of this number so you can test them
BigInteger topPossiblePrime = goalNumber.sqrt();
ArrayList<BigInteger> primes = getPrimes(topPossiblePrime);
@@ -393,7 +401,10 @@ public class Algorithms{
goalNumber.divide(goalNumber);
}
//TODO: If for some reason the goalNumber is not 1 throw an error
//If for some reason the goalNumber is not 1 throw an error
if(!goalNumber.equals(BigInteger.ONE)){
throw new InvalidResult("The factor was not 1: " + goalNumber);
}
//Return the list of factors
return factors;