import java.util.Random; 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 display(double[] array){ for( int i = 0; i < array.length; ++i ){ System.out.println( array[ i ] ); } } /** * 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 start the first index of the subset * @param end the index past the end of the subset * @return A copy of the requested subset from the array */ public static double[] subset( double[] arr, int start, int end){ // iterate from start to end - 1 and copy the values from arr to a new array double[] sub = new double[ end - start ]; int j = 0; for( int i = start; i < end && i < arr.length; ++i){ sub[j] = arr[i]; ++j; } return sub; } /** * Returns the average value * @param ... ... * @return the average value */ public static double average( double[] arr ){ double total = 0.0; for( int i = 0; i < arr.length; ++i){ total = total + arr[ i ]; } double average = total / arr.length; return average; } public static void twice( double[] a ){ for( int i = 0; i < a.length; ++i){ a[i] = a[i] * 2; } } /** * Append a string on to the end of the array * @param arr the array of original values * @param str the item to append */ public static String[] append( String[] arr, String newItem){ // Step 1: Create a new array (of size one more than arr) String[] newArray = new String[ arr.length + 1 ]; // Step 2: Copy all elements of arr into the new array for( int i = 0; i < arr.length; ++i){ newArray[i] = arr[i]; } // Step 3: Add the new item to the new array (at the end) newArray[ arr.length ] = newItem; // Step 4: return a reference to the new array return newArray; } public static void main( String[] args ){ System.out.println("Command-line Args:"); for( String arg : args ){ System.out.println(arg); } Random rand = new Random(); /* 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. */ // To declare a new variable and allocate memory for it: // DataType variableName = new DataType() double[] milesRan = new double[365]; milesRan[ 0 ] = 3.1; // 5K // add simulated data for( int i = 1; i < milesRan.length; ++i){ milesRan[i] = rand.nextDouble() * 5.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 total = 0.0; for( int i = 0; i < 100; ++i){ total = total + milesRan[ i ]; } double average = total / 100; System.out.println("The average for the first 100 days is: " + average); /* int j = 0; // total = total + milesRan[ ++j ]; // uses index 1, afterward j is 1 (notice that we would not have used index 0) BAD // total = total + milesRan[ ++j ]; // uses index 2, afterward j is 2 total = total + milesRan[ j++ ]; // uses index 0, afterward j is 1 total = total + milesRan[ j++ ]; // uses index 1, afterward j is 2 */ /* 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 ); System.out.println( "The average for the entire year is: " + 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 // done above twice( milesRan ); // display each element after the method call display( milesRan ); /* 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). */ // miles ran during Spring Break: Saturday to Sunday int startOfSpringBreakJulianDate = 31/*Jan*/ + 28 /*Feb*/ + 15; int endOfSpringBreakJulianDate = startOfSpringBreakJulianDate + 8; double[] subArray = subset( milesRan, startOfSpringBreakJulianDate, endOfSpringBreakJulianDate); System.out.println("Spring Break:"); display( subArray ); /* 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 ); } } }