Author(wechat): bigshuang2020
ap csa tutor, AP计算机科学a老师
国际教育编程老师, 擅长答疑讲解,带学生实践学习。热爱创作,作品:ap csa原创教案,真题梳理汇总,FRQ专题冲刺。
2023 FRQ Question 1
This question involves the appointmentBook
class, which provides methods for students to schedule appointents with their teacher. Appointments can be scheduled during one of eight class periods during the school day, numbered 1 through 8. A requested appointment has a duration, which is the number of minutes the appointment will last. The 60 minutes within a period are numbered 0 through 59. In order for an appointment to be scheduled, the teacher must have a block of consecutive, available minutes that contains at least the requested number of minutes in a requested period. Scheduled appointments must start and end with in the same period.
The AppointmentBook
class contains two helper methods, isMinuteFree
and reserveBlock
. You will write two additional methods of the AppointmentBook
class.
public class AppointmentBook {/*** Returns true if the minute in the period is available for an appointment,* and returns false otherwise.* Preconditions: 1 <= period <= 8; 0 <= minute <= 59.*/private boolean isMinuteFree(int period, int minute) {// Implementation not shown}/*** Marks the block of minutes that starts at startMinute in period* and is duration minutes long as reserved for an appointment.* Preconditions: 1 <= period <= 8; 0 <= startMinute <= 59; 1 <= duration <= 60.*/private void reserveBlock(int period, int startMinute, int duration) {// Implementation not shown}/*** Searches for the first block of duration as described in part (a).* during the given period.* returns the first minute in the block if such a block is found * or return -1 if no such block is found.* Preconditions: 1 <= period <= 8; 1 <= duration <= 60.*/public int findFreeBlock(int period, int duration) {// To be implemented in part (a)}/*** Searches periods from startPeriod to endPeriod, inclusive, for a block of* duration free minutes. If such a block is found, calls reserveBlock* to reverse the block of minutes and returns true; otherwise returns false* Preconditions: 1 <= startPeriod <= endPeriod <= 8; 1 <= duration <= 60.*/public boolean makeAppointment(int startPeriod, int endPeriod, int duration) {// To be implemented in part (b)}//There may be instance variables, constructors, and methods that are not shown
}
Part (a):
Write the findFreeBlock
method, which searches period
for the first block of free minutes that is duration
minutes long. If such a block is found, findFreeBlock
returns the first minute in the block; otherwise, findFreeBlock
returns -1
. The findFreeBlock
method uses the helper method isMinuteFree
, which returns true
if a particular minute is available to be included in a new appointment and returns false
if the minute is unavailable.
Consider the following list of unavailable and available minutes in period 2:
Minutes in Period 2 | Available? |
---|---|
0-9 (10 minutes) | No |
10-14 (5 minutes) | Yes |
15-29 (15 minutes) | No |
30-44 (15 minutes) | Yes |
45-49 (5 minutes) | No |
50-59 (10 minutes) | Yes |
The method call findFreeBlock(2, 15)
would return 30
to indicate that a 15-minute block starting with minute \(30\) is available. No steps should be taken as a result of the call to findFreeBlock
to mark those \(15\) minutes as unavailable.
The method call findFreeBlock(2, 29)
would also return 30
. Whenever there are multiple blocks that satisfy the requirement, the earliest starting minute is returned.
The method call findFreeBlock(2, 20)
would return -1
, since no 20-minute block of available minutes exists in period 2.
Complete method findFreeBlock
. You must use isMinuteFree
appropriately in order to receive full credit.
/*** Searches for the first block of duration free minutes during period, * as described in part (a). Returns the first minute in the block if such a block * is found or returns -1 if no such block is found.* Preconditions: 1 <= period <= 8; 1 <= duration <= 60.*/
public int findFreeBlock(int period, int duration) {// To be implemented in part (a)
}
Part (b)
Write the makeAppointment
method, which searches the periods from startPeriod
to endPeriod
, inclusive, for the earliest block of duration available minutes in the lowest-numbered period. If such a block is found, the makeAppointment
method calls the helper method reserveBlock
to mark the minutes in the block as unavailable and returns true
. If no such block is found, the makeAppointment
method returns false
.
Consider the following list of unavailable and available minutes in periods 2, 3, and 4 and three successive calls to makeAppointment
:
Period | Minutes | Available? |
---|---|---|
2 | 0-24 (25 minutes) | No |
2 | 25-29 (5 minutes) | Yes |
2 | 30-59 (30 minutes) | No |
3 | 0-14 (15 minutes) | Yes |
3 | 15-40 (26 minutes) | No |
3 | 41-59 (19 minutes) | Yes |
4 | 0-4 (5 minutes) | No |
4 | 5-29 (25 minutes) | Yes |
4 | 30-43 (14 minutes) | No |
4 | 44-59 (16 minutes) | Yes |
- The method call
makeAppointment(2, 4, 22)
returnstrue
and results in the minutes \(5\) through \(26\), inclusive, in period 4 being marked as unavailable. - The method call
makeAppointment(3, 4, 3)
returnstrue
and results in the minutes \(0\) through \(2\), inclusive, in period 3 being marked as unavailable. - The method call
makeAppointment(2, 4, 30)
returnsfalse
, since there is no block of \(30\) available minutes in periods 2, 3, or 4.
The following shows the updated list of unavailable and available minutes in periods 2, 3, and 4 after the three example method calls are complete:
Period | Minutes | Available? |
---|---|---|
2 | 0-24 (25 minutes) | No |
2 | 25-29 (5 minutes) | Yes |
2 | 30-59 (30 minutes) | No |
3 | 0-2 (3 minutes) | No |
3 | 3-14 (12 minutes) | Yes |
3 | 15-40 (26 minutes) | No |
3 | 41-59 (19 minutes) | Yes |
4 | 0-26 (27 minutes) | No |
4 | 27-29 (3 minutes) | Yes |
4 | 30-43 (14 minutes) | No |
4 | 44-59 (16 minutes) | Yes |
Complete method makeAppointment
. Assume that findFreeBlock
works as intended, regardless of what you wrote in part (a). You must use findFreeBlock
and reserveBlock
appropriately in order to receive full credit.
/*** Searches periods from startPeriod to endPeriod, inclusive, for a block* of duration free minutes, as described in part (b). If such a block is found,* calls reserveBlock to reserve the block of minutes and returns true;* otherwise returns false.* Preconditions: 1 <= startPeriod <= endPeriod <= 8; 1 <= duration <= 60.*/
public boolean makeAppointment(int startPeriod, int endPeriod, int duration) {// To be implemented in part (b)
}
2022 FRQ Question 1
This question involves simulation of the play and scoring of a single-player video game. In the game, a player attempts to complete three levels. A level in the game is represented by the Level
class.
public class Level {/** * Returns true if the player reached the goal on this level * and returns false otherwise */public boolean goalReached() {/* implementation not shown */}/** * Returns the number of points (a positive integer) recorded for this level */public int getPoints() {/* implementation not shown */}
}
// There may be instance variables, constructors, and methods that are not shown.
Play of the game is represented by the Game
class. You will write two methods of the Game
class.
public class Game {private Level levelOne;private Level levelTwo;private Level levelThree;/** Postcondition: All instance variables have been initialized. */public Game() {/* implementation not shown */}/*** Returns true if this game is a bonus game and returns false otherwise.*/public boolean isBonus() {/* implementation not shown */}/*** Simulates the play of this Game (consisting of three levels) * and updates all relevant game data.*/public void play() {/* implementation not shown */}/*** Returns the score earned in the most recently played game, * as described in part (a).*/public int getScore() {/* to be implemented in part (a) */}/*** Simulates the play of `num` games and returns the highest score earned, * as described in part (b).* Precondition: num > 0*/public int playManyTimes(int num) {/* to be implemented in part (b) */}
}
// There may be instance variables, constructors, and methods that are not shown.
(a) Write the getScore
Method
Write the getScore
method, which returns the score for the most recently played game. Each game consists of three levels. The score for the game is computed using the following helper methods:
- The
isBonus
method of theGame
class returnstrue
if this is a bonus game and returnsfalse
otherwise. - The
goalReached
method of theLevel
class returnstrue
if the goal has been reached on a particular level and returnsfalse
otherwise. - The
getPoints
method of theLevel
class returns the number of points recorded on a particular level.
Scoring Rules:
- Level one points are earned only if the level one goal is reached.
Level two points are earned only if both the level one and level two goals are reached.
Level three points are earned only if the goals of all three levels are reached. - The score for the game is the sum of the points earned for levels one, two, and three.
- If the game is a bonus game, the score for the game is tripled.
Example Table
Complete the getScore
method:
/** Returns the score earned in the most recently played game, as described in part (a). */
public int getScore()
(b) Write the playManyTimes
Method
Write the playManyTimes
method, which simulates the play of num
games and returns the highest game score earned. For example, if the four plays of the game that are simulated as a result of the method call playManyTimes(4)
earn scores of 75, 50, 90, and 20, then the method should return 90.
Play of the game is simulated by calling the helper method play
. Note that if play
is called only once, followed by multiple consecutive calls to getScore
, each call to getScore
will return the score earned in the single simulated play of the game.
Complete the playManyTimes
method:
/** * Simulates the play of num games and returns the highest score earned, * as described in part (b). * Precondition: num > 0 */
public int playManyTimes(int num)
Class Information for This Question
public class Level {public boolean goalReached();public int getPoints();
}public class Game {private Level levelOne;private Level levelTwo;private Level levelThree;public Game();public boolean isBonus();public void play();public int getScore();public int playManyTimes(int num);
}
2021 FRQ Q1
This question involves the WordMatch
class, which stores a secret string and provides methods that compare other strings to the secret string. You will write two methods in the WordMatch
class.
public class WordMatch {/** The secret string. */private String secret;/** Constructs a WordMatch object with the given secret string of lowercase letters. */public WordMatch(String word) {/* implementation not shown */}/** * Returns a score for guess, as described in part (a).* Precondition: 0 < guess.length() <= secret.length()*/public int scoreGuess(String guess) {/* to be implemented in part (a) */}/** * Returns the better of two guesses, as determined by scoreGuess and the rules for a * tie-breaker that are described in part (b).* Precondition: guess1 and guess2 contain all lowercase letters.* guess1 is not the same as guess2.*/public String findBetterGuess(String guess1, String guess2) {/* to be implemented in part (b) */}
}
Part (a) - Implementing scoreGuess
To determine the score to be returned, scoreGuess
finds the number of times that guess
occurs as a substring of secret
and then multiplies that number by the square of the length of guess
. Occurrences of guess
may overlap within secret
.
Assume that the length of guess
is less than or equal to the length of secret
and that guess
is not an empty string.
The following examples show declarations of a WordMatch
object. The tables show the outcomes of some possible calls to the scoreGuess
method.
Example 1
WordMatch game = new WordMatch("mississippi");
Value of guess |
Number of Substring Occurrences | Score Calculation | Return Value |
---|---|---|---|
"i" |
4 | 4 * 1 * 1 = 4 | 4 |
"iss" |
2 | 2 * 3 * 3 = 18 | 18 |
"issipp" |
1 | 1 * 6 * 6 = 36 | 36 |
"mississippi" |
1 | 1 * 11 * 11 = 121 | 121 |
Example 2
WordMatch game = new WordMatch("aaaabb");
Value of guess |
Number of Substring Occurrences | Score Calculation | Return Value |
---|---|---|---|
"a" |
4 | 4 * 1 * 1 = 4 | 4 |
"aa" |
3 | 3 * 2 * 2 = 12 | 12 |
"aaa" |
2 | 2 * 3 * 3 = 18 | 18 |
"aabb" |
1 | 1 * 4 * 4 = 16 | 16 |
"b" |
0 | 0 * 1 * 1 = 0 | 0 |
Part (b) - Implementing findBetterGuess
The findBetterGuess
method returns the better guess of its two String
parameters, guess1
and guess2
. If the scoreGuess
method returns different values for guess1
and guess2
, then the guess with the higher score is returned. If the scoreGuess
method returns the same value for guess1
and guess2
, then the alphabetically greater guess is returned.
The following example shows a declaration of a WordMatch
object and the outcomes of some possible calls to the scoreGuess
and findBetterGuess
methods.
WordMatch game = new WordMatch("concatenation");
Method Call | Return Value | Explanation |
---|---|---|
game.scoreGuess("ten") |
9 | 1 * 3 * 3 = 9 |
game.scoreGuess("nation") |
36 | 1 * 6 * 6 = 36 |
game.findBetterGuess("ten", "nation") |
"nation" |
Since scoreGuess returns 36 for "nation" and 9 for "ten" , the guess with the greater score, "nation" , is returned. |
game.scoreGuess("con") |
9 | 1 * 3 * 3 = 9 |
game.scoreGuess("cat") |
9 | 1 * 3 * 3 = 9 |
game.findBetterGuess("con", "cat") |
"con" |
Since scoreGuess returns 9 for both "con" and "cat" , the alphabetically greater guess, "con" , is returned. |
Class information for this question
public class WordMatch{private String secretpublic wordMatch(String word)public int scoreGuess(String guess)public String fndBetterGuess(String guess1,String guess2)
}
2020-FRQ Q1
A mathematical sequence is an ordered list of numbers. This question involves a sequence called a hailstone sequence. If \(n\) is the value of a term in the sequence, then the following rules are used to find the next term, if one exists:
- If \(n = 1\), the sequence terminates.
- If \(n\) is even, then the next term is \(\cfrac{n}{2}\).
- If \(n\) is odd, then the next term is $ 3n + 1 $.
For this question, assume that when the rules are applied, the sequence will eventually terminate with the term \(n = 1\).
The following are examples of hailstone sequences.
Example 1: \(5, 16, 8, 4, 2, 1\)
- The first term is 5, so the second term is \(5 \times 3 + 1 = 16\).
- The second term is 16, so the third term is \(\cfrac{16}{2} = 8\).
- The third term is 8, so the fourth term is \(\cfrac{8}{2} = 4\).
- The fourth term is 4, so the fifth term is \(\cfrac{4}{2} = 2\).
- The fifth term is 2, so the sixth term is \(\cfrac{2}{2} = 1\).
Since the sixth term is 1, the sequence terminates.
Example 2: \(8, 4, 2, 1\)
- The first term is 8, so the second term is \(\cfrac{8}{2} = 4\).
- The second term is 4, so the third term is \(\cfrac{4}{2} = 2\).
- The third term is 2, so the fourth term is \(\cfrac{2}{2} = 1\).
Since the fourth term is 1, the sequence terminates.
The Hailstone class
, shown below, is used to represent a hailstone sequence. You will write three methods in the Hailstone
class.
public class Hailstone {/** * Returns the length of a hailstone sequence * that starts with n, as described in part (a). * Precondition: n > 0 */ public static int hailstoneLength(int n) { // to be implemented in part (a)} /** * Returns true if the hailstone sequence * that starts with n is considered long * and false otherwise, as described in part (b). * Precondition: n > 0 */ public static boolean isLongSeq(int n) { // to be implemented in part (b)} /** * Returns the proportion of the first n hailstone sequences * that are considered long, as described in part (c). * Precondition: n > 0 */ public static double propLong(int n) { // to be implemented in part (c)}// There may be instance variables, constructors, // and methods not shown.
}
Part a
The length of a hailstone sequence is the number of terms it contains. For example, the hailstone sequence in example 1 \((5, 16, 8, 4, 2, 1)\) has a length of \(6\) and the hailstone sequence in example 2 \((8, 4, 2, 1)\) has a length of \(4\).
Write the method hailstoneLength(int n)
, which returns the length of the hailstone sequence that starts with n
.
Part b
A hailstone sequence is considered long if its length is greater than its starting value. For example, the hailstone sequence in example 1 \((5, 16, 8, 4, 2, 1)\) is considered long because its length (\(6\)) is greater than its starting value (\(5\)). The hailstone sequence in example 2 \((8, 4, 2, 1)\) is not considered long because its length (\(4\)) is less than or equal to its starting value (\(8\)).
Write the method isLongSeq(int n)
, which returns true
if the hailstone sequence starting with n
is considered long and returns false
otherwise. Assume that hailstoneLength
works as intended, regardless of what you wrote in part (a). You must use hailstoneLength
appropriately to receive full credit.
Part c
The method propLong(int n)
returns the proportion of long hailstone sequences with starting values between \(1\) and \(n\), inclusive.
Consider the following table, which provides data about the hailstone sequences with starting values between \(1\) and \(10\), inclusive.
Starting Value | Terms in the Sequence | Length of the Sequence | Long? |
---|---|---|---|
1 | 1 | 1 | No |
2 | 2, 1 | 2 | No |
3 | 3, 10, 5, 16, 8, 4, 2, 1 | 8 | Yes |
4 | 4, 2, 1 | 3 | No |
5 | 5, 16, 8, 4, 2, 1 | 6 | Yes |
6 | 6, 3, 10, 5, 16, 8, 4, 2, 1 | 9 | Yes |
7 | 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 | 17 | Yes |
8 | 8, 4, 2, 1 | 4 | No |
9 | 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 | 20 | Yes |
10 | 10, 5, 16, 8, 4, 2, 1 | 7 | No |
The method call Hailstone.propLong(10)
returns \(0.5\), since \(5\) of the \(10\) hailstone sequences shown in the table are considered long.
Write the propLong
method. Assume that hailstoneLength
and isLongSeq
work as intended, regardless of what you wrote in parts (a) and (b). You must use isLongSeq
appropriately to receive full credit.
2019 FRQ Q1
The APCalendar
class contains methods used to calculate information about a calendar. You will write two methods of the class.
public class APCalendar {/** Returns true if year is a leap year and false otherwise. */private static boolean isLeapYear(int year) {/* implementation not shown */}/** * Returns the number of leap years between year1 and year2, inclusive. * Precondition: 0 <= year1 <= year2 */public static int numberOfLeapYears(int year1, int year2) {/* to be implemented in part (a) */}/** * Returns the value representing the day of the week for the first day of year, * where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday.*/private static int firstDayOfYear(int year) {/* implementation not shown */}/** * Returns n, where month, day, and year specify the nth day of the year. * Returns 1 for January 1 (month = 1, day = 1) of any year. * Precondition: The date represented by month, day, year is a valid date. */private static int dayOfYear(int month, int day, int year) {/* implementation not shown */}/** * Returns the value representing the day of the week for the given date (month, day, year), * where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday. * Precondition: The date represented by month, day, year is a valid date.*/public static int dayOfWeek(int month, int day, int year) {/* to be implemented in part (b) */}// There may be instance variables, constructors, and other methods not shown.
}
Part (a)
Write the static method numberOfLeapYears
, which returns the number of leap years between year1
and year2
, inclusive. In order to calculate this value, a helper method is provided for you:
isLeapYear(year)
returnstrue
ifyear
is a leap year andfalse
otherwise.
Complete method numberOfLeapYears
below
/** * Returns the number of leap years between year1 and year2, inclusive. * Precondition: 0 <= year1 <= year2 */
public static int numberOfLeapYears(int year1, int year2) {// Implementation goes here
}
Part (b)
Write the static method dayOfWeek
, which returns the integer value representing the day of the week for the given date (month, day, year
), where 0
denotes Sunday, 1
denotes Monday, ..., and 6
denotes Saturday.
For example:
- 2019 began on a Tuesday, so January 5, 2019, was a Saturday.
dayOfWeek(1, 5, 2019) // returns 6
- January 10, 2019, was a Thursday.
dayOfWeek(1, 10, 2019) // returns 4
Two helper methods are provided:
firstDayOfYear(year)
returns the integer value representing the day of the week for the first day ofyear
.- Example:
firstDayOfYear(2019) // returns 2 (Tuesday)
- Example:
dayOfYear(month, day, year)
returnsn
, wheremonth, day, and year
specify then
th day of the year.- Example:
dayOfYear(3, 1, 2017) // returns 60
- Example:
dayOfYear(3, 1, 2016) // returns 61
(leap year)
- Example:
Complete method dayOfWeek
below:
/** * Returns the value representing the day of the week for the given date * (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday. * Precondition: The date represented by month, day, year is a valid date. */
public static int dayOfWeek(int month, int day, int year) {// Implementation goes here
}