The Problem Design a program that lets the user enter the total rainfall for eac
ID: 3891106 • Letter: T
Question
The Problem Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the number of months above and below the average rainfall, and the highest rainfall for each quarter of the year.
The Assignment Write a Java program that contains five method/functions as follows:
main(). Calls the functions enterRain, rainStats, aboveBelow, and quarterlyRain.
enterRain(). Uses a loop to allow the user to enter rainfall for each of 12 months. Each rainfall should be assigned to an array monthlyRain. monthlyRain should be returned to main() after rainfall for each month has been entered.
rainStats(). Accepts the array monthlyRain. Calculates a total for the year and an average rainfall per month from monthlyRain. Displays the total and average rainfall for the year.
aboveBelow(). Searches the array to count the number of months with above average and below average rainfall. Displays the number of months at or above the average and the number of months below the average rainfall for the year.
quarterlyRain(). Searches the array and finds/displays the highest rainfall for each quarter of the year.
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// RainFall.java
import java.util.Scanner;
public class RainFall {
static Scanner scanner = new Scanner(System.in);
/**
* method to read the rainfall stats from user and fill the monthlyrain
* array
*
* @return - an array of double containing rainfalls for 12 months
*/
static double[] enterRain() {
double monthlyRain[] = new double[12];
for (int i = 0; i < 12; i++) {
System.out.print("Enter rainfall for month " + (i + 1) + ": ");
monthlyRain[i] = Double.parseDouble(scanner.nextLine());
}
return monthlyRain;
}
/**
* method to display total and average rainfall
*
* @param monthlyRain
* - array containing rainfall info
*/
static void rainStats(double monthlyRain[]) {
double total = 0;
// calculating total
for (int i = 0; i < monthlyRain.length; i++) {
total += monthlyRain[i];
}
System.out.printf("Total rainfall: %.2f " ,total);
// finding average (total by count)
double avg = (double) total / monthlyRain.length;
System.out.printf("Average rainfall: %.2f " , avg);
}
/**
* method to display number of months above and below average rainfall
*
* @param monthlyRain
* - array containing rainfall info
*/
static void aboveBelow(double monthlyRain[]) {
// finding average first
double total = 0;
for (int i = 0; i < monthlyRain.length; i++) {
total += monthlyRain[i];
}
double avg = (double) total / monthlyRain.length;
/***
* initializing variables to keep the track of number of months above
* and below average
*/
int above = 0;
int below = 0;
// looping through the array and incrementing the appropriate counter
for (int i = 0; i < monthlyRain.length; i++) {
if (monthlyRain[i] >= avg) {
above++;
} else {
below++;
}
}
System.out.println("There are " + above
+ " months with rainfall above average, and " + below
+ " months with rainfall below average");
}
/**
* method to display highest rainfall in each quarter
*
* @param monthlyRain
* - array containing rainfall info
*/
static void quarterlyRain(double monthlyRain[]) {
int monthsIndex = 0;
int quarter = 1;
/**
* looping for 4 times (4 quarters)
*/
while (quarter <= 4) {
/**
* finding the highest in this quarter
*/
double highest = 0;
for (int i = monthsIndex; i < monthsIndex + 3; i++) {
if (i == monthsIndex) {
// first month in the quarter
highest = monthlyRain[i];
} else if (monthlyRain[i] > highest) {
highest = monthlyRain[i];
}
}
/**
* displaying the highest rainfall for this quarter
*/
System.out.println("Highest rainfall for quarter " + quarter
+ " is: " + highest);
// moving to next quarter
monthsIndex += 3;
quarter++;
}
}
public static void main(String[] args) {
// getting input
double monthlyRain[] = enterRain();
// printing total and avg
rainStats(monthlyRain);
// finding number of months above and below avg
aboveBelow(monthlyRain);
// printing quarterly highest rainfalls info
quarterlyRain(monthlyRain);
}
}
/*OUTPUT*/
Enter rainfall for month 1: 44.5
Enter rainfall for month 2: 23.6
Enter rainfall for month 3: 11.5
Enter rainfall for month 4: 30.5
Enter rainfall for month 5: 78.5
Enter rainfall for month 6: 88.1
Enter rainfall for month 7: 45.2
Enter rainfall for month 8: 30.2
Enter rainfall for month 9: 25.1
Enter rainfall for month 10: 10.7
Enter rainfall for month 11: 3.6
Enter rainfall for month 12: 5.6
Total rainfall: 397.10
Average rainfall: 33.10
There are 4 months with rainfall above average, and 8 months with rainfall below average
Highest rainfall for quarter 1 is: 44.5
Highest rainfall for quarter 2 is: 88.1
Highest rainfall for quarter 3 is: 45.2
Highest rainfall for quarter 4 is: 10.7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.