What to practice
- Design complete encapsulated Java classes with private fields, overloaded constructors, getters, setters, and toString().
- Use the this keyword to resolve variable shadowing and pass the current object reference.
- Write full-credit solutions to AP CSA Free-Response Question 2 (Class Design).
Current CED topics: 3.1, 3.2, 3.3, 3.4, 3.5, 3.8, 3.9
Current learning objectives: 3.1.A, 3.2.A, 3.3.A, 3.4.A, 3.5.A, 3.8.A, 3.9.A
1. Core Concept Instruction
In AP Computer Science A Unit 3 (and FRQ #2: Class Design), classes enforce data encapsulation by declaring all instance variables private and providing public constructors, accessor (getter) methods, and mutator (setter) methods. A constructor has the exact same name as the class and NEVER has a return type (not even void—adding void turns it into an ordinary method!). Inside a constructor, initialize every private instance variable; if a parameter name shadows an instance variable name, use this.fieldName = fieldName (otherwise, assigning fieldName = fieldName assigns the parameter to itself and leaves the instance variable at its default value 0, 0.0, false, or null).
Program design also affects people outside the code. System reliability means a program performs its tasks as expected under stated conditions without failure, so test it under a variety of conditions, not only the usual case. Programs also affect society, the economy, and culture, and those effects can be both helpful and harmful. A program that meets a real need can still have unintended harmful effects: a navigation app that saves drivers time may also send heavy traffic down quiet residential streets. A benefit for most users does not cancel a harm to others, so ask who could be hurt and what design change would reduce it, such as a second way to finish the task or a way to correct a wrong result. Legal and intellectual property issues matter too. Programmers often reuse code that others have published as open source and free to use; code not published that way requires permission from its owner, and often a purchase, before it goes into a program.
2. Worked Examples
Worked AP CSA FRQ #2 Class Blueprint: ScoreTracker
Design a ScoreTracker class that tracks a minimum passing score (int) and records student scores. Include a constructor ScoreTracker(int minPass), recordScore(int score), getPassCount(), and getAverageScore() (returns 0.0 if no scores recorded).
- Step 1 (Declare Private Instance Variables): private int minPass; private int totalCount; private int passCount; private int sumScores;
- Step 2 (Write Constructor with No Return Type): public ScoreTracker(int minPass) { this.minPass = minPass; this.totalCount = 0; this.passCount = 0; this.sumScores = 0; }
- Step 3 (Write Mutator Method recordScore): public void recordScore(int score) { totalCount++; sumScores += score; if (score >= minPass) { passCount++; } }
- Step 4 (Write Accessor Methods with Cast Guard): public int getPassCount() { return passCount; } and public double getAverageScore() { if (totalCount == 0) return 0.0; return (double) sumScores / totalCount; }
Result:
Encapsulated Class Invariants, Accessor/Mutator Guard Clauses & this Keyword
Consider a BankVault class with private int balance = 500 and mutator:
public boolean withdraw(int amt) {
if (amt > 0 && amt <= this.balance) { this.balance -= amt; return true; }
return false;
}
Trace v.withdraw(600) followed by v.withdraw(180).
- Step 1 (Evaluate first call): v.withdraw(600) checks 600 <= 500 (false), leaving this.balance = 500 and returning false.
- Step 2 (Evaluate second call): v.withdraw(180) checks 180 > 0 && 180 <= 500 (true), updating this.balance = 320 and returning true.
Result: First call returns false (balance = 500); second call returns true (balance = 320)