Skip to main content
๐Ÿ“œ 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: boolean wants a predicate, int wants 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 Code
FRQ-style repsSpec-following, checksums, extending classes, char-code bugs, run counting.
4 challenges ยท ยท ~55 min