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

Write a program that lets the user enter the total rainfall for each of 12 month

ID: 3695068 • Letter: W

Question

Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Input Validation: Do not accept negative numbers for monthly rainfall figures.

Part 1:

Rewrite Programming Challenge 2. Rainfall Statistics (page 448). Using Vectors and do not use Functions

Remember to use two vectors one for rainfall and one for months.

Extra Credit: make it a dynamic program where the user has flexibility to add any number of months (up to 12) and the months names.

Part 2:

Rewrite Programming Challenge 2. Rainfall Statistics (page 448). Using Vectors and Use Functions i.e Rewrite Part1 of this LAB.

Part 3:

Rewrite Programming Challenge 2. Rainfall Statistics (page 448). Use Range-Based for loop.

Explanation / Answer

import java.util.*;

public class Rainfall
   {
       public static void main(String args[])
           {
               Vector<String> months = new Vector<String>();       //defining two vectors one for months and one for rainfall
               Vector<Double> rainfall = new Vector<Double>();
              
               Scanner s1 = new Scanner(System.in);
               Scanner s2 = new Scanner(System.in);              
              
               for(int i=0;i<12;i++)                               // using a loop to read values into vectors
                   {
                       System.out.println("Enter the month");
                       String str1 = s1.nextLine();
                       months.add(str1);
                      
                       System.out.println("Enter the rainfall for the month of " + str1);
                      
                       double d2 = s2.nextDouble();
                       if(d2<0.0)
                           {
                               System.out.println("Enter only positive Values");
                           }
                       else
                           {
                               rainfall.add(d2);
                           }
                   }
                  
               Enumeration e1 = months.elements();               // enumerations to iterate through vectors
               Enumeration e2 = rainfall.elements();

                double tot_rainfall = 0.0;
                System.out.println("Vector elements are: ");
           while(e2.hasMoreElements())
                   {
               tot_rainfall = tot_rainfall + (double)e2.nextElement();
                   }
                     
               System.out.println("TOTAL RAINFALL IS : " + tot_rainfall);          // display total rainfall
               System.out.println("MAXIMUM RAINFALL MONTH IS : " + months.get(rainfall.indexOf(Collections.max(rainfall))));   // display the month with maximum rainfall. Collections.max returns an object of the type vector whose index in the vector is found by indexOf(). the same index is used against the other vector to get the month name          
               System.out.println("MAXIMUM RAINFALL MONTH IS : " + months.get(rainfall.indexOf(Collections.min(rainfall))));     
               System.out.println("AVERAGE RAINFALL IS : " + tot_rainfall/rainfall.size());                     
           }
   }

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