mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-06 23:13:57 -05:00
Added some exception handling
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -19,11 +19,12 @@ Copyright (C) 2020 Matthew Ellison
|
||||
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;
|
||||
|
||||
|
||||
import mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
|
||||
public class Stopwatch{
|
||||
private Long startTime;
|
||||
private Long stopTime;
|
||||
@@ -36,7 +37,6 @@ public class Stopwatch{
|
||||
//Returns a long with the elapsed time in nanoseconds. Used by other functions to get the time before converting it to the correct resolution
|
||||
private Long getTime(){
|
||||
if(startTime == null){
|
||||
//TODO: This should throw an exception instead of returning 0
|
||||
return 0L;
|
||||
}
|
||||
else if(stopTime == null){
|
||||
@@ -71,36 +71,36 @@ public class Stopwatch{
|
||||
stopTime = null;
|
||||
}
|
||||
//Returns the time in nanoseconds
|
||||
public double getNano(){
|
||||
public double getNano() throws InvalidResult{
|
||||
return getTime().doubleValue();
|
||||
}
|
||||
//Returns the time in microseconds
|
||||
public double getMicro(){
|
||||
public double getMicro() throws InvalidResult{
|
||||
return getTime().doubleValue() / 1000D;
|
||||
}
|
||||
//Returns the time in milliseconds
|
||||
public double getMilli(){
|
||||
public double getMilli() throws InvalidResult{
|
||||
return getTime().doubleValue() / 1000000D;
|
||||
}
|
||||
//Returns the time in seconds
|
||||
public double getSecond(){
|
||||
public double getSecond() throws InvalidResult{
|
||||
return getTime().doubleValue() / 1000000000D;
|
||||
}
|
||||
//Returns the time in minutes
|
||||
public double getMinute(){
|
||||
public double getMinute() throws InvalidResult{
|
||||
return getTime().doubleValue() / 60000000000D;
|
||||
}
|
||||
//Returns the time in hours
|
||||
public double getHour(){
|
||||
public double getHour() throws InvalidResult{
|
||||
return getTime().doubleValue() / 3600000000000D;
|
||||
}
|
||||
//Returns the time as a string at the 'best' resolution. (Goal is xxx.xxx)
|
||||
public String getStr(){
|
||||
public String getStr() throws InvalidResult{
|
||||
//Get the current duration from time
|
||||
return getStr(getTime().doubleValue());
|
||||
}
|
||||
|
||||
public static String getStr(double nanoseconds){
|
||||
public static String getStr(double nanoseconds) throws InvalidResult{
|
||||
Double duration = nanoseconds;
|
||||
//Reduce the number to the appropriate number of digits. (xxx.x).
|
||||
//This loop works down to seconds
|
||||
@@ -135,7 +135,7 @@ public class Stopwatch{
|
||||
case MINUTE: time += " minutes"; break;
|
||||
case HOUR: time += " hours"; break;
|
||||
case ERROR:
|
||||
default: time = "There was an error computing the time"; break; //TODO: This should throw an error instead
|
||||
default: throw new InvalidResult("timeResolution was invalid");
|
||||
}
|
||||
//Return the string
|
||||
return time;
|
||||
@@ -143,6 +143,11 @@ public class Stopwatch{
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
try{
|
||||
return getStr();
|
||||
}
|
||||
catch(InvalidResult error){
|
||||
return "There was an error in getStr(): " + error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
17
src/main/java/mattrixwv/exceptions/InvalidResult.java
Normal file
17
src/main/java/mattrixwv/exceptions/InvalidResult.java
Normal file
@@ -0,0 +1,17 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/Exceptions/InvalidResult.java
|
||||
//Matthew Ellison
|
||||
// Created: 08-24-20
|
||||
//Modified: 08-24-20
|
||||
//This is an exception for an invalid result out of one of my algorithms
|
||||
package mattrixwv.exceptions;
|
||||
|
||||
|
||||
public class InvalidResult extends Exception{
|
||||
private static final long serialVersionUID = 1L;
|
||||
public InvalidResult(){
|
||||
super();
|
||||
}
|
||||
public InvalidResult(String msg){
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,8 @@ import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
|
||||
public class TestAlgorithms{
|
||||
@Test
|
||||
@@ -75,7 +77,7 @@ public class TestAlgorithms{
|
||||
assertEquals("getNumPrimes BigInteger failed", bigCorrectAnswer, bigAnswer);
|
||||
}
|
||||
@Test
|
||||
public void testGetFactors(){
|
||||
public void testGetFactors() throws InvalidResult{
|
||||
//Test 1
|
||||
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(2, 2, 5, 5));
|
||||
Integer number = 100;
|
||||
|
||||
@@ -29,6 +29,8 @@ import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
public class TestStopwatch{
|
||||
private static final Integer NUM_TO_RUN = 100000;
|
||||
private static final Double ALLOWANCE = .0000000001;
|
||||
@@ -40,7 +42,7 @@ public class TestStopwatch{
|
||||
//If it gets to here without throwing an exception everything went well
|
||||
}
|
||||
@Test
|
||||
public void testConversion(){
|
||||
public void testConversion() throws InvalidResult{
|
||||
Stopwatch timer = new Stopwatch();
|
||||
Integer sum = 0;
|
||||
//Start the timer
|
||||
@@ -62,7 +64,7 @@ public class TestStopwatch{
|
||||
assertEquals("Hour resolution test failed", timer.getHour(), (nano / 3600000000000D), ALLOWANCE);
|
||||
}
|
||||
@Test
|
||||
public void testStringConversion(){
|
||||
public void testStringConversion() throws InvalidResult{
|
||||
//Test nanoseconds
|
||||
String results = Stopwatch.getStr(1.0);
|
||||
assertEquals("1.000 nanoseconds", results);
|
||||
|
||||
Reference in New Issue
Block a user