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

IN JAVA: Write a program that calculates the total rainfall, the highest monthly

ID: 3817030 • Letter: I

Question

IN JAVA:

Write a program that calculates the total rainfall, the highest monthly rainfall, and the lowest monthly rainfall of Georgia for the last year. The loop will iterate 12 times, once for each month. During each iteration, ask the user to enter the inches of rainfall for that month. After all iterations, the program displays the total inches of rainfall, the highest monthly rainfall, and the lowest monthly rainfall of Georgia for the last year.

Input validation: Do not accept negative numbers for the monthly rainfall.

Explanation / Answer

RainfallTest.java


import java.util.Arrays;
import java.util.Scanner;

public class RainfallTest {

  
   static String month[] = {"January", "Fabruary", "March", "April", "May", "June", "July", "August", "September","October","November","December"};
  
   public static void main(String[] args) {
           double monthlyRainfall[] = new double[12];
           Scanner scan = new Scanner(System.in);
           for(int i=0; i<monthlyRainfall.length; i++){
               System.out.print("Enter rainfall amount (in inches) for "+month[i]+":");
               monthlyRainfall[i] = scan.nextDouble();
               if(monthlyRainfall[i]<0){
                   System.out.print("Invalid Input. ");
                   i--;
               }
           }
           System.out.println("Entered rainfall details "+Arrays.toString(monthlyRainfall));
               displayTotalRainFall(monthlyRainfall);
               displayMaximumRainFall(monthlyRainfall);
               displayMinimumRainFall(monthlyRainfall);
              
          
   }
  
   public static void displayTotalRainFall(double months[]){
       double total = 0;
       for(int i=0; i<months.length; i++){
           total = total + months[i];
       }
       System.out.printf("Total Rainfall for the Year (in inches): %.2f ",total);
   }

   public static void displayMaximumRainFall(double months[]){
       double max = 0;
       for(int i=0; i<months.length; i++){
           if(max < months[i]){
               max = months[i];
           }
       }
       System.out.println("Maximum Monthly Rainfall (in inches): "+max);
   }
   public static void displayMinimumRainFall(double months[]){
       double min = months[0];
       for(int i=0; i<months.length; i++){
           if(min > months[i]){
               min = months[i];
           }
       }
       System.out.println("Minimum Monthly Rainfall (in inches): "+min);
   }
}

Output:

Enter rainfall amount (in inches) for January:11
Enter rainfall amount (in inches) for Fabruary:12
Enter rainfall amount (in inches) for March:13
Enter rainfall amount (in inches) for April:45
Enter rainfall amount (in inches) for May:56
Enter rainfall amount (in inches) for June:-66
Invalid Input. Enter rainfall amount (in inches) for June:-1
Invalid Input. Enter rainfall amount (in inches) for June:44
Enter rainfall amount (in inches) for July:33
Enter rainfall amount (in inches) for August:66
Enter rainfall amount (in inches) for September:77
Enter rainfall amount (in inches) for October:88
Enter rainfall amount (in inches) for November:99
Enter rainfall amount (in inches) for December:100
Entered rainfall details [11.0, 12.0, 13.0, 45.0, 56.0, 44.0, 33.0, 66.0, 77.0, 88.0, 99.0, 100.0]
Total Rainfall for the Year (in inches): 644.00
Maximum Monthly Rainfall (in inches): 100.0
Minimum Monthly Rainfall (in inches): 11.0