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: 3735204 • 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

RainFallTest.java

import java.util.Scanner;

public class RainFallTest {

static String monthStr[] = {"January", "Fabruary", "March", "April", "May", "June", "July", "August", "September","October","November","December"};

public static void main(String[] args) {

double month[] = new double[12];

Scanner scan = new Scanner(System.in);

for(int i=0; i<month.length; i++){

System.out.println("Enter Rainfall for the month "+monthStr[i]+": ");

month[i] = scan.nextDouble();

if(month[i]<0){

i--;

}

}

RainFall r = new RainFall(month);

r.displayTotalRainFall();

r.displayAverageRainFall();

r.displayMaximumRainFall();

r.displayMinimumRainFall();

}

}

RainFall.java

public class RainFall {

private double months[];

String monthStr[] = {"January", "Fabruary", "March", "April", "May", "June", "July", "August", "September","October","November","December"};

public RainFall(double d[]){

months = d;

}

public void displayTotalRainFall(){

double total = 0;

for(int i=0; i<months.length; i++){

total = total + months[i];

}

System.out.println("The total rainfall is :"+total);

}

public void displayAverageRainFall(){

double total = 0;

double avg = 0;

for(int i=0; i<months.length; i++){

total = total + months[i];

}

avg = total/(double)months.length;

System.out.println("The average rainfall is :"+avg);

}

public void displayMaximumRainFall(){

double max = 0;

int maxIndex = 0;

for(int i=0; i<months.length; i++){

if(max < months[i]){

max = months[i];

maxIndex = i;

}

}

System.out.println("The maximum rainfall is :"+max);

System.out.println("The maximum rainfall month is :"+monthStr[maxIndex]);

}

public void displayMinimumRainFall(){

double min = months[0];

int minIndex = 0;

for(int i=0; i<months.length; i++){

if(min > months[i]){

min = months[i];

minIndex = i;

}

}

System.out.println("The minimum rainfall is :"+min);

System.out.println("The minimum rainfall month is :"+monthStr[minIndex]);

}

}

Output:

Enter Rainfall for the month January:
22
Enter Rainfall for the month Fabruary:
33
Enter Rainfall for the month March:
44
Enter Rainfall for the month April:
55
Enter Rainfall for the month May:
66
Enter Rainfall for the month June:
11
Enter Rainfall for the month July:
99
Enter Rainfall for the month August:
100
Enter Rainfall for the month September:
88
Enter Rainfall for the month October:
77
Enter Rainfall for the month November:
66
Enter Rainfall for the month December:
55
The total rainfall is :716.0
The average rainfall is :59.666666666666664
The maximum rainfall is :100.0
The maximum rainfall month is :August
The minimum rainfall is :11.0
The minimum rainfall month is :June

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