RainFall clss using Array. Write a RainFall class that stores the total rainfall
ID: 3861698 • Letter: R
Question
RainFall clss using Array.
Write a RainFall class that stores the total rainfall for each of the 12 months into an array of doubles. The program should have methids that return the following:
- total rainfall for the year
- the average monthly rainfall
- the month with the most rain
- the month with the least rain
Demonstrate the class in a complete program. Input Validation: Do not accept negative numbers for monthly rainfall figures.
Code RainFall class your operation class and RainFallApp as the driver.
Please use Java and use comments to explain. Thank you!
Explanation / Answer
Rainfall.java
public class Rainfall {
//Declaring array
double rainfall[];
//Parameterized constructor
public Rainfall(double[] rainfall) {
super();
this.rainfall = rainfall;
}
//This method will calculate the total rainfall of the year
public double totalRainFall()
{
//Declaring variable
double sum=0.0;
for(int i=0;i<rainfall.length;i++)
{
//calculating the sum
sum+=rainfall[i];
}
return sum;
}
//This method will calculate the average of yearly rainfall
public double average()
{
//Declaring variables
double sum=0.0,average=0.0;
for(int i=0;i<rainfall.length;i++)
{
//calculating the sum
sum+=rainfall[i];
}
//calculating the average
average=sum/rainfall.length;
return average;
}
//This method will find the highest rainfall of the month
public double highestRainfall()
{
double max=rainfall[0];
for(int i=0;i<rainfall.length;i++)
{
if(max<rainfall[i])
max=rainfall[i];
}
return max;
}
//This method will find the lowest rainfall of the month
public double lowestRainfall()
{
double min=rainfall[0];
for(int i=0;i<rainfall.length;i++)
{
if(min>rainfall[i])
min=rainfall[i];
}
return min;
}
}
____________
RainFallApp.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class RainFallApp {
public static void main(String[] args) {
double rain=0.0;
//Creating an array of size 12
double[] rainfall=new double[12];
//DecimalFormat class is used to format the output
DecimalFormat df=new DecimalFormat("#.##");
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
int i=0;
while(i<12)
{
//Getting the monthly rain fall of each month
System.out.print("Enter the rainfall of month "+(i+1)+" :");
rain=sc.nextDouble();
//if the rainfall cvalue is negative then display error message
if(rain<0)
{
System.out.println(" Invalid.Must be positive Number");
continue;
}
else
{
//Populating the user entered value into an array
rainfall[i]=rain;
i++;
}
}
//Creating an object to Rainfall class by passing the array as argument
Rainfall rf=new Rainfall(rainfall);
//Displaying the total rainfall of the year
System.out.println("Total rainfall for the year :"+df.format(rf.totalRainFall()));
//Displaying the average rainfall of the year
System.out.println("The average monthly rainfall :"+df.format(rf.average()));
//Displaying the highest rainfall of the year
System.out.println("The month with the most rain :"+df.format(rf.highestRainfall()));
//Displaying the lowest rainfall of the year
System.out.println("The month with the least rain :"+df.format(rf.lowestRainfall()));
}
}
___________________
Output:
Enter the rainfall of month 1 :12.2
Enter the rainfall of month 2 :10.2
Enter the rainfall of month 3 :8.8
Enter the rainfall of month 4 :9.2
Enter the rainfall of month 5 :6.4
Enter the rainfall of month 6 :10.2
Enter the rainfall of month 7 :14.6
Enter the rainfall of month 8 :16.6
Enter the rainfall of month 9 :12.9
Enter the rainfall of month 10 :14.3
Enter the rainfall of month 11 :10.7
Enter the rainfall of month 12 :9.8
Total rainfall for the year :135.9
The average monthly rainfall :11.33
The month with the most rain :16.6
The month with the least rain :6.4
___________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.