Question

Consider the following method that is intended to determine if the double values d1 and d2...

Consider the following method that is intended to determine if the double values d1 and d2 are close enough to be considered equal.
For example, given a tolerance of 0.001, the values 54.32271 and 54.32294 would be considered equal.

/** @return true if d1 and d2 are within the specified tolerance, 
 * false otherwise 
 */ 
public boolean almostEqual(double d1, double d2, double tolerance) 
{ 
 /* missing code */ 
}

Which of the following should replace /* missing code */ so that almostEqual will work as intended?

1. return ((d1 + d2) / 2) >= tolerance;

2. return ((d1 + d2) / 2) <= tolerance;

3. return Math.abs(d1 - d2) <= tolerance;

4. return (d1 - d2) <= tolerance;

5. return (d1 - d2) >= tolerance;

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/** @return true if d1 and d2 are within the specified tolerance, 
 * false otherwise 
 */ 
public boolean almostEqual(double d1, double d2, double tolerance) 
{ 
    return Math.abs(d1 - d2) <= tolerance; 
}

return Math.abs(d1 - d2) <= tolerance;
Add a comment
Know the answer?
Add Answer to:
Consider the following method that is intended to determine if the double values d1 and d2...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Floats.java Use the following calculations for di and d2 in your code: double di = 0;...

    Floats.java Use the following calculations for di and d2 in your code: double di = 0; // add .1 to 0 eleven times d1 += 1; // 1 d1 += .1; // 2 d1 += 1; // 3 d1 += 1; // 4 d1 += 1; // 5 d1 += .1; 1/6 d1 += 1; /17 d1 += .1; 118 d1 += 1; // 9 d1 += 1; // 10 d1 += 1; // 11 double d2 = 1 *...

  • (Need to complete the methods and pass a Junit test i can email them to you.)...

    (Need to complete the methods and pass a Junit test i can email them to you.) public class Triangle implements Comparable {    /**    * Stores the number of objects instantiated from the {@code Triangle} class    */    private static int numTriangles;    /**    * The shortest side of the triangle    */    private final double a;    /**    * The side of medium length of the triangle    */    private final double b;...

  • Consider the following incomplete code segment, which is intended to print the sum of the digits...

    Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num is 12345, the code segment should print 15, which represents the sum 1 + 2 + 3 + 4 + 5. int num = 12345; int sum = 0; /*  missing loop header */ { sum += num % 10; num /= 10; } System.out.println(sum); Which of the following should replace /* missing loop header */ so that the...

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • Question 11 (3 points) What does the following recursive method determine? public boolean question 16(int[]a, int[]...

    Question 11 (3 points) What does the following recursive method determine? public boolean question 16(int[]a, int[] b. intj) { if (j == 2.length) return false; else if ( == b.length) return true; else return question 16(2, b.j+1): 3 returns true if b contains less elements than a, false otherwise returns true if b contains more elements than a, false otherwise returns true if a and bare equal in size, false otherwise returns true if a's element value is larger than...

  • Which of the following is true about interfaces: An interface can have only non abstract methods....

    Which of the following is true about interfaces: An interface can have only non abstract methods. All methods in an interface must be abstract. A class can only implement one interface. None of the items listed. Can not contain constants but can have variables. What is the rule for a super reference in a constructor? It must be in the parent class' constructor. You cannot use super in a constructor. It must be the last line of the constructor in...

  • Consider the following class: public class Sequence { private int[] values; public Sequence(int size) { values...

    Consider the following class: public class Sequence { private int[] values; public Sequence(int size) { values = new int[size]; } public void set(int i, int n) { values[i] = n; } public int get(int i) { return values[i]; } public int size() { return values.length; } } Add a method public boolean sameValues(Sequence other) to the Sequence class that checks whether two sequences have the same values in some order, ignoring duplicates. For example, the two sequences 1 4 9...

  • need some help on these a little contested right now the answers i have are 18...

    need some help on these a little contested right now the answers i have are 18 : d 19 : e 20 : c would appriacte why i am right or wrong thank you sorry i added the picture of 17 by mistake 19. The default constructor sets x and y to (0,0) by calling the second constructor. What could be used to replace / missing code */ so that this works as intended? a b = = 0; 0;...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT