Consider the array of floating-point values called "measurements". Complete the code so it would display the values from "measurements" that deviate from TARGET by no more than TOLERANCE. You may want to use Math static method "abs", which returns the absolute value of a given number. For example, Math.abs(-3) = 3.
To be clear, the expected output of your program should be the numbers 3.51, 3.49, 3.51, and 3.5.
double measurements[] = { 3.51, 3.53, 3.49, 3.51, 3.47, 3.50 }; double TOLERANCE = 0.01; double TARGET = 3.5;
Display keyboard shortcuts for Rich Content Editor java
import java.util.Scanner;
public class Tolerance {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size=sc.nextInt();
double[] measurement = new double[size];
for (int i = 0; i < size; i++) {
measurement[i]=sc.nextDouble();
}
double tolerance=sc.nextDouble();
double target = sc.nextDouble();
double lower = target-tolerance;
double upper = target+tolerance;
for (int i = 0; i < size; i++) {
if(measurement[i]>=lower && measurement[i]<=upper) {
System.out.print(measurement[i]+" ");
}
}
}
}
Below is the output of the above code.
Here I attached the explanation of the above code.
I hope you liked the solution. If you do so then support us by pressing that thumbs up button.
Get Answers For Free
Most questions answered within 1 hours.