public class ArraysExercises{ /** * Add an element to a full array * @param array full array * @return */ public static String[] addElement(String[] array, String newElement){ // Step 1: Make a bigger array // Step 2: Copy elements from the original array to the new array // Step 3: Add newElement to the new array // Step 4: Return a reference // Step 1: Make a bigger array String[] expandedArray = new String[array.length + 1]; // Step 2: Copy elements from the original array to the new array int i = 0; for( String element : array ){ expandedArray[i++] = element; } /* for( int i = 0; i < array.length; ++i){ expandedArray[i] = array[i]; } */ // Step 3: Add newElement to the new array expandedArray[i] = newElement; // Step 4: Return a reference return expandedArray; } /** * Displays all of the elements of an array (of doubles) on one line * @param array An array of values */ public static void displayAll(double[] array){ for(int i = 0; i < array.length; ++i){ System.out.print(array[i] + " "); } System.out.println(); } /** * Displays all of the elements of an array (of ints) on one line * @param array An array of values */ public static void displayAll(int[] array){ for(int i = 0; i < array.length; ++i){ System.out.print(array[i] + " "); } System.out.println(); } /** * Double all of the values in array * @param array the array of original values */ public static void twice( int[] array ){ for(int i = 0; i < array.length; ++i){ array[i] = 2 * 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 a array of values * @param startIndex the first index of the subset * @param endIndex the first index after the subset * @return A copy of the requested subset from the array */ public static int[] slicer(int[] a, int startIndex, int endIndex){ int newArray[] = new int[endIndex - startIndex]; for(int i = startIndex; i < endIndex; ++i){ newArray[i - startIndex] = a[i]; } return newArray; } /** * Returns the average value * @param ... ... * @return the average value */ public static void main( String[] args ){ // Display all of the command line arguments System.out.println("args:"); System.out.println("args: " + args); for(int i = 0; i < args.length; ++i){ System.out.println("args["+i+"]: " + args[i]); } /* Array Basics: Declaring and Assigning You want to store the number of miles you ran for each day this year. Declare an array 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. */ // Declare an array named milesRan that can store them. double[] milesRan = new double[365]; // Assume that you ran 3.1 miles the first day of the year. milesRan[0] = 3.1; // 5K System.out.println("I ran " + milesRan[0] + " miles on day 1!"); /* // days of the week String[] daysOfTheWeek = new String[7]; daysOfTheWeek[0] = "Sunday"; */ // add in random date for(int i = 1; i < milesRan.length; ++i){ milesRan[i] = Math.random() * 26.2; } /* 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). */ // average = sum / count // for(initialization; continuation criterion; update ){ // loop body // } double sum = 0.0; int count = 100; for(int i = 0; i < count; ++i){ sum = sum + milesRan[i]; } double average = sum / count; System.out.println("The average of the first " + count + " days is " + 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. */ // method call here displayAll( 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[] steps = new int[21]; int index = 0; steps[ index++ ] = 2000; // Good job Jaivan! // put in some random values for testing for(int i = 1; i < steps.length; ++i){ steps[ index++ ] = (int)(Math.random() * 15000); } // display each element before the method call System.out.println("Steps:"); displayAll( steps ); twice( steps ); // display each element after the method call System.out.println("Steps (after multiplier):"); displayAll( steps ); /* 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[] secondWeek; secondWeek = slicer( steps, 7, 14 ); System.out.println("\nSecond Week:"); displayAll( secondWeek ); /* 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 = addElement(children, "Liam"); for( String child : children ){ System.out.println( child ); } } }