FRQ
› csapa
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-doubledivision 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
APLineyourself. - An
APLinerepresents the equationax + by + c = 0, whereaandbare guaranteed to be nonzero integers. - The slope of the line is defined as the
doublevalue-a / b. - A point
(x, y)is on the line if substituting those values makesax + by + cequal to0. - Required members:
- A constructor with three
intparameters, in this exact order:a,b,c double getSlope()— computes and returns-a / bboolean isOnLine(int x, int y)— returns whether the point(x, y)satisfies the equation
- A constructor with three
Building the APLine Class
Step-by-Step Approach
- Store
a,b, andcas private instance fields —isOnLineneeds the raw coefficients later, not just the derived slope, so all three have to be kept around. - Write the constructor to accept the three integers in the order the question specifies (
a,b,c) and assign each to its field. - In
getSlope, compute-a / bas adouble— sinceaandbare bothint, the cast has to happen before the division actually occurs, not after. - In
isOnLine, computea * x + b * y + cand directly return whether that equals0— no need for anif/elsethat returnstrueorfalseseparately.
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-abefore the division happens, forcing Java to perform floating-point division for the whole expression. Java only needs one side of a/to be adoubleto avoid integer truncation, and casting the numerator here does exactly that.- Keeping
a,b, andcas separate fields, rather than just storing the computed slope —isOnLineneeds 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 to0, 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.ais 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 |
0 → true |
yes |
line2.isOnLine(5, -2) |
-25(5) + 40(-2) + 30 = -125 - 80 + 30 |
-175 → false |
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 / bas integer division first (truncating any fraction), and only converts the already-wrong result to adoubleafterward. 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, nota / b— dropping the negation gives the wrong sign every time. - Storing only the slope and discarding
a,b,c. Without the original coefficients,isOnLinehas 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, andc, 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
intvalues todoublehas to happen before an integer division runs, never after — the truncation already happened by the time a cast is applied to a finishedintresult. - A method that just checks whether a condition is true or false can usually
returnthe boolean expression directly, instead of writing out anif (condition) return true; else return false;block.