Write a java program that reads in the average monthly rainfall for a city for e
ID: 3567352 • Letter: W
Question
Write a java program that reads in the average monthly rainfall for a city for each month of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program should then print out a nicely formatted table showing the rainfall for each of the previous 12 months as well as how much above or below average the rainfall was for each month. The average monthly rainfall is given for the months January, February, and so forth, in order. To obtain the actual rainfall for the previous 12 months, the program should first ask what the current month is and then ask for the rainfall figures for the previous 12 months. The output should correctly label the months. There are a variety of ways to deal with the month names. One straightforward method is to code the months as integers and then do a conversion to a string for the month name before doing the output. A large switch statement is acceptable in an output method. The month input can be handled in any manner you wish so long as it is relatively easy and pleasant for the user. Include a loop that allows the user to repeat this entire calculation until the user requests that the program end.
Have to use arrays and methods.
Explanation / Answer
import java.util.Scanner;
public class RainFall {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter current month 0.jan 1.feb ....");
int sm=sc.nextInt();
String months[] =
{ "January" , "February" , "March" , "April", "May",
"June", "July", "August", "September", "October",
"November", "December"
};
System.out.println("Enter average rain fall for each month starting from january");
int a[]=new int[12];
int ac[]=new int[12];
for (int i = 0; i < a.length; i++) {
if(sm+i+1>11)
System.out.println("enter for month "+months[i+sm-11]);
else
System.out.println("enter for month "+months[sm+i+1]);
a[i]=sc.nextInt();
}
System.out.println("Enter Actual rainfall for each month");
for (int i = 0; i < ac.length; i++) {
if(sm+i+1>11)
System.out.println("enter for month "+months[i+sm-11]);
else
System.out.println("enter for month "+months[sm+i+1]);
ac[i]=sc.nextInt();
}
System.out.println("-------------------------------");
System.out.println("Month Actual Difference");
for (int i = 0; i < ac.length; i++) {
if(sm+i+1>11)
System.out.println(months[i+sm-11]+" "+ac[i]+" "+(a[i]-ac[i]));
else
System.out.println(months[sm+i+1]+" "+ac[i]+" "+(a[i]-ac[i]));
//System.out.println(months[i]+" "+ac[i]+" "+(a[i]-ac[i]));
}
}
}
o/p:
enter current month 0.jan 1.feb ....
2
Enter average rain fall for each month starting from january
enter for month April
33
enter for month May
45
enter for month June
23
enter for month July
34
enter for month August
43
enter for month September
54
6enter for month October
465
enter for month November
23
enter for month December
34
enter for month January
45
enter for month February
56
enter for month March
6
Enter Actual rainfall for each month
enter for month April
56
enter for month May
34
enter for month June
234
enter for month July
76
enter for month August
23
enter for month September
45
enter for month October
12
enter for month November
55
enter for month December
44
enter for month January
33
enter for month February
22
enter for month March
11
-------------------------------
Month Actual Difference
April 56 -23
May 34 11
June 234 -211
July 76 -42
August 23 20
September 45 9
October 12 6453
November 55 -32
December 44 -10
January 33 12
February 22 34
March 11 -5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.