Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a RainFall class that has the following field: • an array of doubles that

ID: 3766004 • Letter: W

Question

Write a RainFall class that has the following field: • an array of doubles that stores the rainfall for each of the 12 months of the year (where the first index corresponds with January, the second with February, etc.) The class should also have the following methods : • a method that returns the total rainfall for the entire year • a method that returns the average monthly rainfall for the year • a method that returns the month with the most rain as a string • a method that returns the month with the least rain as a string Demonstrate the class in a program that takes 12 doubles from the user (take the doubles in the order of the months of the year, the first corresponding to the rainfall in January, etc.). Do input validation: if the user inputs a negative number, ignore it and continue asking them for input until you have 12 nonnegative doubles . Once the user has given you all 12 doubles , create an instance of the RainFall class and call its methods , printing out the total rainfall, the average monthly rainfall, the month with the most rain, and the month with the least rain, each on a separate line.

Explanation / Answer

/**
* The java class RainFall that has constructor that takes
* an array of double type . The class has metods to find
* total rain fall, average rain fall, month of maximum rain fall
* and month of minimum rain fall
* */
//RainFall.java
public class RainFall
{
   //an array of string of 12 month names
   private String monthNames[]=
       {"January","February","March",
               "April","May","June",
               "July","August",
               "September",
               "October","November",
       "December"};

   //instance variables of class
   private final int MONTHS = 12;
   //an array to hold 12 rainfall
   private double[] months=new double[MONTHS];

   //Constructor that takes an array of type double
   public RainFall(double[] months)
   {
       this.months=months;
   }

   //Returns name of month of maximum rain fall
   public String getMaxRainfallMonth()
   {
       //Assume first month is largest rain fall month      
       double largest = months[ 0 ];
       //Set name of starting month, january
       String maxMonth=monthNames[0];

       for (int i = 1; i < months.length; i++)
       {
           if (months[ i ] > largest)
           {
               largest=i;
               //set month name of correspoding i
               maxMonth=monthNames[i];
           }
       }
       //returns maxMonth
       return maxMonth;
   }

   //Returns name of month of minimum rain fall
   public String getMinRainfallMonth()
   {
       //Assume first month is minimum rain fall month      
       double leastRainfall = months[ 0 ];
       //Set name of starting month, january
       String minMonth=monthNames[0];

       for (int i = 1; i < months.length; i++)
       {
           if (months[ i ] < leastRainfall)
           {
               leastRainfall=i;
               //set month name of correspoding i
               minMonth=monthNames[i];
           }
       }
       //returns minimum rain fall month
       return minMonth;
   }


   //Returns total of rain fall
   public double getTotal()
   {
       double total = 0;
       for (int i = 0; i < months.length; i++)
           total += months[ i ];

       return total;
   }

   //Returns average of rain fall
   public double getAverage()
   {
       return getTotal() / months.length;
   }
  
}//End of RainFall

------------------------------------------------------------------------------------------------


//Tester program of rain fall
//RainFallTester.java
import java.util.Scanner;
public class RainFallTester
{
   public static void main(String[ ] args)
   {
       //Create a Scanner class object
       Scanner scanner = new Scanner(System.in);
       //an array of type double
       double[] rainarray=new double[12];
       //set rain to zero
       double rain=0;
      
       //Read 12 double values and check of non-negative values
       for (int month = 0; month < rainarray.length; month++)
       {
           //validation of input rain fall
           do
           {
               System.out.println("Enter rainfall of month ["+(month+1)+"] :");
               //read rain fall
               rain=scanner.nextDouble();              
           }while(rain<0);          
           //Set rain to rainarray at month index
           rainarray[month]=rain;
       }
              
       //Create an instance of class RainFall with double
       //array
        RainFall rainfall = new RainFall(rainarray);
      
        //Call getTotal method
        System.out.println("Total rainfall in a year: "
                               + rainfall.getTotal());
      //Call getAverage method
        System.out.println("Average rainfall in a year: "
                               + rainfall.getAverage());
      //Call getMaxRainfallMonth method
        System.out.println("Maximum rainfall in a year: "
                               + rainfall.getMaxRainfallMonth());
      //Call getMinRainfallMonth method
        System.out.println("Minimum rainfall in a year: "
                               + rainfall.getMinRainfallMonth());      
     } //end of main method
}//end of the class


----------------------------------------------------------------------------

Sample Output:

Enter rainfall of month [1] :
20
Enter rainfall of month [2] :
11
Enter rainfall of month [3] :
24
Enter rainfall of month [4] :
32
Enter rainfall of month [5] :
15
Enter rainfall of month [6] :
23
Enter rainfall of month [7] :
45
Enter rainfall of month [8] :
65
Enter rainfall of month [9] :
7
Enter rainfall of month [10] :
15
Enter rainfall of month [11] :
12
Enter rainfall of month [12] :
61
Total rainfall in a year: 330.0
Average rainfall in a year: 27.5
Maximum rainfall in a year: December
Minimum rainfall in a year: February

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote