Respuesta :
Answer:
The complete program is given below with step by step comments for explanation.
Explanation:
So we need to write a program that can calculate the average daily temperature and humidity. We can create a class named Weather_Station to do the job.
public class Weather_Station
{
public static void main(String[] args)
{
// we are told that the data is for 10 days and 24 hours so they are fixed and cannot be changed
final int Days = 10;
final int Hours = 24;
// Then we define a three-dimensional array named station for storing days, hours and temperature/humidity
double[][][] station = new double[Days][Hours][2];
// Then we read input from a file
Scanner input = new Scanner(System.in);
// Since there is a lot of data, using a loop to extract the data would be a good idea so we run a loop for 10*24 times to get the day, hour, temperature and humidity values.
for (int k = 0; k < Days*Hours; k++)
{
int day = input.nextInt();
int hour = input.nextInt();
double temp = input.nextDouble();
double humidity = input.nextDouble();
station[day - 1][hour - 1][0] = temp;
station[day - 1][hour - 1][1] = humidity;
}
// Since we want to get the average temperature and humidity of each day for 10 days so we run a loop for days times (thats 10)
for (int i = 0; i < Days; i++)
{
double Total_temp = 0;
double Total_humidity = 0;
// Then we run another loop to add up all the temperatures and humidity values for each hour in a day and store them in Total_temp and Total_humidity respectively.
for (int j = 0; j < Hours; j++)
{
Total_temp += station[i][j][0];
Total_humidity += station[i][j][1];
}
}
// The average is simply the total number of temperature values divided by the number of hours, same goes for humidity.
double Avg_temp=Total_temp / Hours;
double Avg_humidity=Total_humidity / Hours;
// Then we print the values of any day's average temperature and humidity
System.out.println("For the Day of " + i + "Average Temperature is " + Avg_temp);
System.out.println("For the Day of " + i + "Average Humidity is " + Avg_humidity);
}
}