Question
Array Methods- Using java code Part 1: Find the index of the first occurrence of a value. Create a method to find the first occurrence
Array Methods- Using java code
Part 1: Find the index of the first occurrence of a value.
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 for these 10 test cases (10, 20, 30, ... 100) Hint use a loop. Print your results in a clear and organized fashion.
Part 2: Counting values
Create a method to count the number of values less than or equal to a specified value.
For testing use this method to calculate the number of days where it never got above freezing (32 degrees).
Part 3: Monthly Averages
In the tmax[] array, the first 31 values are for January, the next 29 values are for February, etc. In main(), create an array with the number of days in each month. Your goal is to calculate the average temperature for each month. Hint: think about what sort of array method would be useful for calculating the average of a month. In main() you should have a loop that calls this method 12 times. Print your results in a clear and organized fashion.
Code:
//This file contains an array of 366 daily high temperatures for Jan 1 - Dec 31 2016.
public class Prog9ArrayMethods {
public static void main(String[] args) {
// daily high temperatures for Portland Maine Jan 1 - Dec 31 2016
int[] tmax = {41, 37, 40, 31, 26, 41, 39, 39, 36, 52, 47, 37, 27, 28,
30, 31, 32, 27, 18, 28, 27, 30, 20, 35, 34, 47, 46, 37,
35, 40, 51, 59, 48, 50, 55, 42, 35, 44, 28, 26, 34, 30,
21, 22, 12, 22, 50, 47, 33, 36, 53, 52, 38, 35, 35, 53,
41, 35, 50, 59, 43, 49, 29, 26, 36, 39, 40, 48, 66, 66,
55, 49, 60, 40, 42, 53, 55, 52, 42, 35, 40, 46, 52, 37,
36, 42, 43, 43, 47, 49, 72, 64, 53, 38, 25, 37, 40, 50,
55, 47, 47, 50, 52, 54, 56, 59, 60, 60, 65, 57, 59, 68,
68, 64, 57, 62, 45, 56, 49, 49, 55, 52, 46, 52, 50, 48,
61, 49, 56, 57, 67, 73, 69, 62, 74, 58, 52, 69, 62, 66,
74, 76, 65, 76, 60, 65, 79, 59, 87, 59, 68, 87, 75, 63,
61, 73, 58, 82, 73, 74, 66, 70, 64, 75, 71, 75, 81, 82,
75, 72, 73, 74, 85, 75, 76, 76, 77, 77, 82, 72, 72, 82,
79, 78, 82, 86, 81, 88, 69, 69, 59, 63, 81, 86, 82, 79,
91, 87, 78, 85, 79, 82, 84, 95, 89, 84, 84, 90, 85, 87,
82, 84, 71, 71, 77, 84, 83, 82, 94, 87, 87, 79, 70, 90,
99, 68, 94, 86, 79, 84, 88, 86, 78, 77, 78, 80, 86, 81,
88, 87, 79, 86, 80, 82, 81, 77, 77, 75, 74, 71, 71, 76,
91, 81, 80, 75, 80, 86, 69, 70, 70, 77, 73, 76, 83, 80,
74, 67, 64, 65, 71, 62, 64, 60, 59, 59, 70, 59, 63, 71,
74, 65, 61, 58, 63, 62, 64, 58, 53, 66, 74, 56, 78, 60,
65, 64, 51, 57, 48, 49, 48, 50, 46, 57, 50, 50, 61, 52,
55, 52, 49, 49, 61, 61, 49, 55, 47, 58, 62, 53, 55, 60,
63, 57, 50, 36, 42, 44, 37, 38, 47, 41, 43, 50, 42, 52,
50, 46, 41, 30, 39, 35, 42, 33, 27, 31, 33, 36, 39, 26,
9, 21, 49, 28, 30, 41, 33, 41, 40, 40, 39, 53, 39, 39,
37, 36};
int max = arrayMax(tmax);
System.out.println("Maximum value: " + max);
int min = arrayMin(tmax);
System.out.println("Minimum value: " + min);
double average = arrayAverage(tmax);
System.out.println("Average value: " + average);
}
// return the maximum value in the array
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;
}
public static int arrayMin(int[] a) {
int min = a[0];
for (int i = 0; i < a.length; i++) {
if (a[i] < min)
min = a[i];
}
return min;
}
public static double arrayAverage(int[] a) {
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum = sum + a[i];
}
double average = (double)sum/a.length;
return average;
}
//Add new method for index of the first occurrence of a value
//Add new method for counting values
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started