public class ArraysExercises01{

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


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

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

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


    public static String[] addAnElement( String[] a, String item ){
        // Step 1: Make a new array (the size of a + 1)
        String[] longerArray = new String[ a.length + 1 ];

        // Step 2: Copy all elements from a to the new array
        for(int i = 0; i < a.length; ++i){
            longerArray[i] = a[i];
        }

        // Step 3: Add in item to the new array
        longerArray[longerArray.length - 1] = item;

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


    public static void main( String[] args ){

        /*
          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.
        */

        /* 
        double[] milesRan;
        milesRan = new double[365];
        // the same as below
        */
        double[] milesRan = new double[365]; // valid indices are 0..364
        milesRan[0] = 3.1; // 5K
        //milesRan[364] = 9999.9;

        // put values in the array (for ease, just use the index as the value)
        for(int i = 1; i < 365; ++i ){
            milesRan[i] = i;
        }

        /*
          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];
        }
        double average = sum / 100.0;

        /*
          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.
        */
        displayAllElements( milesRan );
        System.out.println( averageOfValues( 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.
         */
        int[] values = {0, 2, 4, 6, 8, 10};
        for( int i = 0; i < values.length; ++i){
            int valueToDisplay = values[i];
            System.out.print( valueToDisplay + " " );
        }
        System.out.println();

        doubleValues( values );

        for( int i = 0; i < values.length; ++i){
            System.out.print( values[i] + " " );
        }
        System.out.println();

        // display each element before the method call

        int[] midValues = subset( values, 2, 4 ); // returns [8,12,16] 

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


        /*
          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).
        */


        /*
          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 = addAnElement( children, "Liam" );

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


    }
}