Java Program to compute and print the average temperatues for the four weeks
Write a class called Statistics, which has a static method called average, which takes a one-
dimensional array for double type, as parameter, and prints the average for the values in the
array. Now write a class with the main method, which creates a two-dimensional array for the
four weeks of a month, containing minimum temperatures for the days of the week(an array of
4 by 7), and uses the average method of the Statistics class to compute and print the average
temperatues for the four weeks.
Program:
public class q5
{
public static void main(String args[])
{
int i;
double arr[] = {23, 56, 78, 89, 12, 34, 56};
//Statistics sc = new Statistics();
//sc.average(arr);
//double b = sc.average(arr);
Statistics.average(arr);
//System.out.println(b);
//int [][] arr2 = new int[4][7];
double arr2[][]={{1,2,3,4,5,6,7}, {23, 56, 78, 89, 12, 34, 56}, {23, 56, 78, 89, 12, 34, 56}, {23, 56, 78, 89, 12, 34, 56}};
System.out.println("Average temperatures of each week: ");
for(i=0; i<4; i++)
{
Statistics.average(arr2[i]);
}
}
}
class Statistics
{
static void average(double arr[])
{
double sum = 0;
for(int i =0; i<7; i++)
{
sum = sum + arr[i];
}
double avg = sum/7;
//return avg;
System.out.println(avg);
}
}
dimensional array for double type, as parameter, and prints the average for the values in the
array. Now write a class with the main method, which creates a two-dimensional array for the
four weeks of a month, containing minimum temperatures for the days of the week(an array of
4 by 7), and uses the average method of the Statistics class to compute and print the average
temperatues for the four weeks.
Program:
public class q5
{
public static void main(String args[])
{
int i;
double arr[] = {23, 56, 78, 89, 12, 34, 56};
//Statistics sc = new Statistics();
//sc.average(arr);
//double b = sc.average(arr);
Statistics.average(arr);
//System.out.println(b);
//int [][] arr2 = new int[4][7];
double arr2[][]={{1,2,3,4,5,6,7}, {23, 56, 78, 89, 12, 34, 56}, {23, 56, 78, 89, 12, 34, 56}, {23, 56, 78, 89, 12, 34, 56}};
System.out.println("Average temperatures of each week: ");
for(i=0; i<4; i++)
{
Statistics.average(arr2[i]);
}
}
}
class Statistics
{
static void average(double arr[])
{
double sum = 0;
for(int i =0; i<7; i++)
{
sum = sum + arr[i];
}
double avg = sum/7;
//return avg;
System.out.println(avg);
}
}
Comments
Post a Comment