CompSci.rocks
FRQcsapa

APLine: 2010 FRQ 2

A step-by-step solution to the 2010 AP CSA FRQ 2 (APLine), covering designing a class from scratch around a linear equation, careful int-to-double division, and translating a math condition into a boolean expression in Java.

No class skeleton is handed to you at all in this AP Computer Science A free-response question — you're given a math formula for a line and asked to design the entire APLine class around it, from the fields up.

What This FRQ Tests

  • AP CSA units: Unit 5 (Writing Classes)
  • Core skill: designing a class's fields and constructor directly from a mathematical description, with nothing already scaffolded
  • Secondary skill: getting int-to-double division right, and translating a "does this equation equal zero" condition directly into a boolean expression
  • Official category: "Classes" — this happens to land in the same slot a modern exam would use, but 2010 predates the standardized FRQ-number-to-category order (formalized starting with the 2019–2020 CED redesign), so that match is coincidental rather than guaranteed. It's printed as FRQ 2 in the original exam either way.

The Setup

  • No class skeleton is provided — you design all of APLine yourself.
  • An APLine represents the equation ax + by + c = 0, where a and b are guaranteed to be nonzero integers.
  • The slope of the line is defined as the double value -a / b.
  • A point (x, y) is on the line if substituting those values makes ax + by + c equal to 0.
  • Required members:
    • A constructor with three int parameters, in this exact order: a, b, c
    • double getSlope() — computes and returns -a / b
    • boolean isOnLine(int x, int y) — returns whether the point (x, y) satisfies the equation

Building the APLine Class

Step-by-Step Approach

  1. Store a, b, and c as private instance fields — isOnLine needs the raw coefficients later, not just the derived slope, so all three have to be kept around.
  2. Write the constructor to accept the three integers in the order the question specifies (a, b, c) and assign each to its field.
  3. In getSlope, compute -a / b as a double — since a and b are both int, the cast has to happen before the division actually occurs, not after.
  4. In isOnLine, compute a * x + b * y + c and directly return whether that equals 0 — no need for an if/else that returns true or false separately.

The Code

public class APLine
{
    private int a;
    private int b;
    private int c;

    public APLine(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double getSlope()
    {
        return (double) -a / b;
    }

    public boolean isOnLine(int x, int y)
    {
        return a * x + b * y + c == 0;
    }
}

Why Each Piece Matters

  • (double) -a / b — the cast applies to -a before the division happens, forcing Java to perform floating-point division for the whole expression. Java only needs one side of a / to be a double to avoid integer truncation, and casting the numerator here does exactly that.
  • Keeping a, b, and c as separate fields, rather than just storing the computed slope — isOnLine needs the original coefficients to test a specific point, which the slope alone can't reconstruct.
  • return a * x + b * y + c == 0; — Java evaluates the arithmetic first, then compares the result to 0, so this single line does exactly what "is the equation satisfied" means without any extra branching.
  • this.a = a; — since the constructor's parameters are named identically to the fields (matching the question's required parameter order), this.a is what distinguishes the field from the parameter.

Tracing the Example

Using both lines from the question's own table:

Line getSlope() Computation Matches?
new APLine(5, 4, -17) (double) -5 / 4 -1.25 yes — question states -1.25
new APLine(-25, 40, 30) (double) -(-25) / 40 25 / 40 = 0.625 yes — question states 0.625
Call Computation Result Matches?
line1.isOnLine(5, -2) 5(5) + 4(-2) + (-17) = 25 - 8 - 17 0true yes
line2.isOnLine(5, -2) -25(5) + 40(-2) + 30 = -125 - 80 + 30 -175false yes

All four values match the question's given results exactly.

Common Mistakes to Avoid

  • Casting after the division instead of before(double) (-a / b) computes -a / b as integer division first (truncating any fraction), and only converts the already-wrong result to a double afterward. The cast has to wrap -a (or the whole numerator), not the finished division.
  • Forgetting the negative sign in the slope formula. The problem defines slope as -a / b, not a / b — dropping the negation gives the wrong sign every time.
  • Storing only the slope and discarding a, b, c. Without the original coefficients, isOnLine has no way to test an arbitrary point against the equation.
  • Swapping the constructor's parameter order. The question is explicit that the three parameters represent a, b, and c, in that order — reversing any two of them breaks every computed value silently, since the code would still compile.

Key Takeaways

  • When no class skeleton is given, work backward from what each required method needs to compute — that tells you exactly which fields the class actually needs, not just what feels natural to store.
  • Casting int values to double has to happen before an integer division runs, never after — the truncation already happened by the time a cast is applied to a finished int result.
  • A method that just checks whether a condition is true or false can usually return the boolean expression directly, instead of writing out an if (condition) return true; else return false; block.

Related FRQs