Consider the following method documentation. Which of the following calls satisfies the documented preconditions?
/**
* Returns the percentage of pages read.
* Precondition: total > 0.
* Precondition: 0 <= read <= total.
*/
public double readPct(int total, int read)
readPct(0, 0)readPct(20, 24)readPct(20, -1)readPct(20, 8)readPct(20, 8) is the only one whose total is positive and whose page count stays within range.
Explanation
A precondition states what must be true when a method is called, and the code making the call is responsible for satisfying it. Both documented conditions apply at once: the total must be greater than zero, and the pages read must fall from zero through the total.
- A is not correct: the first precondition requires a total greater than zero, and
readPct(0, 0)passes a total of0instead. - B is not correct: the second precondition allows the pages read to reach only the total, and
readPct(20, 24)passes a larger count. - C is not correct: the second precondition requires the pages read to be at least zero, and
readPct(20, -1)falls below that limit.