Write a RainFall class that has the following field: • an array of doubles that
ID: 3665789 • 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.
OUTPUT:
My Code:
import java.util.Scanner;
public class RainFall {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
final int SIZE = 12;
double[] monthlyRain = new double[SIZE];
System.out.println("Enter the monthly rain fall amount: ");
int i = 0;
// Added input validation with 'Do-While Loop'
for(i = 0; i < SIZE; i++){
do{
System.out.print("Month " + (i+1) + ": ");
monthlyRain[i] = keyboard.nextDouble();
}
while(monthlyRain[i] < 0);
}
System.out.println("The annual sum of rain: " + sumRain(monthlyRain));
System.out.println("The average sum of rain: " + averageRain(monthlyRain));
System.out.println("The most amount of rain fall: " + mostRain(monthlyRain));
System.out.println("The least amount of rain fall: " + leastRain(monthlyRain));
}
// Create a function that returns the total rainFall for the year.
static double sumRain(double[] array){
double sum = 0;
for(int i = 0; i < array.length; i++){
sum += array[i];
}
return sum;
}
// Create a function that returns the average monthly rainFall for the year.
static double averageRain(double[] array){
double average = 0;
average = sumRain(array)/array.length;
return average;
}
// Create a function that returns the month with the most rain.
static double mostRain(double[] array){
double most = 0;
for (int i = 0; i < array.length; i++){
if(array[i] > most){
most = array[i];
}
}
return most;
}
// Create a function that returns the month with the least rain.
static double leastRain(double[] array){
double least = array[0];
for(int i = 0; i < array.length; i++){
if(array[i] < least){
least = array[i];
}
}
return least;
}
}
Explanation / Answer
Program:
import java.util.Scanner;
public class RainFall {
private static Scanner keyboard;
private static double leastRain;
public static void main(String[] args) {
keyboard = new Scanner(System.in);
final int SIZE = 12;
double[] monthlyRain = new double[SIZE];
String []months={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
System.out.println("Enter the monthly rain fall amount: ");
int i = 0;
// Added input validation with 'Do-While Loop'
for(i = 0; i < SIZE; i++){
do{
System.out.print("Month " + (i+1) + ": ");
monthlyRain[i] = keyboard.nextDouble();
}
while(monthlyRain[i] < 0);
}
System.out.println("The annual sum of rain: " + sumRain(monthlyRain));
System.out.println("The average sum of rain: " + averageRain(monthlyRain));
double mostRain=mostRain(monthlyRain);
leastRain = leastRain(monthlyRain);
System.out.print("The most amount of rain fall: ");
for(i=0;i<12;i++)
{
if(monthlyRain[i]==mostRain)
System.out.print(" "+months[i]);
}
System.out.print(" The least amount of rain fall: ");
for(i=0;i<12;i++)
{
if(monthlyRain[i]==leastRain)
System.out.print(" "+months[i]);
}
}
// Create a function that returns the total rainFall for the year.
static double sumRain(double[] array){
double sum = 0;
for(int i = 0; i < array.length; i++){
sum += array[i];
}
return sum;
}
// Create a function that returns the average monthly rainFall for the year.
static double averageRain(double[] array){
double average = 0;
average = sumRain(array)/array.length;
return average;
}
// Create a function that returns the month with the most rain.
static double mostRain(double[] array){
double most = 0;
for (int i = 0; i < array.length; i++){
if(array[i] > most){
most = array[i];
}
}
return most;
}
// Create a function that returns the month with the least rain.
static double leastRain(double[] array){
double least = array[0];
for(int i = 0; i < array.length; i++){
if(array[i] < least){
least = array[i];
}
}
return least;
}
}
Result:
Enter the monthly rain fall amount:
Month 1: 1.12
Month 2: 2.24
Month 3: 3.36
Month 4: 0.48
Month 5: 2.0
Month 6: 5.0
Month 7: 12.12
Month 8: 1.48
Month 9: 3.12
Month 10: 3.24
Month 11: 6.6
Month 12: 1.12
The annual sum of rain: 41.88
The average sum of rain: 3.49
The most amount of rain fall: July
The least amount of rain fall: April
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.