/**
* Search Project
*
* The SearchIllustrationMain
class provides a main method
* for a program that illustrates linear and binary searching.
*
* @author Alyce Brady
* @version 11 February 2016
*/
public class SearchIllustrationMain
{
/**
* The main function initiates execution of this program.
* @param String[] args not used in this program
**/
public static void main(String[] args)
{
System.out.println ("Welcome to the Search Illustration Project.");
// Call another static method to populate an array of integers through which
// to search.
int[] numberList = createArrayOfNumbers();
// Now run some tests with the Int Searcher.
runIntSearchTests(numberList);
// Call another static method to populate an array of strings through which
// to search, then run some tests with the String Searcher.
String[] stringList = createArrayOfStrings();
runStringSearchTests(stringList);
System.out.println ("Program done.");
}//end main
/**
* Creates an array of integers. The array is completely full (i.e.,
* the length attribute can be used to step through the array), and
* the numbers in the array are in sorted order.
*/
private static int[] createArrayOfNumbers()
{
int[] listToReturn = new int[20];
listToReturn[0] = 1;
listToReturn[1] = 4;
listToReturn[2] = 16;
listToReturn[3] = 27;
listToReturn[4] = 29;
listToReturn[5] = 43;
listToReturn[6] = 54;
listToReturn[7] = 60;
listToReturn[8] = 63;
listToReturn[9] = 88;
listToReturn[10] = 101;
listToReturn[11] = 104;
listToReturn[12] = 116;
listToReturn[13] = 127;
listToReturn[14] = 229;
listToReturn[15] = 343;
listToReturn[16] = 554;
listToReturn[17] = 760;
listToReturn[18] = 863;
listToReturn[19] = 988;
return listToReturn;
}
/**
* Runs a set of search tests on an array of ints.
*/
private static void runIntSearchTests(int[] numberList)
{
int firstNumber = 43;
int secondNumber = 125;
IntLinearSearcher searcher = new IntLinearSearcher();
System.out.println("numberList contains " + firstNumber + "? " +
searcher.contains(numberList, firstNumber));
System.out.println("numberList contains " + secondNumber + "? " +
searcher.contains(numberList, secondNumber));
System.out.println("The index of " + firstNumber + " is " +
searcher.indexOf(numberList, firstNumber));
System.out.println("The index of " + secondNumber + " is " +
searcher.indexOf(numberList, secondNumber));
//
// System.out.println("numberList contains " + firstNumber + "? " +
// searcher.altContains(numberList, firstNumber));
//
// System.out.println("numberList contains " + secondNumber + "? " +
// searcher.altContains(numberList, secondNumber));
}
/**
* Creates an array of strings. The array is completely full (i.e.,
* the length attribute can be used to step through the array), and
* the numbers in the array are in sorted order.
*/
private static String[] createArrayOfStrings()
{
String[] listToReturn = new String[42];
listToReturn[0] = "Alex";
listToReturn[1] = "Andrew";
listToReturn[2] = "Aunic";
listToReturn[3] = "Austen";
listToReturn[4] = "Azra";
listToReturn[5] = "Bart";
listToReturn[6] = "Braeden";
listToReturn[7] = "Brandon D.";
listToReturn[8] = "Brandon S.";
listToReturn[9] = "Dan";
listToReturn[10] = "Danny";
listToReturn[11] = "David";
listToReturn[12] = "Delfino";
listToReturn[13] = "Elena";
listToReturn[14] = "Emerson";
listToReturn[15] = "Eugina";
listToReturn[16] = "Evan";
listToReturn[17] = "Freeman";
listToReturn[18] = "Haroon";
listToReturn[19] = "Jack";
listToReturn[20] = "Jake";
listToReturn[21] = "Jennifer";
listToReturn[22] = "Jon";
listToReturn[23] = "Keenan";
listToReturn[24] = "Kio";
listToReturn[25] = "Koji";
listToReturn[26] = "Lionel";
listToReturn[27] = "Matt";
listToReturn[28] = "Michael";
listToReturn[29] = "Michelle";
listToReturn[30] = "Natalia";
listToReturn[31] = "Peter";
listToReturn[32] = "Sarah";
listToReturn[33] = "Scott";
listToReturn[34] = "Sharif";
listToReturn[35] = "Sivhaun";
listToReturn[36] = "Tanush";
listToReturn[37] = "Terence";
listToReturn[38] = "Tim";
listToReturn[39] = "Tomasso";
listToReturn[40] = "Xiang";
listToReturn[41] = "Zach";
return listToReturn;
}
/**
* Runs a set of search tests on an array of strings.
*/
private static void runStringSearchTests(String[] stringList)
{
String firstString = "Sarah";
String secondString = "Alyce";
StringLinSearcher searcher = new StringLinSearcher();
System.out.println("stringList contains " + firstString + "? " +
searcher.contains(stringList, firstString));
System.out.println("stringList contains " + secondString + "? " +
searcher.contains(stringList, secondString));
System.out.println("The index of " + firstString + " is " +
searcher.indexOf(stringList, firstString));
System.out.println("The index of " + secondString + " is " +
searcher.indexOf(stringList, secondString));
}
}//end class