import java.util.Random;


public class ArraysExercises02{

    /**
     * Displays all of the elements of an array (of doubles) on one line
     * @param array An array of values
     */
    public static void displayArrayValues( double[] array ){
        for(int i = 0; i < array.length; ++i){
            System.out.println( array[ i ] );
        }
        System.out.println("With an enhanced for loop:");
        for( double num : array ){
            System.out.println( num );
        }
    }


    /**
     * Makes a copy of the elements in array from startIndex to endIndex (but not including endIndex) (up to the end of the array)
     * @param ... ...
     * @param ... ...
     * @param ... ...
     * @return A copy of the requested subset from the array
     */
    public static double[] subset( double[] arr, int startIndex, int endIndex){
        double[] sub = new double[ endIndex - startIndex ];
        int j = 0;
        for( int i = startIndex; i < endIndex && i < arr.length; ++i ){
            sub[j] = arr[i];
            ++j;
        }
        return sub;
    }


    /**
     * Returns the average value
     * @param array An array of values
     * @return the average value
     */
    public static double average( double[] array ){
        double sum = 0.0;
        for( int i = 0; i < array.length; ++i){
            sum = sum + array[ i ];
        }
        return sum / array.length;
    }


    public static void doubleValues( int[] arr ){
        for( int i = 0; i < arr.length; ++i){
            arr[i] = arr[i] * 2;
        }
    }


    public static String[] arrayExtender( String[] originalArray, String newItem ){
        // Step 1: Create a new array that is 1 longer than originalArray
        String[] newArray = new String[ originalArray.length + 1 ];

        // Step 2: For each element in originalArray, copy it into the new array
        int i = 0;
        for( i = 0; i < originalArray.length; ++i){
            newArray[ i ] = originalArray[i];
        }

        // Step 3: Append the newItem to the newArray
        newArray[ i ] = newItem;

        // Step 4: Return a reference to the new array
        return newArray;
    }

    public static void main( String[] args ){

        System.out.println( "args:" );
        for( String arg : args ){
            System.out.println( arg );
        }

        /*
          Array Basics: Declaring and Assigning
          You want to store the number of miles you ran for each day this year.
          Declare an array to store named milesRan that can store them.
          Assume that you ran 3.1 miles the first day of the year.
          Assign that value to the first element.
        */
        // dataType  variableName  =  new dataType
        double[] milesRan = new double[365];
        milesRan[ 0 ] = 3.1;  // 5K

        // Random values
        Random rand = new Random();
        for( int i = 1; i < milesRan.length; ++i){
            milesRan[i] = rand.nextDouble() * 10.0;
        }

        /*
          Array Basics: Loops & Arrays
          Assume that you have already populated milesRan with the first 100
          days of running.
          Write Java code to calculate and display the average miles run per day
          (for the first 100 days).
        */
        double sum = 0.0;
        for( int i = 0; i < 100; ++i){
            sum = sum + milesRan[ i ];
        }
        System.out.println("The average for the first 100 days is: " + sum / 100);

        /*
          Array Basics: Arrays and Methods - Parameters
          Write a method that displays all of the elements of an array
          (of doubles), all on one line.
          Use that method to display all of the values in milesRan.
        */
        //displayArrayValues( milesRan );


        /*
          Array Basics: Arrays and Methods
          Display all of the values of an int array.
          Next, pass that int array to a method that doubles each element.
          Then, display all of the values of an int array.
         */
        System.out.println("The average for the entire year (" + milesRan.length + " days) is: " + average( milesRan ));


        int[] nameOfArray = {5,4,3,2,1}; // new int[5];

        // display each element before the method call
        System.out.println( "Before the method call");
        for( int i = 0; i < nameOfArray.length; ++i){
            System.out.println( nameOfArray[i] );
        }

        doubleValues( nameOfArray );

        // display each element after the method call
        System.out.println( "After the method call");
        for( int i = 0; i < nameOfArray.length; ++i){
            System.out.println( nameOfArray[i] );
        }


        /*
          Write a method that takes an int array, a starting index and an ending
          index. Return a copy of the array that just has the elements from the
          starting index to the ending index.
          Display each of the values in the returned array (outside of the method).
        */
        double[] springBreakMiles = subset( milesRan, 73, 73 + 7);
        System.out.println( "Spring Break running was: ");
        displayArrayValues( springBreakMiles );


        /*
          Write a method that adds in "Liam" into the children array.
          Add a call to your method where the comment indicates, otherwise,
          do not change the code provided.
          Verify that it displays all four names.
         */

        String[] children = new String[ 3 ];
        children[0] = "Emma";
        children[1] = "Noah";
        children[2] = "Olivia";

        // insert method call here to add in Liam
        children = arrayExtender( children, "Liam" );

        for( String child : children ){
            System.out.println( child );
        }
    }
}