Search⌘ K
AI Features

Solution: Sensor Data Smoothing

Understand how to process sensor data in Java by sorting a double array and calculating an average that excludes outlier values. This lesson helps you separate calculation logic from test code, enhancing clarity and data smoothing techniques.

We'll cover the following...
Java 25
import java.util.Arrays;
public class SensorDataAnalysis {
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);
}
}
...