Answer:
The Java code is given below with appropriate comments
Explanation:
import java.util.Scanner;
public class Temperature {
public static void main(String[] args)
{
// declaring the temperatures array
double[] maxTemp = new double[12];
double[] lowTemp = new double[12];
double avgHighTemp = 0;
double avgLowTemp = 0;
Scanner kb = new Scanner(System.in);
System.out.println("Please enter the highest and lowest temperatures");
System.out.println("of each month one by one below\n");
// inputting the temperatures one by one
for(int i = 0; i < 12; i++)
{
System.out.print("Please enter the highest temperature for month #" + (i+1) + ": ");
maxTemp[i] = Integer.parseInt(kb.nextLine());
System.out.print("Please enter the lowest temperature for month #" + (i+1) + ": ");
lowTemp[i] = Integer.parseInt(kb.nextLine());
System.out.println();
}
avgHighTemp = getAvgHighTemperature(maxTemp);
avgLowTemp = getAvgLowTemperature(lowTemp);
System.out.printf("Average high temperature is: %.2f\n", avgHighTemp);
System.out.printf("Average low temperature is: %.2f\n", avgLowTemp);
}
// method to calculate the average high temperature
public static double getAvgHighTemperature(double[] highTemp)
{
double total = 0;
for(int i = 0; i < 12; i++)
{
total += highTemp[i];
}
return total/12;
}
// method to calculate the average low temperature
public static double getAvgLowTemperature(double[] lowTemp)
{
double total = 0;
for(int i = 0; i < 12; i++)
{
total += lowTemp[i];
}
return total/12;
}
}