In JAVA Write a program that uses a twodimensional array to
In JAVA
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods:
a. Method getData: This method reads and stores the data in the two-dimensional array.
b. Method averageHigh: This method calculates and returns the average high temperature of the year.
c. Method averageLow: This method calculates and returns the average low temperature of the year.
d. Method indexHighTemp: This method returns the index of the highest temperature in the array.
e. Method indexLowTemp: This method returns the index of the lowest temperature in the array.
(These methods must all have the appropriate parameters.)
Solution
CODE
package test;
import java.util.Scanner;
public class Temperatures {
//declaring temperatures as a static array
public static double[][] temperatures = new double[12][2];
public static void main(String[] args) {
getData();
System.out.println(\"Average high is : \"+averageHigh());
System.out.println(\"Average low is : \"+averageLow());
System.out.println(\"Month of highest temperature is : \"+indexHighTemp());
System.out.println(\"Month of lowest temperature is : \"+indexLowTemp());
}
//to get data of temperatures for all months from user
public static void getData() {
System.out.println(\"Enter details\");
Scanner in = new Scanner(System.in);
for (int i = 0; i < 12; i++) {
System.out.println(\"Enter highest temperature for month \" + (i + 1));
temperatures[i][0] = in.nextDouble();
System.out.println(\"Enter lowest temperature for month \" + (i + 1));
temperatures[i][1] = in.nextDouble();
}
}
//to get avegare of highest temperatures of all months
public static double averageHigh() {
double avg = 0;
for (int i = 0; i < 12; i++) {
avg += temperatures[i][0];
}
return avg / 12;
}
//to get sverage of lowest temperatures of all months
public static double averageLow() {
double avg = 0;
for (int i = 0; i < 12; i++) {
avg += temperatures[i][1];
}
return avg / 12;
}
//to get index of highest tempeartures of all months
public static int indexHighTemp() {
int index = 0;
double max = Double.MIN_VALUE;
for (int i = 0; i < 12; i++)
if (temperatures[i][0] > max)
{
index = i;
max =temperatures[i][0];
}
return index+1;
}
//to get index of lowest tempeartures of all months
public static int indexLowTemp() {
int index = 0;
double min = Double.MAX_VALUE;
for (int i = 0; i < 12; i++)
if (temperatures[i][1] < min)
{
index = i;
min = temperatures[i][1];
}
return index+1;
}
}
SAMPLE OUTPUT
Enter details
Enter highest temperature for month 1
60
Enter lowest temperature for month 1
50
Enter highest temperature for month 2
50
Enter lowest temperature for month 2
40
Enter highest temperature for month 3
40
Enter lowest temperature for month 3
30
Enter highest temperature for month 4
30
Enter lowest temperature for month 4
20
Enter highest temperature for month 5
20
Enter lowest temperature for month 5
10
Enter highest temperature for month 6
20
Enter lowest temperature for month 6
10
Enter highest temperature for month 7
30
Enter lowest temperature for month 7
20
Enter highest temperature for month 8
40
Enter lowest temperature for month 8
30
Enter highest temperature for month 9
50
Enter lowest temperature for month 9
40
Enter highest temperature for month 10
60
Enter lowest temperature for month 10
50
Enter highest temperature for month 11
70
Enter lowest temperature for month 11
60
Enter highest temperature for month 12
80
Enter lowest temperature for month 12
70
Average high is : 45.833333333333336
Average low is : 35.833333333333336
Index of highest temperature is : 11
Index of lowest temperature is : 4



