2006 AP CSA FRQ 4: Customer - Complete Solution

2006 AP CSA FRQ 4: Customer

Topic: Comparable & String Operations

Skills Tested: compareTo logic, String comparison, multi-field comparison, return value conventions

Curriculum Alignment: Unit 2 (Selection/Iteration) (2025-2026 AP CSA)

Points: 9 | Time Estimate: ~22 minutes

Part (a)

Write the compareCustomer method that compares customers first by name, then by ID if names are equal.

Try It First! Attempt to solve this part before viewing the solution.

Solution

public int compareCustomer(Customer other)
{
    if (getName().equals(other.getName()))
        return getID() - other.getID();
    
    return getName().compareTo(other.getName());
}

Part (b)

Alternative implementation using nested conditionals for clarity.

Solution

// Alternative implementation:
public int compareCustomer(Customer other)
{
    int nameCompare = getName().compareTo(other.getName());
    
    if (nameCompare != 0)
        return nameCompare;
    
    return getID() - other.getID();
}

Key Concepts

compareTo returns negative, zero, or positive based on ordering
Use equals() for String equality, compareTo() for ordering
Subtraction trick works for numeric comparison (a - b)
Multi-field comparison: check primary field first, then secondary

Common Mistakes to Avoid

Using == instead of equals() for String comparison
Returning only -1, 0, 1 instead of any negative/zero/positive
Wrong order of comparison (ID before name)
Not using the provided getter methods

Official College Board Resources

About the Author: Tanner Crow is a certified AP Computer Science teacher with 11+ years of experience.

Last updated: January 2026

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]