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

Write a program that uses a 2-D array to store the highest and lowest temperatur

ID: 3555920 • Letter: W

Question

Write a program that uses a 2-D array to store the highest and lowest temperatures for each montjh of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods with their appropriate parameters:

a) getData - This method reads and stores the data in the 2-D array.

b) averageHigh - This method calculates and returns the average high temp of the year

c) averageLow - This method calculates and returns the average low temp of the year

d) indexHighTemp - This method returns the index of the highest temp in the array

e) indexLowTemp - This method returns the index of the lowest temp in the array

Explanation / Answer

import java.util.*;
//Write a program that uses a 2-D array to store the highest and lowest temperatures for each montjh of the year.
//The program should output the average high, average low, and highest and lowest temperatures of the year.
//Your program must consist of the following methods with their appropriate parameters:

public class temp_recorder
{
//a) getData - This method reads and stores the data in the 2-D array.
public static void getData(double[][] temp_array)
{
Scanner in = new Scanner(System.in);
for(int i=0; i<temp_array.length; i++)
    {
       System.out.println("Enter Highest Temperature for month "+ (i+1) + " : ");
       temp_array[i][0] = in.nextDouble();
       System.out.println("Enter Lowest Temperature for month "+ (i+1) + " : ");
       temp_array[i][1] = in.nextDouble();
   }
}
//b) averageHigh - This method calculates and returns the average high temp of the year
public static double averageHigh(double[][] temp_array)
{
double sum = 0;
for(int i=0; i<temp_array.length; i++)
sum = sum + temp_array[i][0];
return sum/temp_array.length;
}
//c) averageLow - This method calculates and returns the average low temp of the year
public static double averageLow(double[][] temp_array)
{
double sum = 0;
for(int i=0; i<temp_array.length; i++)
sum = sum + temp_array[i][1];
return sum/temp_array.length;
}
//d) indexHighTemp - This method returns the index of the highest temp in the array
public static int indexHighTemp(double[][] temp_array)
{
int high_index = 0;
for(int i=1; i<temp_array.length; i++)
{
if(temp_array[i][0] > temp_array[high_index][0]) high_index=i;
}
return high_index;
}
//e) indexLowTemp - This method returns the index of the lowest temp in the array
public static int indexLowTemp(double[][] temp_array)
{
int low_index = 0;
for(int i=1; i<temp_array.length; i++)
{
if(temp_array[i][1] < temp_array[low_index][1]) low_index=i;
}
return low_index;
}
public static void main(String[] args)
{
double[][] temp_array = new double[12][2];
getData(temp_array);
System.out.println("Average High temperature for year given by "+averageHigh(temp_array));
System.out.println("Average Low temperature for year given by "+averageLow(temp_array));
System.out.println("Highest temperature for year given by "+(temp_array[indexHighTemp(temp_array)][0]));
System.out.println("Lowest temperature for year given by "+(temp_array[indexLowTemp(temp_array)][1]));
}
}

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