import javabook.*;

/*
 * Introduction to OOP with Java 2nd Edition, McGraw-Hill
 *	
 *  A sample program to test the linear search routine
 *  in the SearchRoutines class.
 *  
 * @author Dr. Caffeine
 *
 */
class TestLinearSearch
{
    
    private final static int EMPTY = -1;
    
    public static void main (String[] args)
    {
        MainWindow  mainWindow;
        OutputBox 	outputBox;
        
        mainWindow = new MainWindow("Test Linear Search");
        outputBox  = new OutputBox( mainWindow );
                
        mainWindow.setVisible( true );
        outputBox.setVisible( true );
        
        int[ ] number = new int[1000];
        
        Search mySearch = new Search( );
        
        int    position;
        
             
        //Initialize the array elements to empty
        for (int i = 0; i < number.length; i++ ) {
            number[i] = EMPTY;
            
        }
        
        //Store numbers from 0 to 999
        //into an array of int at a random position
        int num = 0, loc;
        
        while (num < number.length) {
            
            //Generate a random number between 0 and number.length-1
            //and use this value as the index of the position to
            //store the number
            
            loc   = (int) Math.floor( Math.random() * number.length );
            
            while ( number[loc] != EMPTY ) {
                //loc already has a number in it
                //generate another location
                
                loc = (int) Math.floor( Math.random() * number.length );
            }
            
            number[loc] = num;
            
            num++;
        }
   
        
        //Test linear search
        position = mySearch.linearSearch( number, 435 );
        
        outputBox.printLine( "Search 435; Index returned:   " + position );
        
        position = mySearch.linearSearch( number, 23 );
        
        outputBox.printLine( "Search 23; Index returned:   " + position );

        position = mySearch.linearSearch( number, 98765 );
        
        outputBox.printLine( "Search 98765; Index returned: " + position );
 
    }
    
}