Chapter 7. PC #1. Rainfall Class Write a RainFall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following: • the total rainfall for the year • the average monthly rainfall • the month with the most rain • the month with the least rain Demonstrate the class in a complete program. Main class name: RainFall (no package name) PLEASE MODIFY THIS CODE SO WHEN I UPLOAD IT TO HYPERGRADE IT PASSES THE TEST CASES. IT HAS TO PASS THE TEST CASES BECAUSE WHEN I UPLOAD IT TO HYPERGRADE IT DOES NOT PASS THE TEST CASES. I HAVE PROVIDED PROOF OF THE PART OF THE TEST CASE THAT FAILED. import java.util.Scanner; public class Rainfall { double rain[]; public Rainfall() { rain = new double[12]; } public double averageRainfall() { return getTotalRain()/12.0; } public double getTotalRain() { double total = 0.0; for(int i=0;i<12;i++) total = rain[i]; return total; } public int lowRainMonth() { double min = rain[0]; int month = 0; for(int i=1;i<12;i++) { if(rain[i] < min) { min = rain[i]; month = i; } } return month; } public int highRainMonth() { double max = rain[0]; int month = 0; for(int i=1;i<12;i++) { if(rain[i] > max) { max = rain[i]; month = i; } } return month; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); Rainfall obj = new Rainfall(); for(int i=0;i<12;i++) { System.out.println("Enter the rainfall amount for month " + (i+1) + ":"); obj.rain[i] = sc.nextDouble(); } System.out.println("Maximum rainfall: " + obj.rain[obj.highRainMonth()]); System.out.println("Minimum rainfall: " + obj.rain[obj.lowRainMonth()]); System.out.println("Total rainfall: " + obj.getTotalRain()); System.out.println("Average rainfall: " + obj.averageRainfall()); } } Test Case 1 Enter the rainfall amount for month 1:\n 1.2ENTER Enter the rainfall amount for month 2:\n 2.3ENTER Enter the rainfall amount for month 3:\n 3.4ENTER Enter the rainfall amount for month 4:\n 5.1ENTER Enter the rainfall amount for month 5:\n 1.7ENTER Enter the rainfall amount for month 6:\n 6.5ENTER Enter the rainfall amount for month 7:\n 2.5ENTER Enter the rainfall amount for month 8:\n 3.3ENTER Enter the rainfall amount for month 9:\n 1.1ENTER Enter the rainfall amount for month 10:\n 5.5ENTER Enter the rainfall amount for month 11:\n 6.6ENTER Enter the rainfall amount for month 12:\n 6.0ENTER Maximum rainfall: 6.6\n Minimum rainfall: 1.1\n Total rainfall: 45.2\n Average rainfall: 3.8\n
import java.util.Scanner;
public class Rainfall
{
double rain[];
public Rainfall()
{
rain = new double[12];
}
public double averageRainfall()
{
return getTotalRain()/12.0;
}
public double getTotalRain()
{
double total = 0.0;
for(int i=0;i<12;i++)
total = rain[i];
return total;
}
public int lowRainMonth()
{
double min = rain[0];
int month = 0;
for(int i=1;i<12;i++)
{
if(rain[i] < min)
{
min = rain[i];
month = i;
}
}
return month;
}
public int highRainMonth()
{
double max = rain[0];
int month = 0;
for(int i=1;i<12;i++)
{
if(rain[i] > max)
{
max = rain[i];
month = i;
}
}
return month;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Rainfall obj = new Rainfall();
for(int i=0;i<12;i++)
{
System.out.println("Enter the rainfall amount for month " + (i+1) + ":");
obj.rain[i] = sc.nextDouble();
}
System.out.println("Maximum rainfall: " + obj.rain[obj.highRainMonth()]);
System.out.println("Minimum rainfall: " + obj.rain[obj.lowRainMonth()]);
System.out.println("Total rainfall: " + obj.getTotalRain());
System.out.println("Average rainfall: " + obj.averageRainfall());
}
}
Test Case 1
1.2ENTER
Enter the rainfall amount for month 2:\n
2.3ENTER
Enter the rainfall amount for month 3:\n
3.4ENTER
Enter the rainfall amount for month 4:\n
5.1ENTER
Enter the rainfall amount for month 5:\n
1.7ENTER
Enter the rainfall amount for month 6:\n
6.5ENTER
Enter the rainfall amount for month 7:\n
2.5ENTER
Enter the rainfall amount for month 8:\n
3.3ENTER
Enter the rainfall amount for month 9:\n
1.1ENTER
Enter the rainfall amount for month 10:\n
5.5ENTER
Enter the rainfall amount for month 11:\n
6.6ENTER
Enter the rainfall amount for month 12:\n
6.0ENTER
Maximum rainfall: 6.6\n
Minimum rainfall: 1.1\n
Total rainfall: 45.2\n
Average rainfall: 3.8\n


Class Definition:
- The program defines a class called
Rainfall
.
- The program defines a class called
Instance Variable:
- The class has an instance variable
rain
, which is an array of doubles to store the rainfall data for each month.
- The class has an instance variable
Constructor:
- The class includes a constructor
Rainfall()
, which initializes therain
array to hold 12 months of rainfall data.
- The class includes a constructor
averageRainfall() Method:
- This method calculates and returns the average rainfall across all 12 months. It does this by dividing the total rainfall by 12 and rounding the result to one decimal place.
getTotalRain() Method:
- This method calculates and returns the total rainfall for all 12 months by summing up the values stored in the
rain
array.
- This method calculates and returns the total rainfall for all 12 months by summing up the values stored in the
lowRainMonth() Method:
- This method identifies the month with the lowest rainfall and returns its index (0-based). It iterates through the
rain
array and keeps track of the minimum value and its corresponding month.
- This method identifies the month with the lowest rainfall and returns its index (0-based). It iterates through the
highRainMonth() Method:
- This method identifies the month with the highest rainfall and returns its index (0-based). It iterates through the
rain
array and keeps track of the maximum value and its corresponding month.
- This method identifies the month with the highest rainfall and returns its index (0-based). It iterates through the
main() Method:
Inside the
main()
method, aScanner
objectsc
is created to read input from the user.An instance of the
Rainfall
class,obj
, is created to work with rainfall data.A loop is used to input the rainfall data for each of the 12 months, and the values are stored in the
rain
array.After inputting the data, the program displays the following statistics:
- Maximum rainfall by calling
obj.highRainMonth()
to get the month index and then accessing the corresponding value in therain
array. - Minimum rainfall by calling
obj.lowRainMonth()
to get the month index and then accessing the corresponding value in therain
array. - Total rainfall by calling
obj.getTotalRain()
. - Average rainfall by calling
obj.averageRainfall()
.
- Maximum rainfall by calling
User Interaction:
- The program prompts the user to enter rainfall data for each month and then displays the calculated statistics.
Step by step
Solved in 6 steps with 3 images









