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://apstudent.collegeboard.org/apcourse/ap-computer-science-a/exam-practice.

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

How to Solve This

  1. You will need to loop through each element in the array until you reach a non-zero element. You will also need to keep track of the number of leading zeros.
  2. Remember that you must replace the samples array with a new array without the leading zeros. How do you create an array of a particular size?
    7-14-1: Which loop would be best for this problem?
  • (A) while
  • A while loop is the best choice when you don't know the number of times you need to loop.
  • (B) for
  • You could use a for loop, but typically a while loop is used when you want to loop while a condition is true.
  • (C) for-each
  • A for-each loop would only allow you to loop through all the values, but you first want to loop while there are leading zeros.
    7-14-2: Which is the correct code for creating an integer array variable named samples2 and setting it to refer to an array of integers of size count?
  • (A) int[] samples2;
  • This only declares the variable samples2 which will refer to an array of integers, it doesn't create the array object.
  • (B) int[] samples2 = new Array(count);
  • The new keyword is not used to create an array.
  • (C) int[] samples2 = new int[count];
  • This will create an array of integers of size count and a variable named samples2 which will refer to that array.

Mixed Up Code

        7-14-3: The method trimSilenceFromBeginning below contains correct code for one solution to this problem, but it is mixed up.  Drag the code blocks from the left to the right and put them in order with the correct indention so that the code would work correctly.public void trimSilenceFromBeginning()
{
    int i = 0;
---
    while (this.samples[i] == 0)
    {
---
        i++;
---
    } // end while
---
    int samplesLen = this.samples.length;
    int[] newSamples = new int[samplesLen - i];
---
    for (int j = 0; j < newSamples.length; j++)
    {
---
        newSamples[j] = this.samples[j+i];
---
    } // end for
---
    this.samples = newSamples;
---
} // end method

Try and Solve Part 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.

Next Section - Free Response - Number Cube A