Thursday 13 September 2012

Java Practical - 3




Program Definition 3 ::


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 temperatures for the four weeks.
 


class Statistics
{
public static double average(double arr[])
{
double total=0.0;
for(double d : arr)
{
total = total + d ;
}
return (total/arr.length);
}
}

class Prog9
{
public static void main(String args[])
{
double tempWeek[][] = {{32.0,35.7,40.2,39.9,34.2,31.2,44.4},{34.2,31.2,44.4,33.3,34.2,31.2,44.4},{30.8,31.0,35.5,31.0,34.2,31.2,44.4},{34.2,39.5,42.8,32.9,34.2,31.2,44.4}};

for(int i = 0 ; i < tempWeek.length ; i++)
{
System.out.println("week " + (i+1) + "| Average temperature is :: " + Statistics.average(tempWeek[i]));
}
}
}

/*
D:\JAVA>javac Prog9.java

D:\JAVA>java Prog9
week 1| Average temperature is :: 36.8
week 2| Average temperature is :: 36.128571428571426
week 3| Average temperature is :: 34.01428571428571
week 4| Average temperature is :: 37.028571428571425
*/


Kindly Bookmark and Share it:

0 comments :

Post a Comment

Any Query ? any suggestion ? comment here

 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT