Commit 8781fc51 authored by hasan  khaddour's avatar hasan khaddour

Add Comments & fix Negative Number Case

parent 7e09b055
......@@ -2,18 +2,39 @@ package task;
import java.util.Arrays;
// This Task Calculate The Geometric Mean
// The Geo-Mean is defined by :
// Geo-Mean = N-Root of (a1 * a2 * ... * an) , ai >=0
//
public class TaskGeometricMean extends TaskImpl {
@Override
public void execute() {
//Split the input values
String[] stringArray = input.split(",");
// Parse the values
double[] doubleArray = Arrays.stream(stringArray)
.mapToDouble(Double::parseDouble)
.toArray();
// Check if all the values is poistive
boolean allPositive =Arrays.stream(doubleArray).allMatch((e)->e>0);
if(allPositive){
// Calculate the geometric mean
// Calculate P = (a1 * a2 * ... * an)
double product = Arrays.stream(doubleArray).reduce(1.0, (a, b) -> a * b);
// Calculate the Geo-Mean = N-Root of( P )
double geometricMean = Math.pow(product, 1.0 / doubleArray.length);
// Set tThe result
result = String.valueOf(geometricMean);
}else {
result="All The Array Values Should Be Positive";
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment