Added isFound function

This commit is contained in:
2019-03-17 19:01:44 -04:00
parent 906db6815f
commit caebee7e6d
3 changed files with 157 additions and 1 deletions

Binary file not shown.

View File

@@ -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<Integer> 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<Long> 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<BigInteger> 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;
}
}

View File

@@ -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<Integer> numbers = new ArrayList<Integer>(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<Integer>(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<Integer>(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<Integer>(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<Long> longNumbers = new ArrayList<Long>(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<Long>(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<Long>(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<Long>(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<BigInteger> bigNumbers = new ArrayList<BigInteger>(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<BigInteger>(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<BigInteger>(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<BigInteger>(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");
}
}
}