517,997 views
19 votes
19 votes
Consider the method get Hours, which is intended to calculate the number of hours that a vehicle takes to travel between two mile markers on a highway if the vehicle travels at a constant speed of 60 miles per hour. A mile marker is a sign showing the number of miles along a road between some fixed location (for example, the beginning of a highway) and the current location. IN The following table shows two examples of the intended behavior of getHours, based on the int parameters markeri and marker2. marker1 marke 100 220 100 700.5 Consider the following implementation of getHours. public static double getHours (int markeri, int ON 20 | 2 15. . . e no E /* missing statement */ return hours; Which of the following statements can replace /* missing statement */ so getHours works as intended?

(A) double hours = (Math.abs (markerl) - Math.abs (marker2)) / 60.0;||
(B) double hours = Math.abs (markeri - marker2 / 60.0)
(C) double hours = Math.abs (marker1 - marker2) / 60.0;
(D) double hours = Math.abs((markeri - marker2) / 60);
(E) double hours = (double) (Math.abs (marker1 - marker2) / 60);

User Bruna
by
2.9k points

1 Answer

25 votes
25 votes

Answer:

Replace the comment with:

(c) double hours = Math.abs (marker1 - marker2) / 60.0;

Explanation:

See attachment for right presentation of question

Analyzing the options:

(a): May return a negative result

This option will return a negative value if marker1 is less than marker2 because it subtracts the absolute value of marker2 from the absolute value marker1.

This literally is not different from marker1 - marker2

(b): Incorrect expression

This divides only marker2 by 60, then subtracts the quotient from marker1. This is not the expected expression.

(c) This option is correct

This correctly calculate the positive difference between marker1 and marker2 and the result is divided by 60.0 (note 60.0 not 60)

(d) & (e) Integer division

When a variable declared as double is divided by an integer variable or value, the result of the computation is not always accurate due to approximation

Consider the method get Hours, which is intended to calculate the number of hours-example-1
User Kevin Nguyen
by
2.9k points