0% found this document useful (0 votes)
38 views12 pages

套题2 FRQ

the AP computer science application

Uploaded by

nafijamim511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views12 pages

套题2 FRQ

the AP computer science application

Uploaded by

nafijamim511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

3.

A CodeWordChecker object can be constructed with three parameters: two


integers and a string. The first two parameters specify the minimum and
maximum code word lengths, respectively, and the third parameter specifies a
string that must not occur in the code word.
A CodeWordChecker object can also be constructed with a single parameter
that specifies a string that must not occur in the code word; in this case the
minimum and maximum lengths will default to 6 and 20, respectively.
The following examples illustrate the behavior of CodeWordChecker objects.
Example 1

CodeWordChecker sc1 = new CodeWordChecker(5, 8 );

Valid code words have 5 to 8 characters and must not include the string

Method call Return value Explanation

sc1.isValid true The code word is valid

sc1.isValid ) false The code word contains

sc1.isValid false The code word is too short

sc1.isValid false The code word is too long

Example 2

CodeWordChecker sc2 = new CodeWordChecker("pass")

Valid code words must not include the string Because the bounds are
not specified, the length bounds are 6 and 20, inclusive.

Method call Return value Explanation

sc2.isValid("MyPass true The code word is valid

false The code word contains


sc2.isValid("Mypassport )
pass

sc2.isValid("happy false The code word is too short

sc2.isValid("1000,000,000, false The code word is too long


000,000,000,000

Write the complete CodeWordChecker class. Your implementation must


meet all specifications and conform all examples.
AP Computer Science A Test Booklet

FRQ-2D Array(11) Name

1. Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE
WRITTEN IN JAVA.

Notes:

Assume that the classes listed in the Java Quick Reference have been imported where
appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and
that methods are called only when their preconditions are satisfied.
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.

A two-dimensional array of temperatures is represented in the above class.

a) Write method computeTemp, which computes and returns the new temperature for a given element
of temps according to the following rules.

1. If the element is in the border of the array (in the first row or last row or first column or last
column), the new temperature is the same as the old temperature.
2. Otherwise, the new temperature is the average (arithmetic mean) of the temperatures of the four
adjacent values in the table (located above, below, to the left, and to the right of the element).

Copyright © 2021. The College Board. These materials are part of a College Board program. Use or distribution of these materials online or in print beyond your
school’s participation in the program is prohibited.
Page 1 of 3
AP Computer Science A Test Booklet

FRQ-2D Array(11)

If temps is the table shown below, temps.length is 5 and temps[0].length is 6.

The following table shows the results of several calls to computeTemp.

(b) Write method updateAllTemps, which computes the new temperature for every element of
temps.The new values should be based on the original values, so it will be necessary to create another
two-dimensional array in which to store the new values. Once all the computations are complete, the
new values should replace the corresponding positions of temps. Method updateAllTemps also
determines whether every new temperature is within tolerance of the corresponding old temperature
(i.e., the absolute value of the difference between the old temperature and the new temperature is less
than or equal to tolerance). If so, it returns true; otherwise, it returns false.

If temps contains the values shown in the first table below, then the call updateAllTemps(0.01)
should update temps as shown in the second table.

Copyright © 2021. The College Board. These materials are part of a College Board program. Use or distribution of these materials online or in print beyond your
school’s participation in the program is prohibited.
Page 2 of 3
AP Computer Science A Test Booklet

FRQ-2D Array(11)

In the example shown, the call updateAllTemps (0.01) should return false because there are several new
temperatures that are not within the given tolerance of the corresponding old temperature. For example,
the updated value in temps[2][3] is 37.5, the original value in temps[2][3] was 40.0, and the absolute
value of (37.5 - 40.0) is greater than the value of tolerance (0.01).

Assume that computeTemp works as specified, regardless of what you wrote in part (a).

Please respond on separate paper, following directions from your teacher.

Copyright © 2021. The College Board. These materials are part of a College Board program. Use or distribution of these materials online or in print beyond your school’s participation in the
program is prohibited.
Page 3 of 3

You might also like