01
Overview

Movement that stops on a crash.

A boolean flag tracks whether the object has crashed; once set, all further moves are blocked.

Some movement classes need an "I'm done — no more moves" mode. A crash flag is a boolean instance variable that records whether the object has hit something. Once true, every subsequent move is rejected.

This combines three patterns from earlier topics: position state, bounds checking, and a state flag. The crash flag is a one-way switch — it gets set to true on the first invalid move and never resets.

On the AP exam, this pattern appears in rover/robot FRQs and in any "object that can fail permanently" problem.

02
Core Concept

Guard every move with the crash flag.

Once true, the flag short-circuits all motion.

Class skeleton

public class Rover {
    private int row, col;
    private int rows, cols;
    private boolean crashed;

    public Rover(int rows, int cols) {
        this.rows = rows;
        this.cols = cols;
        this.row = 0;
        this.col = 0;
        this.crashed = false;
    }

    public void move(char direction) {
        if (crashed) return;   // first guard — already crashed

        int newRow = row, newCol = col;
        if (direction == 'U') newRow--;
        else if (direction == 'D') newRow++;
        else if (direction == 'L') newCol--;
        else if (direction == 'R') newCol++;

        if (newRow < 0 || newRow >= rows ||
            newCol < 0 || newCol >= cols) {
            crashed = true;    // crash on out-of-bounds
            return;
        }
        row = newRow;
        col = newCol;
    }

    public boolean isCrashed() { return crashed; }
}

The two guards

  1. Already crashed? Return immediately without changing anything.
  2. Move out of bounds? Set the crash flag and return without moving.

The order matters: check the existing flag first, then compute and validate the new position.

Sticky flag

The crash flag is sticky — it only ever goes from false to true. There's no uncrash() method. This is intentional: the object is permanently disabled after the first crash.

Running a sequence

public void runCommands(String commands) {
    for (int i = 0; i < commands.length() && !crashed; i++) {
        move(commands.charAt(i));
    }
}

The loop's !crashed check is technically redundant (the move method already short-circuits) but cleaner and slightly faster.

03
Key Vocabulary

Crash-flag vocabulary.

Used in persistent-failure object problems.

Vocabulary
  • Crash flag — boolean recording permanent failure.
  • Sticky flag — boolean that only goes true once and stays.
  • Pre-move guard — check before computing a new position.
  • Trial position — newRow/newCol before validation.
  • Permanent failure — state that can't be undone.
  • Short-circuit return — exit method early when failure is known.
04
AP Exam Focus

How crash-flag classes are tested.

Two-guard structure and stickiness dominate.

Exam Focus
  • Guard at the top. Already-crashed check is the first line of move.
  • Set the flag on crash. Out-of-bounds means crash, not just no-op.
  • Don't reset the flag. Crash is permanent.
  • Position stays where it was. Crash doesn't change row/col.

MCQ patterns: trace position and crash flag through a sequence; identify the move that triggers the crash.

05
Worked Example

Trace a Rover that crashes mid-sequence.

Once crashed, all later moves are ignored.

Worked Example AP-style reasoning

Setup. 3×3 grid. Rover r = new Rover(3, 3); — position (0, 0), crashed = false.

Commands: "RRRDD"

  • R: not crashed; try (0, 1); in bounds → row=0, col=1.
  • R: not crashed; try (0, 2); in bounds → row=0, col=2.
  • R: not crashed; try (0, 3); col=3 ≥ 3 (out of bounds) → crashed = true. Position stays (0, 2).
  • D: crashed → return immediately. Position stays (0, 2).
  • D: crashed → return immediately. Position stays (0, 2).

Final state: position (0, 2); crashed = true.

Why this matters. Two of the five commands never executed. Once the crash flag is set, the rover's position is frozen. The last D's "would have been valid" doesn't matter — the flag's set.

06
Common Mistakes

Crash-flag pitfalls.

Most miss the early guard or let the flag reset.

Watch Out
  • No top-of-method guard. Move runs even after crash; flag effectively does nothing.
  • Updating position during crash. Out-of-bounds move shouldn't move; only crash.
  • Resetting the flag. Crash should be permanent unless spec says otherwise.
  • Setting crash on every invalid action. Some specs distinguish "ignored" from "crash"; read the prompt.
  • Forgetting to initialize crashed to false. Java defaults boolean fields to false, but spell it out for clarity.
  • Continuing the loop after crash. Optionally short-circuit with !crashed.
07
Reference Table

Crash-flag class structure.

Two guards; sticky flag.

Crash Flag Reference

AP Quick Reference
ElementDetailAP Tip
Crash fieldprivate boolean crashedInitial false.
First guardif (crashed) return;Top of move method.
Trial positionnewRow, newColCompute before checking.
Crash triggerOut-of-boundsSet flag; don't move.
Position updateOnly if in boundsCommit after validation.
08
Practice Tip

How to master crash-flag movement.

Two guards in every move method; flag never resets.

Practice Strategy

Use the three practice tools below this lesson in order:

  • MCQ Practice — for each call sequence, track both position and crash flag at every step.
  • Java Lab — build a Rover with crash on out-of-bounds. Test sequences that crash early, mid, and never.
  • FRQ Practice — start move with if (crashed) return;. Make the flag the first thing checked, every time.

If you can reflexively write the two-guard pattern — top-of-method short-circuit plus crash-on-invalid — this whole pattern becomes a reliable FRQ answer.

09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 10 Topic 05 Stateful Movement with a Crash Flag FRQ Practice

AP-style topic practice assessment

Start