2010 AP
®
COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS
The declaration of the Trail class is shown below. You will write two unrelated methods of the Trail
class.
public class Trail
{
/** Representation of the trail. The number of markers on the trail is markers.length. */
private int[] markers;
/** Determines if a trail segment is level. A trail segment is defined by a starting marker,
* an ending marker, and all markers between those two markers.
* A trail segment is level if it has a difference between the maximum elevation
* and minimum elevation that is less than or equal to 10 meters.
* @param start the index of the starting marker
* @param end the index of the ending marker
* Precondition: 0 <= start < end <= markers.length - 1
* @return true if the difference between the maximum and minimum
* elevation on this segment of the trail is less than or equal to 10 meters;
* false otherwise.
*/
public boolean isLevelTrailSegment(int start, int end)
{ /* to be implemented in part (a) */ }
/** Determines if this trail is rated difficult. A trail is rated by counting the number of changes in
* elevation that are at least 30 meters (up or down) between two consecutive markers. A trail
* with 3 or more such changes is rated difficult.
* @return true if the trail is rated difficult; false otherwise.
*/
public boolean isDifficult()
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors, and methods that are not shown.
}
(a) Write the Trail method isLevelTrailSegment. A trail segment is defined by a starting marker,
an ending marker, and all markers between those two markers. The parameters of the method are the index
of the starting marker and the index of the ending marker. The method will return true if the difference
between the maximum elevation and the minimum elevation in the trail segment is less than or equal to
10 meters.
For the trail shown at the beginning of the question, the trail segment starting at marker 7 and ending at
marker 10 has elevations ranging between 70 and 80
meters. Because the difference between 80 and 70 is
equal to 10, the trail segment is considered level.
The trail segment starting at marker 2 and ending at marker 12 has elevations ranging between 50 and
120 meters. Because the difference betw
een 120 and 50 is greater than 10, this trail segment is not
considered level.
© 2010 The College Board.
Visit the College Board on the Web: www.collegeboard.com.
GO ON TO THE NEXT PAGE.
-7-