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 printArray(double[] array){
        int numberOfItems = array.length;
        for(int i = 0; i < numberOfItems; ++i){
            System.out.print( array[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 the original array
     * @param startIndex the first index to copy elements from
     * @param endIndex the index after the section to copy elements
     * @return A copy of the requested subset from the array
     */
    public static int[] subset(int[] arr, int startIndex, int endIndex){
        // System.err.println("subset(arr, "+startIndex+", " + endIndex+")");
        int[] subArray = new int[endIndex - startIndex];
        for(int i = startIndex; i < endIndex; ++i){
            //System.err.println("i: " + i);
            subArray[i-startIndex] = arr[i];
        }
        /*
        // loop through each of the indices in the new array
        for(int i = 0; i < subArray.length; ++i){
            System.err.println("i: " + i);
            subArray[i] = arr[i+startIndex];
        }*/
        return subArray;
    }


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

    /**
     * Multiply each of the elements in ints by 2
     * @param ints the array
     */
    public static void twice(int[] ints){
        for(int i = 0; i < ints.length; ++i){
            ints[i] = ints[i] * 2;
        }
    }

    /**
     * Append a new item onto an existing array
     * @param arr the original array
     * @param newItem the item to append
     */
    public static String[] addItem(String[] arr, String newItem){
        // How to add an element to a full array:
        // Step 1: Make a new array, which is 1 longer than the original
        String[] newArray = new String[arr.length + 1];

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

        // Step 3: Add newItem to the new array
        newArray[newArray.length - 1] = newItem;

        // Step 4: Return a reference
        return newArray;
    }

    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 = new double[365]; // valid indices 0..364
        milesRan[0] = 3.1; // 5K

        // Put some values in the array:
        for(int i = 1; i < milesRan.length; ++i){
            milesRan[i] = i;
        }

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

        /*
          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.
        */
        printArray( milesRan );

        System.out.println( "Average of all year: " + arrayAverage( 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 = {10, 55, 0, 24, 12, 69, 13, 101};

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

        twice( values );

        // display each element after the method call
        System.out.println("After:");
        for(int i = 0; i < values.length; ++i){
            System.out.println(values[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).
        */
        int[] subsetArray = subset( values, 1, 7 );

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

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

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


    }
}