The Problem Design a program that lets the user enter the total rainfall for eac
ID: 3891124 • 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. Each method/function will count 10 points and be based on the following: Correctness. The assigned task is accomplished (5 points
. Sample Input/Output Please enter rainfall for month 1
1
Please enter rainfall for month 2
2
Please enter rainfall for month 3
3
Please enter rainfall for month 4
4
Please enter rainfall for month 5
5
Please enter rainfall for month 6
6 Please enter rainfall for month 7
7
Please enter rainfall for month 8
8
Please enter rainfall for month 9
9
Please enter rainfall for month 10
10
Please enter rainfall for month 11
11
Please enter rainfall for month 12
12
The total rainfall for the year is 78 inches.
The average rainfall for the year is 6.5 inches.
6 months were at or above the average rainfall.
6 months were below the average rainfall.
The highest rainfall in Quarter 1 was 3.
The highest rainfall in Quarter 2 was 6.
The highest rainfall in Quarter 3 was 9.
The highest rainfall in Quarter 4 was 12.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Chegg
{
double total,avg;
public static void main (String[] args) throws java.lang.Exception
{
Chegg ob=new Chegg();
double monthlyRain[]=ob.enterRain();
ob.rainStat(monthlyRain);
ob.aboveBelow(monthlyRain);
ob.quarterlyRain(monthlyRain);
// your code goes here
}
double[] enterRain()
{
Scanner sc=new Scanner(System.in);
double monthlyRain[]=new double[12];
for(int i=0;i<12;i++)
monthlyRain[i]=sc.nextDouble();
return monthlyRain;
}
void rainStat(double a[])
{
for(int i=0;i<12;i++)
total+=a[i];
avg=total/12;
System.out.println("Total Rainfall in a year : "+total);
System.out.println("Average Rainfall in a year : "+avg);
}
void aboveBelow(double a[])
{
int above=0,below=0;
for(int i=0;i<12;i++)
{
if(avg>a[i])
above++;
else if(avg<a[i])
below++;
}
System.out.println("Total no. of month with above average Rainfall in a year : "+above);
System.out.println("Total no. of month with below average Rainfall in a year : "+below);
}
void quarterlyRain(double a[])
{
double h;
h=findHighest(a,0,2);
System.out.println("Highest rainfall in a month in quarter1 : "+h);
h=findHighest(a,3,5);
System.out.println("Highest rainfall in a month in quarter2 : "+h);
h=findHighest(a,6,8);
System.out.println("Highest rainfall in a month in quarter3 : "+h);
h=findHighest(a,9,11);
System.out.println("Highest rainfall in a month in quarter4 : "+h);
}
double findHighest(double a[],int s,int e)
{
double h=a[s];
for(int i=s+1;i<=e;i++)
{
if(h<a[i])
h=a[i];
}
return h;
}
}
Sample Input:
1 2 3 4 5 6 7 8 9 10 11 12
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.