๐ WAYPOINT LESSON
Extending provided code
โญ beginnerโณ 12 min read๐ Lesson 71 of 256
Match style, reuse accessors, add methods without touching the given class.
Modifying provided code โ some FRQs give you a working class and ask you to add a method that fits its conventions:
- Match the class's style: if it uses accessors everywhere, use them.
- Reuse existing public methods when the spec allows โ calling
getName()beats re-deriving the field. - Do NOT touch the provided code: add methods, don't rewrite the class. Graders diff against the given parts.
- The spec's return type is a hint:
booleanwants a predicate,intwants a count/index, a class name wants an object.
// Given class: Student { getName(), getScore() }
// Spec: "returns the name of the higher-scoring student;
// if tied, return either"
public static String winner(Student a, Student b) {
if (a.getScore() >= b.getScore()) {
return a.getName();
}
return b.getName();
}
Trace your finished method against the spec's OWN example (specs usually include one) โ if your trace of their example disagrees with their stated answer, your reading is wrong, not their example.
โก Now practice
Ready to CodeFRQ-style repsSpec-following, checksums, extending classes, char-code bugs, run counting.
4 challenges ยท ยท ~55 min