import javabook.*;

/*
 * Introduction to OOP with Java 2nd Edition, McGraw-Hill
 *	
 *  The class for testing the array passing routines 
 *  introduced in Section 9.3.
 *
 *  
 * @author Dr. Caffeine
 *
 */
class TestArrayParameter
{
    private    MainWindow    mainWindow;
    private    OutputBox     outputBox;
    private    InputBox      inputBox;

    
    public TestArrayParameter( )
    {
        mainWindow     = new MainWindow("Test Array Passing");
        outputBox      = new OutputBox( mainWindow );
        inputBox       = new InputBox( mainWindow );
                
        mainWindow.setVisible( true ); 
        outputBox.setVisible( true );
    }
    
    
    public void testArrayPassing( )
    {
        test1( );
        
        test2( );
        
        test3( );
    }
    
    /** 
     * Passes an array argument to the searchMinimum method.
     * The called method returns the index to the smallest
     * element in the passed array.
     */
    private void test1( )
    {
        float[] arrayOne, arrayTwo;
        
        float number;
        
        int size = 10;
        
        //create and assign values to arrayOne and arrayTwo
        arrayOne = new float[size];
        arrayTwo = new float[size];
        
        for ( int i = 0; i < size; i++ ) {
            number = inputBox.getFloat( "Number " + (i+1) );
            
            arrayOne[i] = number;
            arrayTwo[size - (i+1)] = number;
        }
        
        
        //get the index of the smallest element of arrayOne
        int minOne = searchMinimum( arrayOne );
        
        //get the index of the smallest element of arrayTwo
        int minTwo = searchMinimum( arrayTwo );
        
        //output the result
        outputBox.print("Mimimum value in Array One is ");
        outputBox.print(arrayOne[minOne] + " at position " + minOne);
        
        outputBox.skipLine(2);
        
        outputBox.print("Mimimum value in Array Two is ");
        outputBox.print(arrayTwo[minTwo] + " at position " + minTwo);
        
     }
     
     
     /**
      * Caller declares the array. The array is created by the
      * called method readFloats. 
      */
     private void test2( )
     {
        float[] arrayOne, arrayTwo;
        
        //assign values to arrayOne and arrayTwo
        //using
        arrayOne = readFloats();
        
        arrayTwo = readFloats();
        
        
        outputBox.skipLine(2);
        outputBox.printLine("Array One:");
        
        for (int i = 0; i < arrayOne.length; i++ ) {
            outputBox.printLine( arrayOne[i] );
        }
    
        
        outputBox.skipLine(2);
        outputBox.printLine("Array Two:");
        
        for (int i = 0; i < arrayTwo.length; i++ ) {
            outputBox.printLine( arrayTwo[i] );
        }
     }
     
     
     /**
      * Caller creates an array and passes this array
      * to the readIntegers method. This method fills
      * the elements.
      */
     private void test3( )
     {
        
        int[] myIntArray = new int[5];
        
        readIntegers(myIntArray);
        
        outputBox.skipLine(2);
        outputBox.printLine("int Array:");
        
        for (int i = 0; i < myIntArray.length; i++ ) {
            outputBox.printLine( myIntArray[i] );
        }
        
     }
    
    
    
    /**
     * Inputs the float values and returns an array of float
     *
     * @return an array of float values entered
     */
    public float[] readFloats()
    {
        float[] number;
        int N = inputBox.getInteger("How many input values?");
    
        number = new float[N];
    
        for (int i = 0; i < N; i++) {
            number[i] = inputBox.getFloat("Number " + i);
        }
        return number;
    }


    /**
     * Returns the index to the smallest element in the 
     * passed array.
     *
     * @param number an array of float to search for the
     *               smallest element
     *
     * @return the index to the smallest element
     */
    public int searchMinimum(float[] number)
    {
        int indexOfMinimum = 0;
    
        for (int i = 1; i < number.length; i++) {
            if (number[i] < number[indexOfMinimum]) { //found a 
                indexOfMinimum = i;                   //smaller element
            }
        }    
        return indexOfMinimum;
    }


    /**
     * Inputs the integer values and fill the passed
     * array with the entered values.
     *
     * @param number an array of integers whose elements
     *               are filled by this method
     */
    public void readIntegers(int[] number)
    {
        for (int i = 0; i < number.length; i++) {
            number[i] = inputBox.getInteger("Number " + i);
        }
    }


    
}