Fiveable
💻AP Computer Science A
​

💻AP Computer Science A

FRQ 1 – Methods and Control Structures
​
Unit 1: Using Objects and Methods
​
Practice FRQ 1 of 191/19
1. This question involves a system for planning course enrollments. A student uses a CoursePlanner object to build a list of courses they wish to take, ensuring they do not exceed a credit limit. The CoursePlanner class uses helper methods to obtain course requests and credit values.
</>Java
public class CoursePlanner
{
    /** The list of enrolled courses, separated by a single space */
    private String courseList; // To be initialized in part (a)

    /** The total number of credit hours for the enrolled courses */
    private int totalCredits; // To be initialized in part (a)

    /**
     * Processes course requests and builds the course list as described in part (a)
     * Precondition: maxCredits > 0
     */
    public CoursePlanner(int maxCredits)
    { /* to be implemented in part (a) */ }

    /**
     * Returns the next course name requested by the student
     * or null if there are no more requests.
     */
    public String getNextCourse()
    { /* implementation not shown */ }

    /**
     * Returns the number of credits for the specified course.
     * Precondition: course is a valid course name.
     */
    public int getCredits(String course)
    { /* implementation not shown */ }

    /**
     * Returns the number of courses in courseList that match the
     * specified department prefix, as described in part (b)
     * Precondition: prefix is not null.
     */
    public int countCourses(String prefix)
    { /* to be implemented in part (b) */ }

    /* There may be instance variables, constructors, and methods
       that are not shown. */
}
a. Write the CoursePlanner constructor, which initializes the instance variables and processes a sequence of course requests. The constructor should initialize courseList to an empty string and totalCredits to 0. It should then repeatedly call the helper method getNextCourse to obtain course names until getNextCourse returns null.
For each course returned by getNextCourse, the constructor should determine if adding the course would cause totalCredits to exceed maxCredits. If the course fits within the limit, its credits (obtained by calling getCredits) are added to totalCredits, and the course name is appended to courseList. If courseList is not empty, a single space should be added before the new course name. If the course does not fit, it is skipped.
The getNextCourse method returns the next requested course name or null if there are no more requests. The getCredits method returns the integer credit value for a given course name.

Example 1

Consider the following sequence of calls made within the constructor with maxCredits = 10.
Method CallReturn ValueAction Taken
getNextCourse()"CS101"Check credits
getCredits("CS101")4Fits (4 <= 10). Add to list.
getNextCourse()"MAT200"Check credits
getCredits("MAT200")4Fits (4+4 <= 10). Add to list.
getNextCourse()"ENG105"Check credits
getCredits("ENG105")3Does not fit (8+3 > 10). Skip.
getNextCourse()nullStop processing.
After this sequence, courseList should be "CS101 MAT200" and totalCredits should be 8.
Complete the CoursePlanner constructor.
/**
 * Processes course requests and builds the course list as described in part (a)
 * Precondition: maxCredits > 0
 */
public CoursePlanner(int maxCredits)
b. Write the countCourses method, which returns the number of times the specified prefix appears in the instance variable courseList. You may assume that courseList contains valid course names separated by spaces and that prefix represents a valid department code (e.g., "CS", "MAT").

Example 1

Assume courseList contains "CS101 CS102 MAT200 CS201".
Method CallReturn Value
countCourses("CS")3
countCourses("MAT")1
countCourses("BIO")0
The prefix "CS" appears 3 times in the list. The prefix "MAT" appears 1 time.
Complete the countCourses method.
/**
 * Returns the number of courses in courseList that match the
 * specified department prefix, as described in part (b)
 * Precondition: prefix is not null.
 */
public int countCourses(String prefix)






Pep