In: Computer Science
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest. Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. These methods MUST be your original code. Your program should output all the values in the array and then output the average high and the average low.
Remember to submit all your .java files, .class files and screenshots of your code and output.
//RainFall.java import java.util.Scanner; public class RainFall { public static double averageLow(double arr[][]){ double sum = 0; for(int i = 0;i<12;i++){ sum += arr[i][0]; } return sum/12; } public static double averageHigh(double arr[][]){ double sum = 0; for(int i = 0;i<12;i++){ sum += arr[i][1]; } return sum/12; } public static void main(String[] args) { double arr[][] = new double[12][2]; Scanner scanner = new Scanner(System.in); for(int i = 0;i<12;i++) { System.out.print("Enter low temperature for month " + (i+1)+": "); arr[i][0] = scanner.nextDouble(); System.out.print("Enter high temperature for month " + (i+1)+": "); arr[i][1] = scanner.nextDouble(); } System.out.println("Month\tLow\t\tHigh"); for(int i = 0;i<arr.length;i++){ System.out.print((i+1)+"\t"); for(int j = 0;j<arr[i].length;j++){ System.out.print(arr[i][j]+"\t\t"); } System.out.println(); } System.out.println("Average high temperatures = "+averageHigh(arr)); System.out.println("Average low temperatures = "+averageLow(arr)); } }
Enter low temperature for month 1: 1 Enter high temperature for month 1: 2 Enter low temperature for month 2: 3 Enter high temperature for month 2: 4 Enter low temperature for month 3: 5 Enter high temperature for month 3: 6 Enter low temperature for month 4: 7 Enter high temperature for month 4: 8 Enter low temperature for month 5: 9 Enter high temperature for month 5: 12 Enter low temperature for month 6: 34 Enter high temperature for month 6: 56 Enter low temperature for month 7: 78 Enter high temperature for month 7: 98 Enter low temperature for month 8: 12 Enter high temperature for month 8: 23 Enter low temperature for month 9: 34 Enter high temperature for month 9: 45 Enter low temperature for month 10: 56 Enter high temperature for month 10: 67 Enter low temperature for month 11: 78 Enter high temperature for month 11: 89 Enter low temperature for month 12: 76 Enter high temperature for month 12: 99 Month Low High 1 1.0 2.0 2 3.0 4.0 3 5.0 6.0 4 7.0 8.0 5 9.0 12.0 6 34.0 56.0 7 78.0 98.0 8 12.0 23.0 9 34.0 45.0 10 56.0 67.0 11 78.0 89.0 12 76.0 99.0 Average high temperatures = 42.416666666666664 Average low temperatures = 32.75 Process finished with exit code 0