Question

In Java language: Write a rainfall class that stores the total rainfall for each of 12...

In Java language: Write a rainfall class that stores the total rainfall for each of 12 months of years 2015 -2017 into an 2D array of doubles. Use the text file at Actividades/Tareas link as input of rainfall data. The program should read the input data from a text file. The program should have methods that at least return the following: • Total rainfall for the years 2015 – 2017 - return a 1D array • The average monthly rainfall for years 2015-2017 - return a 1D array • The year with the most rain • The year with the least rain • The month with the most rain in a specific year • The month with the least rain in a specific year Demonstrate the class in a complete program.

Example of rainfall data:

2015: 4.96 3.27 1.50 1.38 2.28 2.11 1.61 5.93 4.30 1.77 8.72 3.47
2016: 1.60 3.88 2.22 10.02 7.16 2.74 5.10 5.14 4.91 6.39 17.65 4.30
2017: 1.81 1.70 6.04 6.25 4.53 3.93 6.66 7.68 15.79 6.61 10.16 3.33
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

We have to paste the input file in the project folder

__________________________

// rainfall.txt

2015: 4.96 3.27 1.50 1.38 2.28 2.11 1.61 5.93 4.30 1.77 8.72 3.47
2016: 1.60 3.88 2.22 10.02 7.16 2.74 5.10 5.14 4.91 6.39 17.65 4.30
2017: 1.81 1.70 6.04 6.25 4.53 3.93 6.66 7.68 15.79 6.61 10.16 3.33

_________________________

// ReadFileRainfall.java

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;

public class ReadFileRainfall {

   public static void main(String[] args) throws FileNotFoundException {
       // DecimalFormat class is used to format the output
       DecimalFormat df = new DecimalFormat(".00");
       // Declaring char array
   String months[] = { "January", "February", "March", "April", "May", "June", "July",
   "August", "September", "October", "November", "December" };

       int rows=3,cols=12;
       int cnt=0;
       double rainfall[][]=new double[rows][cols];
       String years[]=new String[rows];
       Scanner sc=new Scanner(new File("rainfall.txt"));
       while(sc.hasNext())
       {
           years[cnt]=sc.next();
           for(int i=0;i<cols;i++)
           {
               rainfall[cnt][i]=sc.nextDouble();
           }
           cnt++;
       }
       sc.close();
      
       double yearsTot[]=totYearsRainfall(rainfall);
       double avgMonthRain[]=avgMonthlyRainFall(rainfall);
       for(int i=0;i<yearsTot.length;i++)
       {
           System.out.println("Total Rainfall in "+years[i]+":"+df.format(yearsTot[i]));
       }
      
       for(int i=0;i<months.length;i++)
       {
           System.out.println("Average rainfall in "+months[i]+":"+df.format(avgMonthRain[i]));
       }
      
       int maxYearIndx=maxRainInYear(yearsTot);
       System.out.println("Year with most rain is :"+years[maxYearIndx]);
       int minYearIndx=minRainInYear(yearsTot);
       System.out.println("Year with least rain is :"+years[minYearIndx]);
       int year=2015;
       int yearIndx=getYearIndx(years,year);
       int maxRainMonIndx=maxRainInYear(rainfall[yearIndx]);
       System.out.println("Maximum rain in year "+year+" in the month "+months[maxRainMonIndx]);
       year=2017;
       yearIndx=getYearIndx(years,year);
       int minRainMonIndx=minRainInYear(rainfall[yearIndx]);
       System.out.println("Minimum rain in year "+year+" in the month "+months[minRainMonIndx]);
   }

   private static int getYearIndx(String[] years, int year) {
       int indx=0;
       for(int i=0;i<years.length;i++)
       {
           if(Integer.parseInt(years[i].substring(0,years[i].length()-1))==year)
           {
               indx=i;
           }
       }
       return indx;
   }


   private static int minRainInYear(double[] tot) {
       double min=tot[0];
       int indx=0;
       for(int i=0;i<tot.length;i++)
       {
           if(min>tot[i])
           {
               min=tot[i];
               indx=i;
           }
       }
       return indx;

   }

   private static int maxRainInYear(double[] tot) {
       double max=tot[0];
       int indx=0;
       for(int i=0;i<tot.length;i++)
       {
           if(max<tot[i])
           {
               max=tot[i];
               indx=i;
           }
       }
       return indx;
   }

   private static double[] avgMonthlyRainFall(double[][] rainfall) {
       double avg[]=new double[rainfall[0].length];
       double sum=0;
       for(int i=0;i<rainfall[0].length;i++)
       {
           sum=0;
           for(int j=0;j<rainfall.length;j++)
           {
               sum+=rainfall[j][i];
           }
           avg[i]=sum/rainfall.length;
       }
       return avg;
   }

   private static double[] totYearsRainfall(double[][] rainfall) {
       double tot[]=new double[rainfall.length];
       double sum=0;
       for(int i=0;i<rainfall.length;i++)
       {
           sum=0;
           for(int j=0;j<rainfall[0].length;j++)
           {
               sum+=rainfall[i][j];
           }
           tot[i]=sum;
       }
       return tot;
   }

}

___________________________________

Output:

Total Rainfall in 2015::41.30
Total Rainfall in 2016::71.11
Total Rainfall in 2017::74.49
Average rainfall in January:2.79
Average rainfall in February:2.95
Average rainfall in March:3.25
Average rainfall in April:5.88
Average rainfall in May:4.66
Average rainfall in June:2.93
Average rainfall in July:4.46
Average rainfall in August:6.25
Average rainfall in September:8.33
Average rainfall in October:4.92
Average rainfall in November:12.18
Average rainfall in December:3.70
Year with most rain is :2017:
Year with least rain is :2015:
Maximum rain in year 2015 in the month November
Minimum rain in year 2017 in the month February

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
In Java language: Write a rainfall class that stores the total rainfall for each of 12...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT