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

Write a program that uses one struct to store the following weather information

ID: 3769441 • Letter: W

Question

Write a program that uses one struct to store the following weather information for one month:

                        Total rainfall

                        High temperature

                        Low temperature

                        Average temperature (Of High/Low temperature)

Assume the values are floats. The program should declare an array that holds 12 structs. The array can then hold weather information for up to an entire year. When the program runs, it should read the information for the year from a file called weather.dat. Each line of the file (0 to 12 lines are possible) will contain one month’s data for Total rainfall, High temperature, and Low temperature. You can assume all three values will be on the line. A typical line might be as follows:

3.2  88.1  59.3

The average temperature for the month should be calculated by the program and stored in the array with the other monthly values as the data is input. The values in the input file will be separated by blanks.

Once the data is read for all the months, the program should prompt the user to select a month (1 = Jan, 2 = Feb, etc.), and then display the Total rainfall, High temperature, Low temperature, and Average temperature for that month. If the user selects 0, the program should calculate and display the total rainfall for the year along with the average rainfall for the year. If the user selects an invalid month, the program should display a helpful error message. The program should allow the user to repeat the month selection until they are ready to exit. Remember to allow a sentinel menu option to exit. Label all output clearly and format it neatly. Use functions to modularize your program.  A good programmer will check to be sure the program handles an empty file and the last record in the input file correctly, and calculates yearly values correctly when the number of months input is not 12.

also add comments and make sure to use data from file called weather.dat

Explanation / Answer

package weatherinfo;
import java.util.*;
import java.io.*;

class weather {
    public double totRainFall;
    public double HiTemp;
    public double loTemp;
    public double avgTemp;
  
    public void disp() {
        System.out.println("Total rain fall " + totRainFall + " Low Temperature: " + loTemp + " High Temperature: " + HiTemp + " Average Temperature: " + ( ( loTemp + HiTemp) / 2 ) ) ;
    }
  
}; // end weather class

public class WeatherInfo {
  
    public static void main(String[] args) throws FileNotFoundException, IOException {
        weather jan = new weather();
        weather feb = new weather();
        weather mar = new weather();
        weather apr = new weather();
        weather may = new weather();
        weather jun = new weather();
        weather july = new weather();
        weather aug = new weather();
        weather sep = new weather();
        weather oct = new weather();
        weather nov = new weather();
        weather dec = new weather();

        weather [] wa = new weather[12]; // wa = weather array
        InputStream is = new FileInputStream("E:/Files/weather.txt");
        //FileInputStream fis = new FileInputStream("E:/Files/weather.txt");
      
        double totRain=0.0, hiTemp=0.0, loTemp=0.0;
      
        DataInputStream dis1 = new DataInputStream(is);
      
        int i;
       for (i = 1; i <= 12; i++)    {
            totRain = dis1.readDouble();     // readDouble();
            hiTemp = dis1.readDouble();
            loTemp = dis1.readDouble();
            wa[i].totRainFall = totRain;
            wa[i].HiTemp = hiTemp;
            wa[i].loTemp = loTemp;
            wa[i].avgTemp = (hiTemp + loTemp ) / 2;
       } // end for
       for (i = 1; i <= 12; i++)    {
           System.out.print(wa[i].totRainFall + " , " + wa[i].HiTemp + " , " + wa[i].loTemp + " , " + wa[i].avgTemp);
       }
        System.out.println("Data from the file weather: " + totRain + " , " + hiTemp + " , " + loTemp + " Average Temp " + ( ( loTemp + hiTemp ) / 2 ) ) ;
       int repeat = 1;
while(repeat == 1)   {
    System.out.println("Select Month:");
    System.out.println(" 1 = January , 2 = February   ,   3 = March");
    System.out.println(" 4 = April    , 5 = May        ,   6 = June");
    System.out.println(" 7 = July     , 8 = August     ,   9 = September");
    System.out.println(" 10 = October , 11 = November   , 12 = December");
    Scanner sc = new Scanner(System.in);
    int choice = sc.nextInt();
    double sum=0.0, yearAvg=0.0;
    if (choice == 0 ) {
        for (i = 1; i<= 12; i++)
            sum = sum + wa[i].totRainFall;
        yearAvg = sum / 12.0;
        System.out.println("Yearly Total Rainfall = " + sum + "Yearly Average = " + yearAvg);
    } // end if
         //yearlyAvg =
    System.out.println("Like to continue? 1 = yes, 0 = no");
} // end while
    jan.disp();
    feb.disp();
    mar.disp();
  
  
  
  
    } // end main
  
} // end public class

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