diff --git a/mattrixwv/Algorithms.class b/mattrixwv/Algorithms.class index 56fd36d..27e8ba6 100644 Binary files a/mattrixwv/Algorithms.class and b/mattrixwv/Algorithms.class differ diff --git a/mattrixwv/Algorithms.java b/mattrixwv/Algorithms.java index 38e336a..ff1bdcc 100644 --- a/mattrixwv/Algorithms.java +++ b/mattrixwv/Algorithms.java @@ -25,7 +25,6 @@ Copyright (C) 2019 Matthew Ellison package mattrixwv; -import java.util.Arrays; import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; @@ -730,4 +729,37 @@ public class Algorithms{ //Return the product of all elements return product; } + public static Boolean isFound(ArrayList ary, Integer key){ + //Look through every element in the array, looing for the key element + for(Integer num : ary){ + //If there is an element in the array that is the same as key return true + if(num == key){ + return true; + } + } + //If you made it to the end of the array without finding a match return false because the element was not found + return false; + } + public static Boolean isLongFound(ArrayList ary, Long key){ + //Look through every element in the array, looing for the key element + for(Long num : ary){ + //If there is an element in the array that is the same as key return true + if(num == key){ + return true; + } + } + //If you made it to the end of the array without finding a match return false because the element was not found + return false; + } + public static Boolean isBigFound(ArrayList ary, BigInteger key){ + //Look through every element in the array, looing for the key element + for(BigInteger num : ary){ + //If there is an element in the array that is the same as key return true + if(num == key){ + return true; + } + } + //If you made it to the end of the array without finding a match return false because the element was not found + return false; + } } diff --git a/testAlgorithms.java b/testAlgorithms.java index f800c44..45ee1bc 100644 --- a/testAlgorithms.java +++ b/testAlgorithms.java @@ -82,6 +82,12 @@ public class testAlgorithms{ timer.stop(); System.out.println("It took " + timer.getStr() + " to run this test\n"); + //Test getProd + timer.start(); + testIsFound(); + timer.stop(); + System.out.println("It took " + timer.getStr() + " to run this test\n"); + //Print a closing message System.out.println("Tests completed"); } @@ -447,4 +453,122 @@ public class testAlgorithms{ System.out.println("getProd passed all tests"); } } + //This function tests the isFound functions + private static void testIsFound(){ + Boolean failed = false; //Holds whether a test was failed + + //Test 1 + Boolean correctAnswer = true; + ArrayList numbers = new ArrayList(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); + Boolean answer = Algorithms.isFound(numbers, 0); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the first test"); + System.out.println("compare = " + answer.compareTo(correctAnswer)); + failed = true; + } + + //Test 2 + correctAnswer = true; + numbers = new ArrayList(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); + answer = Algorithms.isFound(numbers, 9); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the second test"); + failed = true; + } + + //Test 3 + correctAnswer = true; + numbers = new ArrayList(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); + answer = Algorithms.isFound(numbers, 5); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the third test"); + failed = true; + } + + //Test 4 + correctAnswer = false; + numbers = new ArrayList(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); + answer = Algorithms.isFound(numbers, 10); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the fourth test"); + failed = true; + } + + //Test 5 + correctAnswer = true; + ArrayList longNumbers = new ArrayList(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L)); + answer = Algorithms.isLongFound(longNumbers, 0L); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the fifth test"); + failed = true; + } + + //Test 6 + correctAnswer = true; + longNumbers = new ArrayList(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L)); + answer = Algorithms.isLongFound(longNumbers, 9L); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the sixth test"); + failed = true; + } + + //Test 7 + correctAnswer = true; + longNumbers = new ArrayList(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L)); + answer = Algorithms.isLongFound(longNumbers, 5L); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the seventh test"); + failed = true; + } + + //Test 8 + correctAnswer = false; + longNumbers = new ArrayList(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L)); + answer = Algorithms.isLongFound(longNumbers, 10L); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the eighth test"); + failed = true; + } + + //Test 9 + correctAnswer = true; + ArrayList bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L))); + answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(0L)); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the nineth test"); + failed = true; + } + + //Test 10 + correctAnswer = true; + bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L))); + answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(9L)); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the tenth test"); + failed = true; + } + + //Test 11 + correctAnswer = true; + bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L))); + answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(5L)); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the eleventh test"); + failed = true; + } + + //Test 12 + correctAnswer = false; + bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L))); + answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(10L)); + if(!answer.equals(correctAnswer)){ + System.out.println("isFound failed the twelth test"); + failed = true; + } + + //Print a message if all of the tests passed + if(!failed){ + System.out.println("isFound passed all tests"); + } + } } \ No newline at end of file