Rainfall Statistics Write a program that stores the name of
Rainfall Statistics
Write a program that stores the name of each month in an array of Strings, and the rainfall for each month of the year into an array of doubles. The program should determine the total rainfall for the year, the average monthly rainfall, and the months with the most and least amount of rain.
You will need:
Two one-dimensional arrays – one to store the names of each month and another to store the rainfall for each month. (What would be the size of these arrays?)
Use an initialization list to store the names of each month into the String array.
A for loop to receive the values of rainfall.
You should refer to your string array to get the names for the appropriate month.
The rainfall values should be stored in the doubles array.
Make sure the user does not enter a negative value for rainfall. (Use a while loop for this part)
A for loop to compute the running sum
A variable to store the average
A for loop (with a nested if statement) to determine the largest amount of rainfall in the doubles array
You need to refer to the String array to indicate the month with the largest rainfall.
A for loop (with a nested if statement) to determine the smallest amount of rainfall
You need to refer to the String array to indicate the month with the largest rainfall.
Sample output:
Enter the rainfall (in inches) for January: -5.5
Values must be 0 of more.
Re-enter the rainfall for January: 5.5
Enter the rainfall (in inches) for February: 4
Enter the rainfall (in inches) for March: 7.5
Enter the rainfall (in inches) for April: 10
Enter the rainfall (in inches) for May: 8.5
Enter the rainfall (in inches) for June: 6
Enter the rainfall (in inches) for July: 3
Enter the rainfall (in inches) for August: 2.5
Enter the rainfall (in inches) for September: 9
Enter the rainfall (in inches) for October: 7
Enter the rainfall (in inches) for November: 6.5
Enter the rainfall (in inches) for December: 8
The total rainfall for the year is 77.5 inches.
The average rainfall for the year is 6.46 inches.
The largest amount of rainfall for the year is 10 inches in April.
The smallest amount of rainfall for the year is 2.5 inches in August.
Code Shell
//*********************************************************
// Rainfall.java
//
// This program asks the user to enter rainfall amounts
// for the year. It then computes the total rainfall
// the average rainfall, and reports the months with the
// highest and lowest amount of rainfall.
//*********************************************************
import java.util.Scanner;
import java.text.DecimalFormat;
public class Rainfall
{
public static void main(String [] args)
{
// Declare constant to store size of both arrays
final int NUM_MONTH = /* complete this statement */;
Scanner scan = new Scanner(System.in);
// Declare array to store the rainfall data
double [] rain = new double [NUM_MONTH];
// Declare array to store the name of each month
// Use an initialization list here
String [] month = /* complete this statement */;
// Get the rainfall for each month.
for (int i = 0; i < NUM_MONTH; i++)
{
System.out.print(\"Enter the rainfall (in inches)
for \"+ month[i] + \": \");
rain[i] = scan.nextDouble();
// Validate the input
while (...)
{
}
}
// Calculate the running sum
double total = 0;
for (...)
// total computed here (see listing 5.7 in text)
// Format output to 2 decimal places
DecimalFormat fmt = new DecimalFormat(\"0.##\");
// Display the total rainfall
// Calculate the average
double average;
// Display the average
// Now determine the largest amount of rainfall
double largest = rain[0]; // holds largest value
// assume largest value is in first position
int index = 0;
for (...) // don’t compare first value to itself
// loop control variable should equal 1
{
if (/* value is greater than largest*/)
{
largest = /*value in array */;
index = /*position in array */;
}
}
// Display the largest amount of rainfall and the
// month when it occured
// Now determine the smallest amount of rainfall
double smallest = rain[0]; // holds smallest value
index = 0; // reset index
for (...) // don’t compare first value to itself
// loop control variable should equal 1
{
if (/* value is smaller than smallest*/)
{
smallest = /*value in array */;;
index = /*position in array */;
}
}
// Display the smallest amount of rainfall and the
// month when it occured
}
}
Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
------------------------------------------------------------------------------
OUTPUT:
Enter the rainfall (in inches)for January: -5.5
Values must be 0 of more.
Re-enter the rainfall for January
5.5
Enter the rainfall (in inches)for February: 4
Enter the rainfall (in inches)for March: 7.5
Enter the rainfall (in inches)for April: 10
Enter the rainfall (in inches)for May: 8.5
Enter the rainfall (in inches)for June: 6
Enter the rainfall (in inches)for July: 3
Enter the rainfall (in inches)for August: 2.5
Enter the rainfall (in inches)for September: 9
Enter the rainfall (in inches)for October: 7
Enter the rainfall (in inches)for November: 6.5
Enter the rainfall (in inches)for December: 8
The total rainfall for the year is 77.5 inches
The average rainfall for the year is 6.46 inches
The largest amount of rainfall for the year is 10.0 inches in April.
The smallest amount of rainfall for the year is 2.5 inches in August.



