Your task is to complete the \'LinearSearch\' function, which has been outlined
ID: 3739434 • Letter: Y
Question
Your task is to complete the 'LinearSearch' function, which has been outlined in the form of pseudocode, in the provided test framework. Upon completing the LinearSearch function you should get output that looks like this (after pressing enter enough times) Welcome to Linear Search Demonstration The array contains the following 9 words: the quick brown jumps over the lazy dog the word "the" found in position in the list Press any key to continue: the word "able" not found in the list Press any key to continue: the word "over" found in position 5 in the list Press any key to continue: the word "zebra not found in the list Press any key to continue: the word "dog" found in position 8 in the list Press any key to continue: Press any key to exit progran: _ Implement the LinearSearch function as defined by the specification. It should take two arguments: an array of strings and a string to search for and return the position of that string in the array or -1 if the string was not found in the array If the array contains multiple copies of the search string, you should return the first position the string appears in. Note that the Main() function of the provided code is never called- only the Linearsearch() function is called in order to test your program. As a result, there is no problem is the program uses ReadKey (). However, it also means your LinearSearch() method must be public Sample codeExplanation / Answer
public static int LinearSearch(String[] words, String word) {
int position = 0;
//equals method is used to compare string in java. ex : string1.equals(string2).
while ((position < words.length) && !(word.equals(words[position])))
position++; //incrementing
if (position < words.length)
return position; // found it
else
return -1; // key not in the list
// following return provided so that code compiles
//return position; // it can be deleted once above algorithm is completed
} //end LinearSearch
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.