Sunday, September 26, 2010

Assignment 7 - LinearSearchTest

Assignment 7 - LinearSearchTest

















/*
LinearSearchTest
*/

import java.util.Scanner;

public class LinearSearchTest
{
public static void main( String args[] )
{
//create scanner object to put data
Scanner input = new Scanner( System.in );

int searchInt; //search key
int position; //location of search key in array

//create array and output int
LinearArray searchArray = new LinearArray( 10 );
System.out.println( searchArray ); //print array

//get input from user
System.out.print( "Please enter an integer value (-1 to quit): " );
searchInt = input.nextInt(); //read first int from user

//repeatedly input an interger: -1 terminates the program
while( searchInt != -1 )
{
//preform linear search
position = searchArray.linearSearch( searchInt );

if(position == -1) //integer was not found
System.out.println( "The integer " + searchInt +" was not found.\n" );

//get input from user
System.out.print( "Please enter an integer value (-1 to quit):" );
searchInt = input.nextInt(); //read next int from user
}
}
}

No comments:

Post a Comment