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 *...

  • 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...

  • 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...

  • 1 Suppose you are designing a set of classes intended to represent a solar system and...

    1 Suppose you are designing a set of classes intended to represent a solar system and the planets and comets within it. Which of the following class structures is a correct representation? public class Comet { ... } public class Planet { ... } public class SolarSystem extends Planet { ArrayList <Comet> comets; ... } public class Planet { ... } public class Comet { ... } public class SolarSystem { ArrayList <Planet> planets; ArrayList <Comet> comets; ... } public...

  • Summer, 2018 9. What will be returned from the following method? public static int methodA(0 double...

    Summer, 2018 9. What will be returned from the following method? public static int methodA(0 double a 8.5+9.5 return a 10. Use Scanner and switch. between 1 and 5. Evaluate the number and display the corresponding vowel: Write a program that prompts the user to enter an integer Vowel Integer 2 Match each term with the correct statement below. a. relational operator b. NOT operator c. Boolean values dual-alternative e. switch statement i. AND operator true and false ouble equal...

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
ADVERTISEMENT