From the free course AP Computer Science A on Lykke

AP Practice 2

Key concepts: Java data types, including int, double, Boolean values, and reference types · Casting and the range of variables · Boolean expressions and compound operators such as && and || · Methods and control structures · Writing, tracing, and analyzing Java program code · Debugging code by identifying and correcting errors · Initial conditions required for code to work as intended · Classes, objects, and encapsulation · Using the Java Quick Reference · AP Computer Science A assessment format

A debugging task is not solved by guessing what the programmer intended; it is solved by tracing the actual values, identifying the violated condition, and changing only what prevents the program from meeting its specification.

AP Practice 2

A debugging task is not solved by guessing what the programmer intended; it is solved by tracing the actual values, identifying the violated condition, and changing only what prevents the program from meeting its specification. The most reliable workflow combines Java data types, Boolean logic, control structures, casting, and boundary testing.

The debugging workflow

Step Question to ask Typical evidence
1. Identify the specification What must the method return or accomplish? Valid score range, required output
2. Identify types What are the types of every variable and expression? int, double, boolean, or reference type
3. Trace representative inputs What happens for ordinary, boundary, and invalid values? Variable table or handwritten trace
4. Test Boolean logic Must both conditions hold, or is either sufficient? && versus `
5. Correct the smallest error What single change restores the intended behavior? Revised expression or statement
6. Retest Does the correction work at every boundary? Input-output checks

Worked debugging task: validating a score

A school stores each test score as an int. A valid score is any integer from $0$ through $100$, inclusive. The method below is intended to return true exactly when the score is valid.

public static boolean isValidScore(int score)
{
    return score >= 0 || score <= 100;
}

The error is the compound Boolean operator. The && operator evaluates to true only when both operands are true; otherwise it evaluates to false. The || operator evaluates to true when at least one operand is true.

The corrected Boolean expression is:

return score >= 0 && score <= 100;

Now trace boundary and outside-range values:

score score >= 0 score <= 100 Combined result
$0$ true true true
$100$ true true true
$-1$ false true false
$101$ true false false

Misconception check — “Either valid condition is enough.”
For a range, both limits must hold. With ||, a score of $-1$ passes because it is less than or equal to $100$; a score of $101$ passes because it is greater than or equal to $0$. That expression accepts almost every integer, not just scores in the intended interval.

Types, casting, and range

An integer value in Java uses the primitive type int. A double stores a number that may contain a fractional part. A reference type is used to define objects, such as String, Integer, or Double; the variable holds a reference to an object rather than the object’s primitive value directly.

Casting changes how Java treats a primitive value in an expression. Casting a double to an int truncates the digits to the right of the decimal point:

int whole = (int) 8.9;       // 8
double quotient = 7 / 2;     // 3.0
double precise = 7 / 2.0;    // 3.5

The second expression performs integer division before widening the result to double. Because the expression contains a double in the third line, Java widens the int value to double before division.

For non-negative $x$, rounding to the nearest integer can be written as (int)(x + 0.5); for negative $x$, use (int)(x - 0.5). Casting does not remove the finite range of an int: an integer expression that exceeds the permitted range can produce an out-of-range result through overflow.

Classes and encapsulation

A well-designed class uses encapsulation: its data is protected inside the object, while methods control how that data is read or changed.

public class Thermostat
{
    private double temperature;

    public Thermostat(double initialTemperature)
    {
        temperature = initialTemperature;
    }

    public void increase(double amount)
    {
        temperature += amount;
    }

    public double getTemperature()
    {
        return temperature;
    }
}

The field is private, so outside code cannot directly assign an invalid value. The public methods form a controlled interface. On an exam, distinguish a compile-time error, such as using an undeclared variable, from a logic error, such as using || where && is required.

Exam execution and retrieval check

For a short debugging or code-analysis task, spend roughly one-third of the available time tracing, one-third correcting, and one-third retesting. Use the Java Quick Reference for unfamiliar accessible methods such as Integer.parseInt(String s), but do not use it as a substitute for tracing variable values.

Retrieval check: What does this return for value = 100.5?

return value >= 0 && value <= 100;

The result is false: both comparisons must be true, and $100.5 \le 100$ is false. This demonstrates 3.C Determine the result or output based on code that contains procedural abstractions, 4.A Describe the behavior of a code segment or program, and 4.B Describe the initial conditions that must be met for a code segment to work as intended or described. A strong error review records the incorrect assumption, the input that exposed it, and the smallest correction that fixes it.

AP Practice 2 - AP Computer Science A - image 1
AP Practice 2 - AP Computer Science A - image 1
AP Practice 2 - AP Computer Science A - image 2
AP Practice 2 - AP Computer Science A - image 2
AP Practice 2 - AP Computer Science A - image 3
AP Practice 2 - AP Computer Science A - image 3
AP Practice 2 - AP Computer Science A - image 4
AP Practice 2 - AP Computer Science A - image 4
AP Practice 2 - AP Computer Science A - image 5
AP Practice 2 - AP Computer Science A - image 5
AP Practice 2 - AP Computer Science A - image 6
AP Practice 2 - AP Computer Science A - image 6
AP Practice 2 - AP Computer Science A - image 7
AP Practice 2 - AP Computer Science A - image 7
AP Practice 2 - AP Computer Science A - diagram 1
AP Practice 2 - AP Computer Science A - diagram 1

Source Materials

The whole course on Lykke · Browse all public course wikis