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

Instructions/Design: Below is a partially written program named Rainfall.java th

ID: 3642602 • Letter: I

Question

Instructions/Design:
Below is a partially written program named Rainfall.java that stores the total rainfall for each of 12 months into an array of doubles and then produces output about the rainfall data. It is your job to complete the program:

1) The program should have the following 4 methods that return values:
the total rainfall for the year: totalRain (returns a double)
the average monthly rainfall: averageRain (returns a double)
the month with the most rain: mostRain (returns an int - the index of the array)
the month with the least rain: leastRain(returns an int - an index of the array)
2) In addition, the program uses (and you must write) a method that prompts the user and then stores input into the array. This method does not return any values. Name the method: initRain

3) Note: Each of the five methods you will write is public static and each takes the array as an argument.

4) The output should display only one digit after the decimal place.

You must write five methods for the program.
You must use an array parameter in the methods.

Sample Output:
Here is first run of program (program output in italics and user input in bold/italics ):

Enter rain fall for month 1: 10.1
Enter rain fall for month 2: 11.2
Enter rain fall for month 3: 12.3
Enter rain fall for month 4: 13.4
Enter rain fall for month 5: 14.5
Enter rain fall for month 6: 15.6
Enter rain fall for month 7: 5.1
Enter rain fall for month 8: 6.2
Enter rain fall for month 9: 7.3
Enter rain fall for month 10: 8.4
Enter rain fall for month 11: 9.5
Enter rain fall for month 12: 10.0
The total rainfall for this year is 123.6
The average rainfall for this year is 10.2
The month with the highest amount of rain is 6.
The month with the lowest amount of rain is 7.

You create your own data for second run.

Explanation / Answer

Please rate...

import java.util.Scanner;
class Rainfall
{
    public static double totalRain(double a[])
    {
        int i;
        double s=0;
        for(i=0;i<a.length;i++)
        {
            s=s+a[i];
        }
        return s;
    }
    public static double averageRain(double a[])
    {
        int i;
        double s=0;
        for(i=0;i<a.length;i++)
        {
            s=s+a[i];
        }
        return (s/12);
    }
    public static int mostRain(double a[])
    {
        int i,c=-1;
        double b=a[0];
        for(i=0;i<a.length;i++)
        {
            if(a[i]>b)
            {
                b=a[i];
                c=i;
            }
        }
        return c;
    }
    public static int leastRain(double a[])
    {
        int i,c=-1;
        double b=a[0];
        for(i=0;i<a.length;i++)
        {
            if(a[i]<b)
            {
                b=a[i];
                c=i;
            }
        }
        return c;
    }
    public static void initRain(double a[])
    {
        int i;
        Scanner s=new Scanner(System.in);
        for(i=0;i<a.length;i++)
        {
            System.out.println("Enter rain fall for month "+(i+1)+" : ");
            a[i]=s.nextDouble();
        }
    }
    public static void main(String args[])
    {
        double a[]=new double[12];
        initRain(a);
        System.out.format("The total rain fall for this year is %.1f%n",totalRain(a));
        System.out.format("The average rain fall for this year is %.1f%n",averageRain(a));
        System.out.format("The month with the highest amount of rain is %d%n",(mostRain(a)+1));
        System.out.format("The month with the least amount of rain is %d",(leastRain(a)+1));
    }
}