Question 1: Methods and Control Structures
Train route delays and station codes
- Units 1 and 2
- 7 points
- About 25 minutes
You write two methods, or a constructor and a method, of a class you're given. Part A needs loops and/or if statements plus calls to the class's own methods; Part B needs String methods. New since the course was revised for 2025–26 (first tested in May 2026): it's worth 7 points instead of 9, and points are no longer taken off as penalties. On the exam: Question 1 of 4, 7 points (Part A 4 points, Part B 3 points). Section II has 4 free-response questions in 90 minutes (45% of the score), all assessing Practice 2 (Develop Code). The exam is fully digital in Bluebook: you type your Java, and the Java Quick Reference is provided. No calculator. Suggested time is our split of the 90 minutes by points; College Board gives no per-question timing.
The question and its sources
A transit app keeps track of how late a train is at each stop on its route. The stops are numbered 0 through getNumStops() - 1 in the order the train visits them. You will write two methods of the TrainRoute class.
The TrainRoute class
public class TrainRoute
{
/** Returns the number of stops on the route. */
public int getNumStops()
{ /* implementation not shown */ }
/**
* Returns the number of minutes the train was late at the given stop,
* or 0 if it was on time.
* Precondition: 0 <= stop < getNumStops()
*/
public int getDelay(int stop)
{ /* implementation not shown */ }
/** Described in part (a) */
public int longestOnTimeRun(int maxDelay)
{ /* to be implemented in part (a) */ }
/** Described in part (b) */
public static String stationCode(String name)
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not shown.
}Example for part (a): delays on one route
| Stop | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| getDelay(stop) | 0 | 3 | 1 | 7 | 2 | 2 | 0 | 9 |
Source: Hypothetical example
Example calls for part (a) on the route above
| Call | Value returned | Why |
|---|---|---|
longestOnTimeRun(2) | 3 | Stops 4, 5 and 6 are three stops in a row with a delay of 2 or less. |
longestOnTimeRun(5) | 3 | Stops 0–2 and stops 4–6 are both runs of 3. |
longestOnTimeRun(0) | 1 | Only stops 0 and 6 have a delay of 0, and they aren't next to each other. |
longestOnTimeRun(8) | 7 | Stops 0–6 all qualify; stop 7 does not. |
Source: Hypothetical example
Suggested time: 25 minutes
Your answers are saved in this browser as you type.
Part (a)
4 pointsA stop is *acceptable* when the train's delay there is less than or equal to maxDelay. Write the method longestOnTimeRun, which returns the number of stops in the longest run of consecutive acceptable stops on the route. If no stop is acceptable, the method returns 0. Assume maxDelay >= 0.
You must use getNumStops and getDelay appropriately to receive full credit.
Complete method longestOnTimeRun.public int longestOnTimeRun(int maxDelay)
Tab indents. Press Esc, then Tab, to move on.
0 lines · 0 / 2,500 characters
Part (b)
3 pointsEach station has a name made of one or more words separated by single spaces, with no spaces at the start or end. A station's code is the first letter of each word, in order. For example, stationCode("North Market Street") returns "NMS", stationCode("Port of Spain") returns "PoS" and stationCode("Central") returns "C".
Write the static method stationCode, which returns the code for name.
Complete method stationCode.public static String stationCode(String name)
Tab indents. Press Esc, then Tab, to move on.
0 lines · 0 / 2,500 characters
Checking scoring…
Scoring it yourself shows you the rubric, examples and a model answer. Try writing your answer first.