//ArraySearcher.java

import java.util.Scanner; //added

/**
 * Class for searching an already sorted array of integers.
*/
public class ArraySearcher
{
    private Scanner keyboard =  new Scanner(System.in); //added
    private int[] a;
    
    /**
     Precondition: theArray is full and is sorted
     from lowest to highest.
    */
    public ArraySearcher
    (
        int[] theArray //in
    )
    {
        a = theArray; //a is now another name for theArray.
    }
    
    /**
     If target is in the array, returns the index of an occurrence
     of target. Returns -1 if target is not in the array.
    */
    public int find
    (
        int target //in
    )
    {
        return binarySearch(target, 0, a.length - 1);
    }
    
    //Uses binary search to search for target in a[first] through
    //a[last] inclusive. Returns the index of target if target
    //is found. Returns -1 if target is not found. 
    private int binarySearch
    (
        int target, //in
        int first,  //in
        int last    //in
    )
    {
        int result;
        if (first > last)
            result = -1;
        else
        {
            int mid = (first + last) / 2;
            System.out.println("Now looking at value " + a[mid]); //added
            System.out.print("Press Enter to continue ... ");     //added
            keyboard.nextLine();                                  //added
            if (target == a[mid]) 
                result = mid; 
            else if (target < a[mid])
                result = binarySearch(target, first, mid - 1);
            else //(target > a[mid])
                result = binarySearch(target, mid + 1, last);
        }
        return result;
    }
}
