Unit 4A - Free Response - Sound B

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period1 exercise2 graded tests

Files

README.mdDownload

Unit 4A - Free Response - Sound B

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

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 Past AP CSA Exams.

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.

Sound Table3

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

Sound Table4

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?

Check your understanding: Which loop would be best for this problem? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/soundB.html#fr_soundb_1

Check your understanding: 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? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/soundB.html#fr_soundb_2

Mixed Up Code

Check your understanding: The method trimSilenceFromBeginning below contains correct code for one solution to this problem, but it is mixed up. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/soundB.html#SoundB

Try and Solve Part B

Exercise 1

FRQ Sound B: Finish writing 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.

Open the 01-FRQSoundB folder and edit Sound.java. Press Run to try it; Run Tests checks it (2 tests).

For teachers

Unit 4A - Free Response - Sound B

Students read CSAwesome2's “Unit 4A - Free Response - Sound B” in CS Room and work through its 1 Java coding exercise.

  • Time: 1 class period
  • Tests: 2 in 1 graded exercise, set up for you when you import the lesson

Goals

  • Practice for Unit 4: Data Collections.

Before class

In class

  1. Students read README.md. Questions that need CS Awesome's interactive widgets link back to it.
  2. Students do exercise 01 in its folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-FRQSoundBSound.javaAutograded2

Teachers can download a reference solution after signing in.