228,990 views
1 vote
1 vote
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. a 7-line code segment reads as follows. line 1: forward slash, asterisk, asterisk, at, return true if d1 and d2 are within the specified tolerance, comma. line 2: asterisk, false otherwise. line 3: asterisk, forward slash. line 4: public boolean almost equal, open parenthesis, double d1 comma double d2 comma double tolerance, close parenthesis. line 5: open brace. line 6: forward slash, asterisk, missing code, asterisk, forward slash. line 7: close brace. which of the following should replace / * missing code * / so that almostequal will work as intended? responses

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;

User M Shafique
by
3.1k points

1 Answer

16 votes
16 votes

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

Step-by-step explanation:

The absolute value tells you how far a number is from zero.

User Adam Rabung
by
2.6k points