Java Array Excercise Write a RainFall class that stores the total rainfall for e
ID: 3762394 • Letter: J
Question
Java Array Excercise
Write a RainFall class that stores the total rainfall for each of 12 months into an array of
floats. 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
Month names should be stored in an array of Strings.
Demonstrate the class in a complete program.
Write a method that produces output like this:
Austin Tx Rainfall 2009
Jan 0.40
Feb 0.94
Mar 3.21
Apr 3.74
May 1.73
Jun 1.03
Jul 1.27
Aug 2.58
Sep 6.98
Oct 6.90
Nov 2.80
Dec 2.53
Total 34.11
Average 2.84
Max 6.98
Min 0.40
Explanation / Answer
/************************************************
* The java program that intializes an array of type double
* for 12 months.Then calls the static methods to calculate
* total, average,max and min .Then call print method that
* prints the month name and rain fall
* then prints the total, average, max and min value.
**************************************************/
//RainFall.java
public class RainFall
{
public static void main(String[] args)
{
//rain fall array for 12 months
double rainfall[]=
{0.40,0.94,3.21,
3.74,1.73,1.03,
1.27,2.58,6.98,
6.90,2.80,2.53};
//Month names for 12 months
String months[] =
{"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October","November", "December" };
//calling methods
double total=totalRainFall(rainfall);
double avg=avgRainFall(rainfall);
double max=maxRainFall(rainfall);
double min=minRainFall(rainfall);
//call print method to print the output
print(months,rainfall,total,avg,max,min);
}
/**The method print takes the month and rain fall arrays
* ,total,avaerage ,max and min and prints the values on the console*/
private static void print(String[] months,double[] rainfall,
double total,double avg,
double max, double min) {
System.out.println("Austin Tx Rainfall 2009");
for (int i = 0; i < rainfall.length; i++) {
System.out.printf("%-10s%-10.2f ",months[i],rainfall[i]);
}
System.out.printf("%-10s%-10.2f ","Total",total);
System.out.printf("%-10s%-10.2f ","Average",avg);
System.out.printf("%-10s%-10.2f ","Max",max);
System.out.printf("%-10s%-10.2f ","Min",min);
}
/**The method minRainFall that takes the double array and
* returns the minimum rain fall*/
private static double minRainFall(double[] rainfall) {
//assume value at index =0
double min=rainfall[0];
for (int i = 1; i < rainfall.length; i++) {
if(rainfall[i]<min)
min=rainfall[i];
}
return min;
}
/**The method maxRainFall that takes the double array and
* returns the maximum rain fall*/
private static double maxRainFall(double[] rainfall) {
//assume value at index =0
double max=rainfall[0];
for (int i = 1; i < rainfall.length; i++) {
if(rainfall[i]>max)
max=rainfall[i];
}
return max;
}
/**The method avgRainFall that takes the double array and
* returns the average rain fall*/
private static double avgRainFall(double[] rainfall) {
//call totalRainFall to get total of array
double total=totalRainFall(rainfall);
return total/rainfall.length;
}
/**The method totalRainFall that takes the double array and
* returns the total rain fall*/
private static double totalRainFall(double[] rainfall)
{
//declare and total to zero
double total=0;
//add the double values
for (int i = 0; i < rainfall.length; i++) {
total+=rainfall[i];
}
//return total
return total;
}
}//end of the class RainFall
--------------------------------------------
Sample Output:
January 0.40
February 0.94
March 3.21
April 3.74
May 1.73
June 1.03
July 1.27
August 2.58
September 6.98
October 6.90
November 2.80
December 2.53
Total 34.11
Average 2.84
Max 6.98
Min 0.40
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.