/**                 Example to show the use of method overload
 *                and basic array handling in methods
 *
 */
public class ArrayMethods {

    //      Method to fill a pre-allocated array.
    //      with a specified value
    //
    //      Note: changing the ARRAY ELEMENT values in this method
    //            changes them in the main program.
    static double[] fillArray(double array[], double value) {
	for(int i = 0; i < array.length; i++) {       // Fill the array
	    array[i] = value;
	}
	return array;                     // Return the array.
    }


    //         Method to sum the elements of an array 
    //         and return a double, being the sum

    static double arraySum(double array[]) {
	double sum = 0;
	for(int i = 0; i < array.length; i++) {
		sum += array[i];
	    }
	return sum;
    }

    

    

    //      Method to allocate and fill array with
    //      spefified value                
    //     
    //      Note: this method has the same NAME as the fillArray method above
    //      but a different argument list, so it is a DIFFERENT method.
    //      The method is specified by its NAME and it ARGUMENT list.

    static double[] fillArray(int length, double value) {
	double array[] = new double[length];          // Allocate the array

	//          now use the method decalared above to do the
	//          work of filling the array; resuse code when ever
	//          possible!!!

	return fillArray(array,value);       
    }



    //                    Start of main program

    public static void main(String args[]){
	
	//              Declare and allocate an array
	double firstArray[] =    new double[10];

	//              Fill pre-allocated array with value 100.0

	firstArray = fillArray(firstArray, 100.0);
	
	//              Allocate and fill array (same name but
	//              different argument list, so different method!!!!

	double secondArray[] = fillArray(10,100.0); 

	//              Sum up both array and print out sums
	//              (which should be the same!!!!).

	double firstSum = arraySum(firstArray);
	double secondSum = arraySum(secondArray);
	System.out.println("First sum is : " + firstSum);
	System.out.println("Second sum is : " + secondSum);
	
    }
}



