๐ WAYPOINT LESSON
Reading the contract
โญโญ intermediateโณ 10 min read๐ Lesson 81 of 256
Extract signature, inputs, output, and bound words before writing code.
An AP question is a contract, not a conversation. Every part of it carries obligations:
Write method `int countInRange(int[] data, int lo, int hi)`
Return the number of elements of `data` that are greater than or equal
to `lo` and less than or equal to `hi`.
Before writing a single line, extract the contract's four parts:
- Name & signature โ
countInRange, three parameters, returnsint. The signature is given; you may not change it. - Inputs โ
data(anint[], possibly length 0),loandhi(int, any order). - Output โ a count: an integer โฅ 0, returned, never printed.
- Conditions โ "greater than or equal to
lo" and "less than or equal tohi": both bounds are inclusive. The words is / or / and / at least decide your operators.
The exam loves three hidden traps inside plain English:
- inclusive vs exclusive bounds: "from the first through the last"
means
<=; "between" on the AP exam is stated exactly โ obey the words given, never a convention you remember from elsewhere. - count vs find: "return the number of..." wants a counter, not the element and not its index.
- return vs print: an FRQ that asks you to return prints nothing. A solution that prints the answer scores zero for that part.
Train the reading habit: underline the return type, circle every comparative phrase, and list the edge inputs (empty array, single element, all matching, none matching) before you code.