Search⌘ K
AI Features

Solution: Sensor Data Smoothing

Explore how to implement sensor data smoothing by sorting a double array, ignoring outliers, and calculating the average. Understand how to separate calculation logic from test code for cleaner design.

We'll cover the following...
Java 25
import java.util.Arrays;
public class SensorAnalysis {
public static double processSensorData(double[] data) {
// Sort to push min to index 0 and max to end
Arrays.sort(data);
double sum = 0;
// Start loop at 1 (skip min) and end before length-1 (skip max)
for(int i = 1; i < data.length - 1; i++) {
sum += data[i];
}
// Calculate average using the count of included items
return sum / (data.length - 2);
}
public static void main(String[] args) {
double[] rawReadings = {24.5, 18.2, 29.1, 21.0, 19.5, 35.0, 22.8};
System.out.println("Raw Data: " + Arrays.toString(rawReadings));
// Call the method
double stableAvg = processSensorData(rawReadings);
System.out.println("Sorted Data (after method): " + Arrays.toString(rawReadings));
System.out.println("Stable Average: " + stableAvg);
}
}
...