Language:EN
Pages: 19
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Page 1 Preview
int minute private void reserveblock period

Int minute private void reserveblock period

AP® Computer Science A 2023 Free-Response Questions

SECTION II
Time—1 hour and 30 minutes
4 Questions

• In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.

GO ON TO THE NEXT PAGE.

1. This question involves the AppointmentBook class, which provides methods for students to schedule appointments 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 within the same period.

GO ON TO THE NEXT PAGE. © 2023 College Board.

public class AppointmentBook
{
/**
* Returns true if minute in period is available for an appointment and returns * false otherwise

* Preconditions: 1 <= period <= 8; */

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, 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.

Visit College Board on the web: collegeboard.org.

4

Minutes in Period 2 Available?
0–9 (10 minutes)
10–14 (5 minutes)

Yes

15–29 (15 minutes)

No

30–44 (15 minutes)
45–49 (5 minutes)
50–59 (10 minutes)

The method call findFreeBlock(2, 9) 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.

If there are multiple parts to this question, write the part letter with your response.

GO ON TO THE NEXT PAGE.

(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.

The method call makeAppointment(2, 4, 22) returns true and results in the minutes 5 through 26, inclusive, in period 4 being marked as unavailable.

The method call makeAppointment(3, 4, 3) returns true and results in the minutes 0 through 2, inclusive, in period 3 being marked as unavailable.

Period Minutes
2

No

2

25–29 (5 minutes)

2 30–59 (30 minutes)
3
3

3–14 (12 minutes)

Yes

3 15–40 (26 minutes)
3 41–59 (19 minutes)
4
4

27–29 (3 minutes)

Yes

4 30–43 (14 minutes)
4 44–59 (16 minutes)

Visit College Board on the web: collegeboard.org.

6

* calls reserveBlock to reserve the block of minutes and returns true; otherwise

* returns false.

Begin your response at the top of a new page in the separate Free Response booklet and fill in the appropriate circle at the top of each page to indicate the question number.

If there are multiple parts to this question, write the part letter with your response.

© 2023 College Board.

Visit College Board on the web: collegeboard.org.

A sign contains as many lines as are necessary to display the entire message. The message is split among the lines of the sign without regard to spaces or punctuation. Only the last line of the sign may contain fewer characters than the width indicated by the constructor parameter.

The following are examples of a message displayed on signs of different widths. Assume that in each example, the sign is declared with the width specified in the first column of the table and with the message "Everything on sale, please come in" , which contains 34 characters.

GO ON TO THE NEXT PAGE. © 2023 College Board.

Visit College Board on the web: collegeboard.org.

String str;

int x;

3

width 3.

str = sign1.getLines(); "ABC;222;DE" Semicolons separate the text displayed on the first, second, and third lines of the sign.

width 10.

"ABCD"

The message for sign3 contains 6

Sign("ABCDEF", 6);

width 6.

str = sign3.getLines(); "ABCDEF"

No semicolon appears, since the text to be

0

There is no text to display.

x = sign4.numberOfLines();

str = sign4.getLines(); null
Sign("AB_CD_EF", 2);

The sign needs four lines to display the

GO ON TO THE NEXT PAGE.

© 2023 College Board.

If there are multiple parts to this question, write the part letter with your response.

GO ON TO THE NEXT PAGE.

3. This question involves the analysis of weather data. The following WeatherData class has an instance variable, temperatures, which contains the daily high temperatures recorded on consecutive days at a particular location. The class also contains methods used to analyze that data. You will write two methods of the WeatherData class.

public class WeatherData
{
/** Guaranteed not to be null and to contain only non-null entries */ private ArrayList<Double> temperatures;

GO ON TO THE NEXT PAGE.

© 2023 College Board.

The three shaded values shown would be removed by the method call cleanData(85.0, 120.0).

/**
* Cleans the data by removing from temperatures all values that are less than * lower and all values that are greater than upper, as described in part (a)  */
public void cleanData(double lower, double upper)

Begin your response at the top of a new page in the separate Free Response booklet and fill in the appropriate circle at the top of each page to indicate the question number.

12

AP® Computer Science A 2023 Free-Response Questions

In the following sample contents of temperatures, all heat waves based on the threshold temperature of 95.2 are shaded. The method call longestHeatWave(95.2) would return 4, which is the length of the longest heat wave.

public int longestHeatWave(double threshold)

Begin your response at the top of a new page in the separate Free Response booklet and fill in the appropriate circle at the top of each page to indicate the question number.

public void cleanData(double lower, double upper) public int longestHeatWave(double threshold)

GO ON TO THE NEXT PAGE.

4. This question involves pieces of candy in a box. The Candy class represents a single piece of candy.

public class Candy
{
/** Returns a String representing the flavor of this piece of candy */ public String getFlavor()
{ /* implementation not shown */ }

/**
* Moves one piece of candy in column col, if necessary and possible, so that the box * element in row 0 of column col contains a piece of candy, as described in part (a). * Returns false if there is no piece of candy in column col and returns true otherwise. * Precondition: col is a valid column index in box.

*/
public boolean moveCandyToFirstRow(int col) { /* to be implemented in part (a)*/ }

Visit College Board on the web: collegeboard.org.

14

In the following example, the grid represents the contents of box. An empty square in the grid is null in box. A non-empty square in the grid represents a box element that contains a Candy object. The string in the square of the grid indicates the flavor of the piece of candy.

Visit College Board on the web: collegeboard.org.

15

/**
* Moves one piece of candy in column col, if necessary and possible, so that the box * element in row 0 of column col contains a piece of candy, as described in part (a). * Returns false if there is no piece of candy in column col and returns true otherwise.

Precondition: col is a valid column index in box. *
*/
public boolean moveCandyToFirstRow(int col)

public String getFlavor()

public class BoxOfCandy

Visit College Board on the web: collegeboard.org.

16

The following grid shows the contents of box before any of the removeNextByFlavor method calls.

Visit College Board on the web: collegeboard.org.

17

The method call removeNextByFlavor("grape") returns null because no grape-flavored candy is found. The contents of box are unchanged.

Complete the removeNextByFlavor method.

public Candy removeNextByFlavor(String flavor)

Begin your response at the top of a new page in the separate Free Response booklet and fill in the appropriate circle at the top of each page to indicate the question number.

public class BoxOfCandy

private Candy[][] box

18

AP® Computer Science A 2023 Free-Response Questions

19

You are viewing 1/3rd of the document.Purchase the document to get full access instantly

Immediately available after payment
Both online and downloadable
No strings attached
How It Works
Login account
Login Your Account
Place in cart
Add to Cart
send in the money
Make payment
Document download
Download File
img

Uploaded by : Dearbhla O'Byrne

PageId: DOCF890AD2