Do not write a class as specified regarding the array. Instead use two one-dimen
ID: 3762164 • Letter: D
Question
Do not write a class as specified regarding the array. Instead use two one-dimensional arrays of size 12 each (parallel arrays) and 4 methods to solve the problem as specified. One array will hold the names of the months and the other array will hold the rainfall in inches for the months. Pass the appropriate array or arrays to the methods as needed. Put String [ ] month names for 12 months.
Write a RainFall class that stores the total rainfall for each of 12 months into an array of double s. 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.
Explanation / Answer
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class RainFall {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double rain[] = new double[12];
String[] monthNames = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December" };
System.out
.println("Please enter in the following rainfall for the months: ");
System.out.println("Month Rainfall (In inches)");
for (int i = 0; i < monthNames.length;) {
System.out.print(monthNames[i] + ": ");
double inVal = scanner.nextDouble();
if (inVal > 0) {
rain[i] = inVal;
i++;
} else {
System.out.println("Please Enter postive numbers only");
}
}
// (Or rain[] = 1,2,3,4,5,6,7,8,9,10,11,12);
double sum = totalRainFall(rain);
double average = sum / (double) rain.length;
System.out.println("The sum of all the rain is: " + sum);
System.out.println("The average rainfall was:" + average + " inches");
int most = getMaxIndex(rain);
int least = getMinIndex(rain);
System.out.println("The month with the most rain was: " + rain[most]
+ " for the month: " + monthNames[most]);
System.out.println("The month with the least rain was: " + rain[least]
+ " for the month: " + monthNames[least]);
}
/**
* @param rain
* @return get total rain fall
*/
public static double totalRainFall(double rain[]) {
double sum = 0.0;
for (int i = 0; i < rain.length; i++) {
sum += rain[i];
}
return sum;
}
/**
* @param rain
* @return getting index of max rain fall
*/
public static int getMaxIndex(double rain[]) {
double largest = rain[0];
int index = 0;
for (int i = 1; i < rain.length; i++) {
if (rain[i] >= largest) {
largest = rain[i];
index = i;
}
}
return index;
}
/**
* @param rain
* @return getting index of min rain fall
*/
public static int getMinIndex(double rain[]) {
double smallest = rain[0];
int index = 0;
for (int i = 1; i < rain.length; i++) {
if (rain[i] <= smallest) {
smallest = rain[i];
index = i;
}
}
return index;
}
}
output:
Please enter in the following rainfall for the months:
Month Rainfall (In inches)
January: 45
February: 89
March: -99
Please Enter postive numbers only
March: 65
April: 45
May: -23
Please Enter postive numbers only
May: 88
June: 21
July: 54
August: 36
September: 12
October: 15
November: 87
December: 78
The sum of all the rain is: 635.0
The average rainfall was:52.916666666666664 inches
The month with the most rain was: 89.0 for the month: February
The month with the least rain was: 12.0 for the month: September
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.