import java.util.Random;

public class ArraysExercises{

    /**
     * Displays all of the elements of an array (of doubles) on one line
     * @param arr An array of values
     */
    public static void displayAll(double[] arr){
        // System.out.println("displayAll(" + arr + " (" + arr.length + " elements)");
        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 a Array of values
     * @param ... ...
     * @param ... ...
     * @return A copy of the requested subset from the array
     */
    public static int[] sub(int[] a, int startIndex, int endIndex){
        int[] s = new int[endIndex - startIndex];
        for(int i = startIndex; i < a.length && i < endIndex; ++i ){
            s[i - startIndex] = a[i];
        }
        /*
        for(int j = 0; j < s.length; ++j ){
            s[j] = a[j + startIndex];
        }
        */
        return s;
    }


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


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


    /**
     * Append an item to the end of an array
     *
     */
    public static String[] append(String[] strArray, String item){
        // Step 1: Make a longer array
        String[] res = new String[strArray.length + 1];

        // Step 2: Copy all of the existing elements to the new array
        for( int i = 0; i < strArray.length; ++i){
            res[i] = strArray[i];
        }
        // Step 3: Add/append new item
        res[strArray.length] = item;

        // Step 4: return reference to new array
        return res;
    }

    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.
        */
        // DataType variableName;
        double[] milesRan = new double[365];
        milesRan[0] = 3.1; // 5K

        // put in random values between 0..5K
        Random rand = new Random();
        for(int i = 1; i < milesRan.length; ++i){
            milesRan[i] = rand.nextDouble() * 3.1;
        }

        /*
          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;
        for(int i = 0; i < 100; ++i){
            total += milesRan[i];
        }
        System.out.println("total: " + total);
        System.out.println("average: " + total / 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.
        */
        displayAll( milesRan );
        System.out.println( "Average of all: " + average( 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.
         */


        // display each element before the method call
        int[] nums = {7, 5, 3, 1, 55, 42, 99, 97, 45};
        System.out.println("Before:");
        for( int i = 0; i < nums.length; ++i){
            System.out.println(nums[i]);
        }
        twiceValues( nums );

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


        /*
          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 );
        }
    }
}