public class ArraysExercises02{

    /**
     * Displays all of the elements of an array (of doubles) on one line
     * @param arr An array of values
     */
    public static void display(double[] arr){
        for( int i = 0; i < arr.length; ++i){
            System.out.print( arr[i] + ", ");
        }
        System.out.println();
    }
    public static void display(int[] 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 the original array
     * @param start first index to copy to new array
     * @param end the last index to copy to the new array (inclusive)
     * @return A copy of the requested subset from the array
     */
    public static int[] subset(int[] a, int start, int end){
        int subLength = end - start + 1;
        int[] sub = new int[ subLength ];

        for(int i = start; i <= end; ++i){
            //System.err.println("subset: i: " + i + " (subLength: " + subLength+")");
            sub[i - start] = a[i];
        }
        return sub;
    }


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


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

    /**
     * Append a string to an array
     * @param strs the original array
     * @param newItem the element to add to strs
     */
    public static String[] append( String[] strs, String newItem){
        // Step 1: Create a new array with space for the new element
        String[] strsExpanded = new String[strs.length + 1];

        // Step 2: Copy old array to new array
        for(int i = 0; i < strs.length; ++i){
            strsExpanded[i] = strs[i];
        }

        // Step 3: Add new element
        strsExpanded[ strsExpanded.length - 1 ] = newItem;

        // Step 4: Return
        return strsExpanded;
    }

    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 numbers in the array to work with
        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 100
          days of running.
          Write Java code to calculate and display the average miles run per day
          (for the first 100 days).
        */
        double total = 0.0;
        int numDays = 100;
        for( int i = 0; i < numDays; ++i){
            total = total + milesRan[i];
        }
        double averageFirst100 = total / numDays;
        System.out.println("Average for the first "+numDays+" days: " + averageFirst100);

        /*
          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.
        */
        display(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, 15, 17, 21, 5, 11};

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

        twice( values );

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

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

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


    }
}