Search⌘ K
AI Features

Solution: Sensor Data Smoothing

Explore how to smooth sensor data by sorting arrays and averaging middle values, ignoring outliers. Understand array manipulation, sorting methods, and clean function design for effective Java data processing.

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);
}
}
...