-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMeans.java
41 lines (33 loc) · 1.33 KB
/
Means.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// takes two numbers & computes the means
// means are calculated arithmetically, geometrically, & harmonically
import java.util.Scanner;
public class Means{
// arithemetic mean (2 doubles), sum divided by amount of numbers
private double arithmeticMean(double a, double b){
return (a + b) / 2;
}
// geometric mean (2 doubles), square root of the product of the numbers
private double geometricMean(double a, double b){
return Math.sqrt(a * b);
}
// harmonic mean (2 doubles), total number divided by the sum of the inverses
private double harmonicMean(double a, double b){
return 2 / ((1 / a) + (1 / b));
}
public Means(){
Scanner scanner = new Scanner( System.in ) ;
System.out.println("Enter two floating point numbers whose");
System.out.println("means you would like to compute.");
double x = scanner.nextDouble();
double y = scanner.nextDouble();
// output based on the input given (first two numbers input)
System.out.println("For the given set ("+x+","+y+"),");
System.out.println("Arithemetic Mean = " + arithmeticMean(x,y));
System.out.println("Geometric Mean = " + geometricMean(x,y));
System.out.println("Harmonic Mean = " + harmonicMean(x,y));
}
//main run of program
public static void main(String[] args){
Means means = new Means() ;
}
}