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.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import mattrixwv.exceptions.InvalidResult;
|
||||||
|
|
||||||
|
|
||||||
public class Algorithms{
|
public class Algorithms{
|
||||||
//This function returns a list with all the prime numbers <= goalNumber
|
//This function returns a list with all the prime numbers <= goalNumber
|
||||||
@@ -305,7 +307,7 @@ public class Algorithms{
|
|||||||
return primes;
|
return primes;
|
||||||
}
|
}
|
||||||
//This function returns all factors of goalNumber
|
//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
|
//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));
|
Double topPossiblePrime = Math.ceil(Math.sqrt(goalNumber));
|
||||||
ArrayList<Integer> primes = getPrimes(topPossiblePrime.intValue());
|
ArrayList<Integer> primes = getPrimes(topPossiblePrime.intValue());
|
||||||
@@ -331,12 +333,15 @@ public class Algorithms{
|
|||||||
goalNumber /= goalNumber;
|
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 the list of factors
|
||||||
return 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
|
//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));
|
Double topPossiblePrime = Math.ceil(Math.sqrt(goalNumber));
|
||||||
ArrayList<Long> primes = getPrimes(topPossiblePrime.longValue());
|
ArrayList<Long> primes = getPrimes(topPossiblePrime.longValue());
|
||||||
@@ -362,12 +367,15 @@ public class Algorithms{
|
|||||||
goalNumber /= goalNumber;
|
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 the list of factors
|
||||||
return 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
|
//You need to get all the primes that could be factors of this number so you can test them
|
||||||
BigInteger topPossiblePrime = goalNumber.sqrt();
|
BigInteger topPossiblePrime = goalNumber.sqrt();
|
||||||
ArrayList<BigInteger> primes = getPrimes(topPossiblePrime);
|
ArrayList<BigInteger> primes = getPrimes(topPossiblePrime);
|
||||||
@@ -393,7 +401,10 @@ public class Algorithms{
|
|||||||
goalNumber.divide(goalNumber);
|
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 the list of factors
|
||||||
return 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
|
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/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package mattrixwv;
|
package mattrixwv;
|
||||||
|
|
||||||
|
|
||||||
|
import mattrixwv.exceptions.InvalidResult;
|
||||||
|
|
||||||
|
|
||||||
public class Stopwatch{
|
public class Stopwatch{
|
||||||
private Long startTime;
|
private Long startTime;
|
||||||
private Long stopTime;
|
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
|
//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(){
|
private Long getTime(){
|
||||||
if(startTime == null){
|
if(startTime == null){
|
||||||
//TODO: This should throw an exception instead of returning 0
|
|
||||||
return 0L;
|
return 0L;
|
||||||
}
|
}
|
||||||
else if(stopTime == null){
|
else if(stopTime == null){
|
||||||
@@ -71,36 +71,36 @@ public class Stopwatch{
|
|||||||
stopTime = null;
|
stopTime = null;
|
||||||
}
|
}
|
||||||
//Returns the time in nanoseconds
|
//Returns the time in nanoseconds
|
||||||
public double getNano(){
|
public double getNano() throws InvalidResult{
|
||||||
return getTime().doubleValue();
|
return getTime().doubleValue();
|
||||||
}
|
}
|
||||||
//Returns the time in microseconds
|
//Returns the time in microseconds
|
||||||
public double getMicro(){
|
public double getMicro() throws InvalidResult{
|
||||||
return getTime().doubleValue() / 1000D;
|
return getTime().doubleValue() / 1000D;
|
||||||
}
|
}
|
||||||
//Returns the time in milliseconds
|
//Returns the time in milliseconds
|
||||||
public double getMilli(){
|
public double getMilli() throws InvalidResult{
|
||||||
return getTime().doubleValue() / 1000000D;
|
return getTime().doubleValue() / 1000000D;
|
||||||
}
|
}
|
||||||
//Returns the time in seconds
|
//Returns the time in seconds
|
||||||
public double getSecond(){
|
public double getSecond() throws InvalidResult{
|
||||||
return getTime().doubleValue() / 1000000000D;
|
return getTime().doubleValue() / 1000000000D;
|
||||||
}
|
}
|
||||||
//Returns the time in minutes
|
//Returns the time in minutes
|
||||||
public double getMinute(){
|
public double getMinute() throws InvalidResult{
|
||||||
return getTime().doubleValue() / 60000000000D;
|
return getTime().doubleValue() / 60000000000D;
|
||||||
}
|
}
|
||||||
//Returns the time in hours
|
//Returns the time in hours
|
||||||
public double getHour(){
|
public double getHour() throws InvalidResult{
|
||||||
return getTime().doubleValue() / 3600000000000D;
|
return getTime().doubleValue() / 3600000000000D;
|
||||||
}
|
}
|
||||||
//Returns the time as a string at the 'best' resolution. (Goal is xxx.xxx)
|
//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
|
//Get the current duration from time
|
||||||
return getStr(getTime().doubleValue());
|
return getStr(getTime().doubleValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getStr(double nanoseconds){
|
public static String getStr(double nanoseconds) throws InvalidResult{
|
||||||
Double duration = nanoseconds;
|
Double duration = nanoseconds;
|
||||||
//Reduce the number to the appropriate number of digits. (xxx.x).
|
//Reduce the number to the appropriate number of digits. (xxx.x).
|
||||||
//This loop works down to seconds
|
//This loop works down to seconds
|
||||||
@@ -135,7 +135,7 @@ public class Stopwatch{
|
|||||||
case MINUTE: time += " minutes"; break;
|
case MINUTE: time += " minutes"; break;
|
||||||
case HOUR: time += " hours"; break;
|
case HOUR: time += " hours"; break;
|
||||||
case ERROR:
|
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 the string
|
||||||
return time;
|
return time;
|
||||||
@@ -143,6 +143,11 @@ public class Stopwatch{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString(){
|
public String toString(){
|
||||||
return getStr();
|
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 org.junit.Test;
|
||||||
|
|
||||||
|
import mattrixwv.exceptions.InvalidResult;
|
||||||
|
|
||||||
|
|
||||||
public class TestAlgorithms{
|
public class TestAlgorithms{
|
||||||
@Test
|
@Test
|
||||||
@@ -75,7 +77,7 @@ public class TestAlgorithms{
|
|||||||
assertEquals("getNumPrimes BigInteger failed", bigCorrectAnswer, bigAnswer);
|
assertEquals("getNumPrimes BigInteger failed", bigCorrectAnswer, bigAnswer);
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void testGetFactors(){
|
public void testGetFactors() throws InvalidResult{
|
||||||
//Test 1
|
//Test 1
|
||||||
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(2, 2, 5, 5));
|
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(2, 2, 5, 5));
|
||||||
Integer number = 100;
|
Integer number = 100;
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ import static org.junit.Assert.assertNotEquals;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import mattrixwv.exceptions.InvalidResult;
|
||||||
|
|
||||||
public class TestStopwatch{
|
public class TestStopwatch{
|
||||||
private static final Integer NUM_TO_RUN = 100000;
|
private static final Integer NUM_TO_RUN = 100000;
|
||||||
private static final Double ALLOWANCE = .0000000001;
|
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
|
//If it gets to here without throwing an exception everything went well
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void testConversion(){
|
public void testConversion() throws InvalidResult{
|
||||||
Stopwatch timer = new Stopwatch();
|
Stopwatch timer = new Stopwatch();
|
||||||
Integer sum = 0;
|
Integer sum = 0;
|
||||||
//Start the timer
|
//Start the timer
|
||||||
@@ -62,7 +64,7 @@ public class TestStopwatch{
|
|||||||
assertEquals("Hour resolution test failed", timer.getHour(), (nano / 3600000000000D), ALLOWANCE);
|
assertEquals("Hour resolution test failed", timer.getHour(), (nano / 3600000000000D), ALLOWANCE);
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void testStringConversion(){
|
public void testStringConversion() throws InvalidResult{
|
||||||
//Test nanoseconds
|
//Test nanoseconds
|
||||||
String results = Stopwatch.getStr(1.0);
|
String results = Stopwatch.getStr(1.0);
|
||||||
assertEquals("1.000 nanoseconds", results);
|
assertEquals("1.000 nanoseconds", results);
|
||||||
|
|||||||
Reference in New Issue
Block a user