Home > Back-end >  trying to correct "getAverageRainfall" to display the average of input using "getTota
trying to correct "getAverageRainfall" to display the average of input using "getTota

Time:12-03

Specifications: Write a program that stores the total rainfall for each of the 12 months in an array. The program should have the following separate methods:

getRainfall – takes an array as an argument and reads the amount of rainfall in inches for each of the 12 months from the user and stores the values in the array. The method cannot accept a negative number from the user.

displayRainfall – takes the array containing the inches of rainfall per month as an argument and displays the rainfall for each month.

getTotalRainfall – takes the array containing the inches of rainfall per month as an argument and returns the total number of inches of rainfall for the year.

getAverageRainfall – takes the array containing the inches of rainfall per month as an argument and returns the average number of inches per month.

getRainfallAbove – takes the array containing the inches of rainfall per month and a number as arguments and returns the number of months that had rainfall exceeded the number argument.

Declare the array in the main method and call each of separate methods and output the results in main to demonstrate the working state of the entire program.

My code:



        public static final int MONTHS = 12;
    static double[] rain = new double[MONTHS]; //Crate an array to hold the rain values
    
    public static void main(String[] args) throws IOException {
        
        
        
        rain = getRainfall(args); 
        displayRainfall(rain);
        getTotalRainfall(rain); 
        getAverageRainfall(rain);

   }
   
       public static double[] getRainfall(String[] args) throws IOException {
            String[] months = { "January", "February", "March", "April", "May", "June", "July",
                                "August", "September", "October", "November", "December" }; //Each month in a year

               //Crate an array to hold the rain values
               double[] rain = new double[12];

               //Create a Scanner object for keyboard input.
               Scanner keyboard = new Scanner(System.in);
               
               //Get rain values and store them in the rain array
               for (int i = 0; i < months.length; i  )
                   {          
                   System.out.print("Enter rainfall for "   months[(i)]   ": ");
                   rain[i] = keyboard.nextDouble();
                   if (rain[i] <0)
                   {
                       System.out.println("You can not use a negative number.\n");
                       i--;
                   }
               }
         return rain;
        }
       
        private static void displayRainfall(double[] rain) {
            String[] months = { "January", "February", "March", "April", "May", "June", "July",
                    "August", "September", "October", "November", "December" }; //Each month in a year
            
            System.out.println("\nDisplaying rainfall for each month\n");
            for(int i = 0; i < months.length; i  ) {
                   System.out.print("Rainfall for "   months[i]   ": "   rain[i]   "\n");
            } 
        }
            
                private static double getTotalRainfall(double[] rain) {
                    {
                        double total = 0.0; //Accumulator

                        //Get sum of all values in the rain array.
                        for (double value: rain)
                            total  = value;
                        System.out.printf("\nThe total rainfall for the year is: "   total);

                        return total;
                    }
                }
                    private static double getAverageRainfall(double[] rain) {
                        {
                            double average = 0.0;
                            average  = getTotalRainfall(rain)/ MONTHS;
                            return average;
                        }

Output:


Rainfall for April: 1.0
Rainfall for May: 1.0
Rainfall for June: 1.0
Rainfall for July: 1.0
Rainfall for August: 1.0
Rainfall for September: 1.0
Rainfall for October: 1.0
Rainfall for November: 1.0
Rainfall for December: 1.0

The total rainfall for the year is: 12.0
The total rainfall for the year is: 12.0

CodePudding user response:

You got to return rain and use it in the display method

   public static void main(String[] args) throws IOException {     
         double[] rain = getRainfall(args); 
         displayRainfall(rain);     
   }
    
        public static void getRainfall(String[] args) throws IOException {
             String[] months = { "January", "February", "March", "April", "May", "June", "July",
                                 "August", "September", "October", "November", "December" }; //Each month in a year

            //Crate an array to hold the rain values
            double[] rain = new double[12];

                //Create a Scanner object for keyboard input.
                Scanner keyboard = new Scanner(System.in);
                
                //Get rain values and store them in the rain array
                for (int i = 0; i < months.length; i  )
                    {          
                    System.out.print("Enter rainfall for "   months[(i)]   ": ");
                    rain[i] = keyboard.nextDouble();
                    if (rain[i] <0)
                    {
                        System.out.println("You can not use a negative number.\n");
                        i--;
                    }
                }
          return rain;
        }
    private static void displayRainfall(double[] rain) {
       String[] months = { "January", "February", "March", "April", "May", "June", "July",
                         "August", "September", "October", "November", "December" }; //Each month in a year
                 
       System.out.println("\nDisplaying rainfall for each month\n");
       for(int i = 0; i < months.length; i  ) {
            System.out.print("Rainfall for "   months[i]   ": "   rain[i]   "\n,  ");
       }
  }
  • Related