๐ WAYPOINT LESSON
Decomposition and contracts
โญ beginnerโณ 12 min read๐ Lesson 23 of 256
Breaking specs into helpers, preconditions and postconditions.
Decomposition โ the design skill the exam's FRQs reward:
- Read the spec; identify the inputs, the output, and the steps.
- If the steps are complex, name each one as a helper method.
- Write the top-level method as a sequence of helper calls.
Small worked example โ "how many years until doubling a population?":
public static int yearsToDouble(double start) {
double pop = start;
int years = 0;
while (pop < start * 2) {
pop *= 1.05; // 5% growth
years++;
}
return years;
}
Preconditions and postconditions are the contract style the exam uses:
- Precondition: what must be true when the method is called
(
n >= 0). - Postcondition: what the method guarantees when it returns (returns the sum of digits of n).
A method may assume its precondition; it must deliver its postcondition. Reading the precondition first tells you which edge cases you may skip and which you must handle.