7.4.5. Free Response - Sound B

The following is a free response question from 2011. It was question 1 on the exam. You can see all the free response questions from past exams at https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year.

Question 1. Digital sounds can be represented as an array of integer values. For this question, you will write two unrelated methods of the Sound class.

A partial declaration of the Sound class is shown below.

public class Sound
{
 /** the array of values in this sound; guaranteed not to be null */
 private int[] samples;

 /** Changes those values in this sound that have an amplitude
  *  greater than limit */
  *  Values greater than limit are changed to limit.
  *  @param limit the amplitude limit
  *         Precondition: limit >= 0
  *  @return the number of values in this sound that this
  *    method changed
  */
 public int limitAmplitude(int limit)
 { /* to be implemented in part (a) */ }

 /** Removes all silence from the beginning of this sound.
  *  Silence is represented by a value of 0.
  *  Precondition: samples contains at least one nonzero value
  *  Postcondition: the length of samples reflects the
  *                  removal of starting silence
  */
 public void trimSilenceFromBeginning()
 { /* to be implemented in part (b) */ }

 // There may be instance variables, constructors, and methods
 // that are not shown.
}

Part b. Recorded sound often begins with silence. Silence in a sound is represented by a value of 0.

Write the method trimSilenceFromBeginning that removes the silence from the beginning of a sound. To remove starting silence, a new array of values is created that contains the same values as the original samples array in the same order but without the leading zeros. The instance variable samples is updated to refer to the new array. For example, suppose the instance variable samples refers to the following array.

../_images/soundTable3.png

After trimSilenceFromBeginning has been called, the instance variable samples will refer to the following array.

../_images/soundTable4.png

7.4.5.1. How to Solve This

Click to reveal problems and the algorithm to help you write your solution.

7.4.5.2. Mixed Up Code

Click to reveal the Mixed Up Code for the solution of this problem.

7.4.5.3. Try and Solve Part B

FRQ Sound B: Finish writing the method trimSilenceFromBeginning below that removes the silence from the beginning of a sound. To remove starting silence, a new array of values is created that contains the same values as the original samples array in the same order but without the leading zeros. The instance variable samples is updated to refer to the new array.

You have attempted of activities on this page