Lab 07: Choose Your Path
Switch statements, ternary expressions, and branching narratives
After this lab, you will be able to:
- Use
switchstatements to route execution based on a value - Explain why
breakis required and what happens without it (fall-through) - Compare strings in
switchusing.equals()semantics (Java 7+) - Write ternary expressions as compact alternatives to simple
if/else
What You’re Building
You will write a menu-driven program where the user picks from numbered options, each choice routes through a switch statement, sub-choices use string comparison, and a status line uses a ternary expression. This covers the three branching constructs that complement if/else: switch, string-based dispatch, and the ternary operator.
Concepts and Misconceptions
| Concept | Common Mistake | What the Test Catches |
|---|---|---|
Missing break |
Omitting break causes fall-through into the next case |
Multiple case outputs printed for one selection |
default case |
No default branch — invalid input produces no output |
Test sends invalid option and expects an error message |
String in switch |
Using == instead of relying on switch (which uses .equals() internally) |
Not applicable to switch itself, but relevant for sub-choice comparisons |
| Ternary syntax | Writing condition ? "a", "b" (comma instead of colon) |
Compilation error |
Checkpoints
Checkpoint 1: Menu Display and Switch Routing
What to do: Print a numbered menu (e.g., 1-Explore, 2-Rest, 3-Quit). Read the user’s integer choice. Use a switch statement with case 1:, case 2:, case 3:, and default: to print a different message for each option. Include break in every case.
What the test checks: Each valid option produces exactly one output message. The default case prints an error for invalid choices like 5 or -1.
Debugging tip: If you see two messages printed for one choice, you are missing a break. Without break, execution falls through to the next case. Add break; as the last statement in every case block.
Checkpoint 2: String-Based Sub-Choices with .equals()
What to do: For one of the menu options, prompt for a follow-up string choice (e.g., "north", "south", "east"). Use if/else if with .equals() to route the response. Print a distinct message for each direction and an error for unrecognized input.
What the test checks: The string comparison is case-sensitive and matches expected values exactly. Using == would intermittently fail.
Debugging tip: If the comparison fails even though the strings look identical, check for case mismatch. "North".equals("north") is false. Either normalize input with .toLowerCase() before comparing, or document that input must be lowercase and match exactly.
Checkpoint 3: Ternary Status Display
What to do: After the main choice executes, print a status line that uses a ternary expression. For example: String status = (choice == 3) ? "Game Over" : "Continuing...";. Print the status.
What the test checks: The status message matches the expected value for each choice. The test also inspects the source for the ? and : ternary operators.
Debugging tip: The ternary operator is an expression, not a statement. It must be assigned to a variable or used inside another expression (like a println argument). Writing condition ? doThis() : doThat(); as a standalone statement is a style violation and may not compile depending on the return types.
How to Debug
-
Test the
defaultcase first. Send an invalid input (like99) and confirm the error message prints. Many students write the happy path and forgetdefault, which costs easy points. -
Trace fall-through intentionally. Temporarily remove one
breakand observe the output. Understanding what fall-through looks like helps you recognize it when it happens accidentally. -
Keep ternary expressions simple. If the ternary has nested ternaries or long strings, rewrite it as
if/else. The ternary operator is for one-line, two-outcome decisions. Anything more complex becomes unreadable and error-prone.
Scoring
| Component | Points | Criteria |
|---|---|---|
| Checkpoints | 3 | 1 pt each. Binary: the checkpoint test passes or it does not. |
| Autograder | 5 | Correctness across all test cases. Partial credit by proportion of tests passed. |
| Timeliness | 2 | Full credit if submitted by the due date. 0 if late. |
| Total | 10 |