Implementing Linear Search

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Sounds like a counting loop.

Implementing Linear Search

Let us create a class Searcher that contains a linear search method. We will make the linear search method a static method. This will enable us to use the method without creating a Searcher object.

Here is a skeleton of the program:

class Searcher
{
  // seek target in the array of strings.
  // return the index where found, or -1 if not found.

  public static int search(  array,  target )
  {

    . . . . . . // implement linear search

  }
}

class SearchTester
{
  public static void main ( String[] args )
  {
    final int theSize = 20 ;
    String[] strArray = new String[ theSize ] ;  

    . . . . . . // put values into strArray

     // call the static search method
    int where = Searcher.search( strArray, "Peoria" );
    if ( where >= 0 )
      System.out.println("Target found in cell " + where );
    else
      System.out.println("Target not found" );

  }
}

QUESTION 16:

Fill in the blanks for the types of the formal parameters.