Lab 6a: Processing Arrays with Functions Introduction: As we have already seen, functions can be used in Java to package code so it can be easily reused in our program or in different programs. Most functions have one or more parameters. In this lab, we will see how functions can use array parameters to process large quantities of data. Instructions: Consider the following Java program. You can see that we have four functions that take in one double array as a parameter. These functions are currently empty “skeleton functions” without any code in them. The main program calls these four functions and prints the results. import java.util.Scanner; import java.util.Arrays; public class Main { static void add1(double data[]) { } static void sqrt(double data[]) { } static double average(double data[]) { return 0; } static double median(double data[]) { return 0; } public static void main(String[] args) { double data[] = {3,1,4,1,5,9,2,6,5,3,5}; System.out.println("Data: " + Arrays.toString(data)); add1(data); System.out.println("Add1(Data): " + Arrays.toString(data)); sqrt(data); System.out.println("Sqrt(Data): " + Arrays.toString(data)); System.out.println("Average(Data): " + average(data)); System.out.println("Median(Data): " + median(data)); } } Step 1: Copy this program into your Java program editor, and compile and run it. You will see that the program prints the initial array several times and says the average and median are equal to zero. This is clearly incorrect, but by using “skeleton functions” we are able to debug a main program that successfully calls each of these functions with the correct parameters. Step 2: Edit the “add1” function, and a for-loop that goes from 0 to data.length-1 and adds one to each of the elements of the data array. When you compile and run your program, you should see the new contents of the data array printed out. Notice that with array parameters we ARE able to modify the contents of a parameter, and this change is visible in the main program. Step 3: Edit the “sqrt” function, and add a for-loop that replaces each array element with “Math.sqrt(data[i])”. Now when you run the program you will see some more interesting data values. Fortunately, all of our data values are positive. If you have any negative values you will see “Nan” printed instead. Step 4: Edit the “average” function, and use a for-loop to add all of the values together, and then divide by data.length to calculate the average. To test this function, you should comment out the calls to “add1” and to “sqrt” in the main program. The average of the original data values should be equal to 44 / 11 = 4.0. Step 5: Finally, edit the “median” function to calculate and return the median value. As you know, the median is defined to be the value with half of the values smaller, and half of the values larger. To find this value, call the “Arrays.sort” function to sort the data array. The value in the data.length/2 location in the data array will be the median. When you run your program you should see that the median value of the original data is also 4.0. Step 6: Edit the “main” function, and copy/paste the line that prints the original data values after the line that prints the median value. When you run the program, you will see that the original data array has been sorted. This is an unwanted “side effect” of calculating the median. In order to leave the original data alone, we need to make a copy of the data before we do the sorting. Do a google search for “Java Arrays documentation” and look at the “copyOf” function. Call this function to create an array called “copy” and sort this array to calculate the median. Once you do this, the original data should be printed again. Step 7: Finally, edit “main” function to remove the comments so “add1” and “sqrt” are being called again. Now when you run the program you will see that the average and median are slightly different values. Edit the initialization of the data array and change the last value to 1000. When you run the program again you will see that the average changes significantly, but the median remains the same. Step 8: When your program is working correctly, upload your final program and a copy of your program output into Blackboard for grading.