Harmonic Mean If it takes one hose 12 hours to fi ll a pool, and another hose 4
ID: 3809948 • Letter: H
Question
Harmonic Mean If it takes one hose 12 hours to fi ll a pool, and another hose 4 hours, then together they fi ll the pool in (2 * 4 * 12) / (4 +12) 6 hours. The harmonic mean of two positive numbers a and b is 2 ab/ ( a+ b ). Write a method double harmonicMean(int x, int y) that returns the harmonic mean of a > 0 and b > 0 . Write another method that returns the arithmetic mean of a and b , that is, the average of a and b . Finally, include a third method that returns the geometric mean of a and b , that is, the square root of a * b . Test your methods in a program that reads two positive integers and displays their harmonic mean, arithmetic mean, and geometric mean. For example, if a and b have values 12 and 4, the harmonic mean is 6.0, the arithmetic mean is 8.0, and the geometric mean is 48 ~ 6.928. Did you notice that the harmonic mean times the arithmetic mean equals the square of the geometric mean? This identity might be helpful to you when you design your methods.
Explanation / Answer
public class Means {
public static void main(String args[])
{
System.out.println(harmonicMean(12,4));
System.out.println(average(12,4));
System.out.println(geometricMean(12,4));
}
public static int harmonicMean(int x,int y)
{
int mean= (2*x*y)/(x+y);
return mean;
}
public static int average(int x,int y)
{
int mean= (x+y)/2;
return mean;
}
public static double geometricMean(int x,int y)
{
return Math.sqrt(harmonicMean(x,y)*average(x,y));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.