Creating methods with array parameters
For this program you are going to create a variety of methods for operating on arrays. Do a good job because these method will also be used in Program 10. Download the starting code Prog9ArrayMethods.java into your Java project in Eclipse. This file contains an array of 366 daily low temperatures for Portland Maine Jan 1 - Dec 31 2020.
30, 23, 3, 3, 16, 7, -4, 8, 15, 22, 36, 35, 33, 31, 19,
15, 17, 31, 28, 22, 26, 27, 24, 24, 8, 0, 27, 25, 32,
31, 5, -1, 23, 23, 19, 22, 12, 2, 19, 21, 24, 33, 37,
31, 27, 19, 16, 18, 32, 36, 29, 25, 26, 25, 38, 37, 36,
. . .
You are going to be writing methods to operate on arrays. This one is already written for you:
public static int arrayMax(int[] a) {
int max = a[0];
for (int i = 0; i < a.length; i++)
if (a[i] > max)
max = a[i];
return max;
}
You will also be writing testing code in main() for each
method. This is the testing code for arrayMax():
System.out.println("Maximum value: " + max);
Run the starting code. It should work and find the maximum value in the array.
NOTE: In all the methods
that you write for this assignment, the methods should
return a value, but NOT print it. The testing code
will call the methods and print the results.
Create a method int arrayMin(int[] a) to find the minimum value, and write the testing code in main().
Create a method double arrayAverage(int[] a) to
calculate the average value for the array and return it as
a double, and write the testing code. (The average should
be 40.4153...)
Create a similar method double arrayAverage(int[] a,
int starti, int endi) that calculates the average for
a subsection of an array. The subsection goes from starti to
endi, but does not include the value at endi. This is the
same indexing convention used for substring(start, end).
Write testing code to test it on the subsection [5, 10).
(The average of that section is 18.4).
The starting code also contains an array monthStarts[]
that holds the starting indices in tmin[] for each month.
int[] monthStarts = {-1, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
The section of tmin[] for each month starts at the index specified in monthStarts[] for that month and continues up to the index for the start of the next month. For example the data for February is stored at indices from 31 up to but not including index 60. We can therefore use the interval [31, 60) to calculate the average temperature for February.
To make things easy and intuitive in monthStarts[] we are
using index 1 for January, 2 for February, ... , 13 for the
month after December.
In your main method write code that uses a loop, this array, and your method from part 3 to calculate the average temperatures for each month. Print your results in a clear and organized fashion.
Create a method to find the first occurrence of a specified value in the array and return the index it was found at. If it was not found, return -1. Your method will have two parameters: the array and the value.
Write testing code using a loop for these 11 test cases (0, 10, 20, 30, ... 100). Print your results in a clear and organized fashion.
Turn in the final version of your code and the testing results for all of the test cases.