Question

Consider the array of double values called "measurements". Complete the code so it would display on...

Consider the array of double values called "measurements". Complete the code so it would display on the console 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 or expression. For example, if double variable measure = 2.9, then Math.abs(measure - 3) = 0.1..

To be clear, the expected output of your program should be the numbers 3.51, 3.49, 3.51, and 3.50.

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

Homework Answers

Answer #1

Java Program:

public class Main
{
        public static void main(String[] args) {
                double measurements[] = { 3.51, 3.53, 3.49, 3.51, 3.47, 3.50 };
        double TOLERANCE = 0.01;
        double TARGET = 3.5;
        
        for (int i = 0; i < measurements.length; i++)
        {
            double tol = Math.abs(measurements[i] - TARGET);
            if (tol < TOLERANCE)
            {
                System.out.print(measurements[i] + " ");
            }
        }
        }
}

Output:

Thumbs Up Please !!!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT