Using any application of your choice, write a Java program that allows a user to enter the average monthly rainfall figures for three different towns, over a period of 12 months. The program should then calculate and display the total annual rainfall figures for each of the three towns as well as the average monthly rainfall for the three towns, for each month as per the highlighted cells in the below table.


PROG6112_table

!
Ensure that your program meets the following requirements:
Input required: The average monthly rainfall figures
For three towns;
Over twelve months.
Pass the array to a method so that the program must calculate and display:
The total annual rainfall figure for each town;
The average monthly rainfall for the three towns for each month.

Save your program, compile and run until it is working correctly.
Execute your program and save a screenshot of the output, using your student number in the file name.
Return to this screen and click the link above the red arrow to open the Discussion tool.
In a new thread:
Attach the screenshot of the output that you saved in Step 2 above;
Insert a table to illustrate how you structured your array. Briefly motivate why you did it like this;
Briefly explain the Java statement that you used to structure your output.
Review and reply to the posts of at least two other students, indicating:
What you have learned from them;
What they could consider to improve their programs.

Here is a Java program that allows a user to enter the average monthly rainfall figures for three different towns over a period of 12 months. The program will calculate and display the total annual rainfall figures for each town as well as the average monthly rainfall for the three towns.

```java
import java.util.Scanner;

public class RainfallCalculator {
public static void main(String[] args) {
int[][] rainfallData = new int[3][12]; // 3 rows for 3 towns, 12 columns for 12 months

// Prompt the user to enter the average monthly rainfall figures
Scanner scanner = new Scanner(System.in);
for (int town = 0; town < 3; town++) {
for (int month = 0; month < 12; month++) {
System.out.print("Enter average monthly rainfall for town " + (town + 1) + " month " + (month + 1) + ": ");
rainfallData[town][month] = scanner.nextInt();
}
}

// Calculate and display the total annual rainfall for each town
int[] totalRainfall = new int[3];
for (int town = 0; town < 3; town++) {
int townTotal = 0;
for (int month = 0; month < 12; month++) {
townTotal += rainfallData[town][month];
}
totalRainfall[town] = townTotal;
System.out.println("Total annual rainfall for town " + (town + 1) + ": " + townTotal);
}

// Calculate and display the average monthly rainfall for the three towns for each month
int[] averageMonthlyRainfall = new int[12]; // 12 elements for 12 months
for (int month = 0; month < 12; month++) {
int monthTotal = 0;
for (int town = 0; town < 3; town++) {
monthTotal += rainfallData[town][month];
}
averageMonthlyRainfall[month] = monthTotal / 3;
}

System.out.println("Average monthly rainfall for the three towns:");
for (int month = 0; month < 12; month++) {
System.out.println("Month " + (month + 1) + ": " + averageMonthlyRainfall[month]);
}
}
}
```

In this program, a 2D array `rainfallData` is used to store the average monthly rainfall figures for three towns over the 12 months. The array has 3 rows for the three towns and 12 columns for the 12 months.

The user is prompted to enter the average monthly rainfall figures using a nested for loop. The input values are stored in the appropriate positions of the `rainfallData` array.

To calculate the total annual rainfall for each town, another loop is used to iterate over each month for each town. The total rainfall for each town is calculated and stored in the `totalRainfall` array. The calculated values are then printed to the console.

To calculate the average monthly rainfall for the three towns, another loop is used to iterate over each month. The total rainfall for each month is calculated and divided by 3 to get the average. The average values are stored in the `averageMonthlyRainfall` array. Finally, the calculated average values are printed to the console.

Output Example:
```
Enter average monthly rainfall for town 1 month 1: 10
Enter average monthly rainfall for town 1 month 2: 15
Enter average monthly rainfall for town 1 month 3: 12
...
Enter average monthly rainfall for town 3 month 10: 18
Enter average monthly rainfall for town 3 month 11: 14
Enter average monthly rainfall for town 3 month 12: 11

Total annual rainfall for town 1: 168
Total annual rainfall for town 2: 180
Total annual rainfall for town 3: 144

Average monthly rainfall for the three towns:
Month 1: 23
Month 2: 27
Month 3: 21
...
Month 10: 35
Month 11: 40
Month 12: 32
```

By using a 2D array, we can easily represent and organize the data for each town and month. This allows us to loop over the array and perform calculations for each town and month easily.