Fiveable
💻AP Computer Science A
​

💻AP Computer Science A

FRQ 1 – Methods and Control Structures
​
Unit 1: Using Objects and Methods
​
AP CS A FRQ Types & Units

Each FRQ type tests specific skills taught in particular units. Here's why certain units appear for each question type:

FRQFocusUnitsWhy
FRQ 1Methods and Control Structures1-2Tests method writing, loops, conditionals - foundational programming from Units 1-2
FRQ 2Classes3Tests class design, constructors, instance variables - all Unit 3 OOP content
FRQ 3Array/ArrayList4Tests 1D array and ArrayList traversal, manipulation - Unit 4 content
FRQ 42D Array4Tests 2D array traversal and manipulation - Unit 4 content

This mapping reflects College Board's exam structure - each FRQ type tests specific skills that are taught in particular units.

Practice FRQ 1 of 191/19

1. This question involves the TripPlanner class, which is used to plan a journey and calculate the total travel time. The TripPlanner class uses helper methods to determine the sequence of stops and the travel time to each stop.

</>Java
public class TripPlanner
{
    private String itinerary; // To be initialized in part (a)
    private int totalMinutes; // To be initialized in part (a)

    /**
     * Returns the name of the next stop to visit, or null if there are no more stops.
     * Postcondition: Returns a String representing the next stop or null.
     */
    public String getNextStop()
    { /* implementation not shown */ }

    /**
     * Returns the time in minutes to travel to the specified stop.
     * Precondition: stop is not null.
     */
    public int getTravelTime(String stop)
    { /* implementation not shown */ }

    /**
     * Creates a TripPlanner object with a starting location.
     * Builds the itinerary and calculates totalMinutes as described in part (a).
     * Precondition: start is a non-empty string.
     */
    public TripPlanner(String start)
    { /* to be implemented in part (a) */ }

    /**
     * Returns a simplified code for the trip based on the itinerary,
     * as described in part (b).
     * Precondition: itinerary contains at least one location.
     *               Each location in itinerary is at least 3 characters long.
     *               Locations in itinerary are separated by " -> ".
     */
    public String generateCode()
    { /* to be implemented in part (b) */ }
}
a.

Write the TripPlanner constructor, which initializes the instance variables. The constructor should set itinerary to the start parameter and totalMinutes to 0. Then, it should repeatedly call the getNextStop method to obtain the next stop. If a stop is returned (i.e., not null), the constructor should append the string " -> " followed by the stop name to itinerary and add the travel time for that stop (obtained by calling getTravelTime) to totalMinutes. The process continues until getNextStop returns null.

A helper method, getNextStop, has been provided to obtain the next destination. Consecutive calls to getNextStop are guaranteed to eventually return null.

Example 1

Consider the following calls to getNextStop and getTravelTime made within the constructor.
Method CallReturn Value
getNextStop()"Museum"
getTravelTime("Museum")20
getNextStop()"Cafe"
getTravelTime("Cafe")45
getNextStop()null
Based on these return values, a call to new TripPlanner("Hotel") should set the instance variable itinerary to "Hotel -> Museum -> Cafe" and should set the instance variable totalMinutes to 65.
Complete the TripPlanner constructor. You must use getNextStop and getTravelTime appropriately in order to receive full credit.
/**
 * Creates a TripPlanner object with a starting location.
 * Builds the itinerary and calculates totalMinutes as described in part (a).
 * Precondition: start is a non-empty string.
 */
public TripPlanner(String start)
b.

Write the generateCode method, which returns a string code representing the trip. The code is formed by taking the first three letters of each location in itinerary, converting them to uppercase, and separating them with hyphens ("-"). You may assume that itinerary is formatted correctly with locations separated by " -> " and that every location name has at least three characters.

Example 1

Assume the instance variable itinerary has been correctly initialized.
Value of itineraryReturn Value of generateCode()
"Boston -> New York -> Washington""BOS-NEW-WAS"
"Home -> Gym""HOM-GYM"
"Paris""PAR"
The method extracts the first three letters of each location, converts them to uppercase, and joins them with hyphens.
Complete the generateCode method.
/**
 * Returns a simplified code for the trip based on the itinerary,
 * as described in part (b).
 * Precondition: itinerary contains at least one location.
 *               Each location in itinerary is at least 3 characters long.
 *               Locations in itinerary are separated by " -> ".
 */
public String generateCode()






Pep

essential ap study content awaits..

Features
Testimonials
Testimonials
start studying →
FRQ Directions
Free Response Question Practice

This practice environment simulates the AP AP Computer Science A Free Response Questions section. Here are some guidelines:

  • Read each question carefullybefore responding. Pay attention to command verbs like "identify," "explain," "analyze," or "evaluate."
  • Use the timer to practice time management. You can pause, restart, or hide the timer as needed.
  • Mark for Review if you want to come back to a question later.
  • Your responses are saved automatically as you type. You can also use the drawing tool for questions that require diagrams or graphs.
  • Use the toolbar for formatting options like bold, italic, subscript, and superscript.
  • Navigate between questions using the Previous and Next buttons at the bottom of the screen.

Tip: Answer all parts of each question. Partial credit is often available, so even if you are unsure, provide what you know.