AP COMPUTER SCIENCE A Scoring Guide
Unit 2 Progress Check: FRQ
1. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
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.
Consider the following class.
public class LightSequence
{
// attributes not shown
/** The parameter seq is the initial sequence used for
* the light display
*/
public LightSequence(String seq)
{ /* implementation not shown */ }
/** Inserts the string segment in the current sequence,
* starting at the index ind. Returns the new sequence.
*/
public String insertSegment(String segment, int ind)
{ /* implementation not shown */ }
/** Updates the sequence to the value in seq
*/
public void changeSequence(String seq)
{ /* implementation not shown */ }
/** Uses the current sequence to turn the light on and off
* for the show
*/
public void display()
{ /* implementation not shown */ }
}
(a) Write a statement to create a LightSequence object gradShow that has the initial light sequence "0101
0101 0101".
Write the statement below.
AP Computer Science A Page 1 of 7
Scoring Guide
Unit 2 Progress Check: FRQ
Please respond on separate paper, following directions from your teacher.
(b) Write a statement that will call the display method to display the light sequence for the gradShow object.
Write the statement below.
Please respond on separate paper, following directions from your teacher.
(c) Write a statement that will be used to update the gradShow light sequence to "0011 0011 0011".
Write the statement below.
Please respond on separate paper, following directions from your teacher.
(d) Write a code segment that will call the insertSegment method to insert the segment "1111 1111" in the
current sequence for gradShow at index 4. The resulting sequence will be stored in the string resultSeq.
Write the code segment below.
Please respond on separate paper, following directions from your teacher.
(e) Assume that the string oldSeq has been properly declared and initialized and contains the string segment. Write a
code segment that will remove the first occurrence of segment from oldSeq and store it in the string newSeq. Consider
the following examples.
If oldSeq is "1100000111" and segment is "11", then "00000111" should be stored in newSeq.
If oldSeq is "0000011" and segment is "11", then "00000" should be stored in newSeq.
If oldSeq is "1100000111" and segment is "00", then "11000111" should be stored in newSeq.
Write the code segment below. Your code segment should meet all specifications and conform to the examples.
Please respond on separate paper, following directions from your teacher.
(f) Two lights will be arranged on a two-dimensional plane. The vertical distance between the two lights is stored in the
double variable a. The horizontal distance between the two lights is stored in the double variable b.
The straight-line distance between the two lights is given by the formula .
Page 2 of 7 AP Computer Science A