From caebee7e6d01d2cd2f08ea94bda0afa7440daa74 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Sun, 17 Mar 2019 19:01:44 -0400 Subject: [PATCH] Added isFound function --- mattrixwv/Algorithms.class | Bin 7990 -> 8840 bytes mattrixwv/Algorithms.java | 34 +++++++++- testAlgorithms.java | 124 +++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-) diff --git a/mattrixwv/Algorithms.class b/mattrixwv/Algorithms.class index 56fd36dcdb6f59b32759d1148e9192011bf0208f..27e8ba61907f33c10a3cdc881cfc53216da60723 100644 GIT binary patch delta 2349 zcma)72~d?)7(L&8j~+gLMcE%c2;2}1$ptA9*B~Pwr4^bhCO@`B?izXiQPq;o~?;X1sODrtKG7F2*-@)nDs2vs*1ZfCr7*Lq7d=4sc>*syrBwC;K-REf!UvxlA zbV4v(iIgc|xl>qZF*Xq0LVhC| z%_!Hm`u*yeMa4=g&ZgQNgkwHaUc@cE1pTp$KFi@nEf3EMYOKOkrc!~mRNh49W};g; zyNJE4=l?cc)!L<-ts|ZWsy<59Mg(97g0K@2*o_!GL*+fV1$!|H`;d+2nC*)!e?KPU z0Ly=!nH|DByg|*~^gT-TV@*d}`ibW~O1@9Yk68N0O!hOD{ssH@C2qoLHsB09cNXP1 zhgmp}YJ7u*_?Bh-zz+V%9dVJVet{jLuv2U--KEp~&v~wbuQEL}R{m#8QdDYKg-J@*j}|JSNw%{KN5yu%stKB?;F^ zFLajP=q)Komp)V*h;cFqUbz{?l8!PNqRZR78-FMKC6nI6&{jsEi;TjJG8#i=4Dw|> z{qrzQyr__TER-p_DqvrHDSV_1PML-PnSn@|iA1TuK$(R*WDZ714YFkca%CYVNv$pj zd^cqk>s-yu)}WQFN1$wAcK4%`Y(ZCf2q{vBTV)%D%flEW+nHelJ?mKO6R449T^)2P z0trY%( z@&Q+Wh+z2yQF0vD%ID1dB>Q~|{p2)rI7|O?$dRv6Am`bk)0iz6Fi*bMyF&IvTtv76 zB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ff --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