1 Rainfall Class Write a RainFall class that stores the tota

1. Rainfall Class
Write a RainFall class that stores the total rainfall for each of 12 months into an array of
doubles. 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
Demonstrate the class in a complete program.
Input Validation: Do not accept negative numbers for monthly rainfall figures.

Solution

//RainFall.java

import java.util.Scanner;
public class RainFall
{
   //returns the total RainFall for the year.
   public static double totalRainFall(double[] rain)
   {
       double total = 0;
       for(int i = 0; i < rain.length; i++)
       {
           total += rain[i];
       }
       return total;
   }
  
   // returns the average monthly RainFall for the year.
   public static double averageRainfall(double[] rain)
   {
       double average = totalRainFall(rain)/rain.length;
       return average;
   }
  
   // returns the month with the most rain.
   public static int mostMonthRain(double[] rain)
   {
       int highest = 0;
       for (int i = 1; i < rain.length; i++)
       {
           if(rain[i] > rain[highest])
           {
               highest = i;
           }
       }
       return highest+1;
   }
  
   // returns the month with the least rain.
   public static int leastMonthRain(double[] rain)
   {
       int lowest = 0;
       for(int i = 0; i < rain.length; i++)
       {
           if(rain[i] < rain[lowest])
           {
               lowest = i;
           }
       }
       return lowest+1;
   }


   public static void main(String[] args)
   {
       int rain_SIZE = 12;
       Scanner scan = new Scanner(System.in);
      
       double[] rain = new double[rain_SIZE];
      
       for(int i = 0; i < rain_SIZE; i++)
       {
           while(true)
           {      
               System.out.print(\"Enter rainfall amount for Month \" + (i+1) + \": \");
               rain[i] = scan.nextDouble();

               if(rain[i] > 0)
                   break;
               else
                   System.out.println(\"Invalid Input\");
           }
          
       }      
  
       System.out.println(\"\ The total rainfall for the year: \" + totalRainFall(rain));
       System.out.println(\"The average monthly rainfall: \" + averageRainfall(rain));
       System.out.println(\"The month with the most rain: \" + mostMonthRain(rain) + \"\\tRain: \" + rain[mostMonthRain(rain)-1]);
       System.out.println(\"The month with the least rain: \" + leastMonthRain(rain) + \"\\Rain: \" + rain[leastMonthRain(rain)-1]);
   }      
              
}


/*
output:

Enter rainfall amount for Month 1: 23
Enter rainfall amount for Month 2: 43
Enter rainfall amount for Month 3: 21
Enter rainfall amount for Month 4: 45
Enter rainfall amount for Month 5: 65
Enter rainfall amount for Month 6: 87
Enter rainfall amount for Month 7: 65
Enter rainfall amount for Month 8: 44
Enter rainfall amount for Month 9: 34
Enter rainfall amount for Month 10: 67
Enter rainfall amount for Month 11: 66
Enter rainfall amount for Month 12: 32

The total rainfall for the year: 592.0
The average monthly rainfall: 49.333333333333336
The month with the most rain: 6       Rain: 87.0
The month with the least rain: 3   Rain: 21.0

*/

1. Rainfall Class Write a RainFall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that ret
1. Rainfall Class Write a RainFall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that ret

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site