What to practice
- Trace numeric, String, and array recursive methods and unwind LIFO call stacks.
- Read and process structured text data using Scanner methods in the 2026–2027 AP CSA CED.
- Synthesize full-credit Java code across all four AP CSA Free-Response question archetypes.
Current CED topics: 4.1, 4.2, 4.6, 4.16, 4.17
Current learning objectives: 4.1.A, 4.1.B, 4.1.C, 4.2.A, 4.6.A, 4.16.A, 4.17.A, 4.17.C
1. Core Concept Instruction
A recursive method calls itself on a smaller or simpler problem until it reaches a base case. Trace the calls downward, solve the base case, and then work upward through the pending operations. A statement before the recursive call runs on the way down; a statement after it runs as the calls return. The first worked example follows both numeric results and String output.
A data set is a collection of records used to answer a question. Before writing a loop, identify what each record means, the units of its values, and which records belong in the calculation. A small table is useful for planning: record the input, whether it qualifies, and the running total and count. For a mean, divide the total of qualifying values by the number of qualifying values; an empty group needs a stated rule rather than division by zero.
Data quality matters. A missing-value code is not an observed measurement. Duplicated records, inconsistent units, incorrect values, or missing groups can change a result. Record any cleaning rule and report what is missing. A large data set can still be biased if its collection method leaves out people relevant to the question. For example, app-user commute records do not automatically describe students who do not use that app. Use data collected for a suitable population and purpose; do not assume that a calculation fixes a biased input.
Protect privacy by collecting and sharing only the personal information needed for the stated purpose, restricting access, and retaining data only as needed. Removing names alone may not prevent identification when other fields can be combined. Meeting attendance totals can support room planning without publishing names, birth dates, or home addresses. Consider who might be helped or harmed before collecting data or using an algorithm's result.
For text-file input, import java.io.File, java.io.IOException, and java.util.Scanner. Connect a Scanner to a File made from the pathname. The containing method may declare throws IOException to allow file-opening failure to be reported to its caller. Use hasNext() to check for another input item, read the item with a method suited to its documented type, process it, and close the scanner after use. A file of integer tokens can use nextInt(); decimal tokens can use nextDouble(); Boolean tokens can use nextBoolean(); next() reads the next String token. nextLine() reads a line. Know the input format before reading: hasNext() does not guarantee that a token is an integer. This chapter's numeric example explicitly assumes all tokens are valid ints.
Current exam scope includes file reading, closing, and ordinary String.split with a literal delimiter. It excludes keyboard input, special regular-expression features, and code that mixes nextLine with other Scanner reading methods on the same input source. Use a consistent token-based or line-based strategy in each example. For a simple nonempty record "east,12", split(",") produces the two String fields "east" and "12"; no numeric conversion is implied by splitting.
The timed free-response sections use four tasks: Methods and Control Structures (7 points, with String work in part B), Class Design (7 points), one ArrayList method (5 points), and one 2D-array method (6 points). Their total is 25. The extra bank tasks are supplemental exercises and may use a different number of methods or points. Conceptual inheritance relationships remain in the course; implementing them is outside the current exam scope.
2. Worked Examples
Worked Recursive Call-Stack Unwinding & String Recursion Trace
Part 1: Evaluate mystery(4) where:
public static int mystery(int n) {
if (n <= 1) return 2;
return 3 * mystery(n - 1) - 1;
}
Part 2: What is printed by revPrint("CAT") where:
public static void revPrint(String s) {
if (s.length() > 0) {
revPrint(s.substring(1));
System.out.print(s.substring(0, 1));
}
}
- Step 1 (Unwind mystery(4) from Base Case): Base case: mystery(1) = 2.
- Step 2 (Substitute Upward): mystery(2) = 3 * mystery(1) - 1 = 3(2) - 1 = 5; mystery(3) = 3 * mystery(2) - 1 = 3(5) - 1 = 14; mystery(4) = 3 * mystery(3) - 1 = 3(14) - 1 = 41.
- Step 3 (Trace revPrint("CAT") Call Stack Downward): revPrint("CAT") calls revPrint("AT"), which calls revPrint("T"), which calls revPrint("") (base case s.length() == 0 returns immediately).
- Step 4 (Unwind Print Statements After Recursive Call): Because System.out.print is AFTER the recursive call, the deepest call prints first: "T", then "A", then "C" -> prints "TAC".
Result:
Read, Filter, and Close a File of Integer Tokens
readings.txt contains the integer tokens 12 -3 8 0. All tokens are valid ints, and the file opens successfully. Trace meanNonnegative("readings.txt"), then consider an empty file and a file containing only negative values.
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public static double meanNonnegative(String path) throws IOException {
Scanner input = new Scanner(new File(path));
double total = 0.0;
int count = 0;
while (input.hasNext()) {
int value = input.nextInt();
if (value >= 0) {
total += value;
count++;
}
}
input.close();
if (count == 0) {
return 0.0;
}
return total / count;
}
- File identifies the pathname; Scanner supplies token-reading operations. This method belongs inside a class; the import statements belong before that class.
- Read 12: total = 12.0 and count = 1. Read -3: the filter rejects it and both accumulators stay unchanged.
- Read 8: total = 20.0 and count = 2. Read 0: total stays 20.0 while count becomes 3, since zero is nonnegative.
- hasNext() becomes false after the last token. Close the scanner, then return 20.0 / 3. The double accumulator ensures floating-point division.
- For an empty or negative-only file, count remains zero and the specified result is 0.0. A noninteger token would violate the input contract; the program does not silently clean invalid input.
Result: Returns 20.0 / 3 (approximately 6.6667); returns 0.0 when no nonnegative readings exist.
Plan a Data-Set Calculation Before Coding
A sensor table has columns station, temperatureC, and recordedAt. The temperatures are 10, -999, and -5. Its data dictionary says -999 means missing; real values lie between -20 and 50 degrees Celsius. Plan the mean of the available readings and identify two checks needed before using it to describe all stations.
- Keep 10 and -5; do not treat the -999 missing-value code as a measurement. The running (sum, count) states are (10, 1), (10, 1), and (5, 2).
- The available-reading mean is 5 / 2 = 2.5 degrees Celsius. The negative value -5 is valid; deleting every negative would change the question.
- Check that records use the same units and do not duplicate a station/time observation. Check whether the missing reading or unrepresented stations differ systematically from observed ones.
- Report that one of three readings is missing. The calculated mean describes available readings; it does not reveal the missing temperature or guarantee representation of all stations.
Result: Available-reading mean: 2.5 degrees Celsius; 2 observed readings and 1 missing reading.