Unit 4A - Free Response - Sound A
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period1 exercise3 graded tests
Files
Unit 4A - Free Response - Sound A
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 a. The volume of a sound depends on the amplitude of each value in the sound. The amplitude of a value is its absolute value. For example, the amplitude of -2300 is 2300 and the amplitude of 4000 is 4000.
Write the method limitAmplitude that will change any value that has an amplitude greater than the given limit. Values that are greater than limit are replaced with limit, and values that are less than -limit are replaced with –limit. The method returns the total number of values that were changed in the array. For example, assume that the array samples has been initialized with the following values.

When the statement
int numChanges = limitAmplitude(2000);
is executed, the value of numChanges will be 5, and the array samples will contain the following values.

How to Solve This
We will have to loop through each value in the array and compare the value to the limit. We will need to keep track of the number of values changed.
If the current value is greater than the limit, it should be reset to the limit and the count of the values changed should be incremented.
If the current value is less than the negative of the limit, then it should be reset to the negative of the limit and the count of values should be incremented.
We will have to return the count of values changed.
Check your understanding: Which loop would be best for this problem? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/soundA.html#fr_sounda_1
Check your understanding: Which is the correct code for changing the current value to the negative of the limit? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/soundA.html#fr_sounda_2
Mixed Up Code
Check your understanding: The method
limitAmplitudebelow contains the correct code for a solution to this problem, but the code blocks are mixed up. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/soundA.html#SoundA
Try and Solve Part A
Exercise 1
FRQ Sound A: Write the method limitAmplitude that will change any value that has an amplitude greater than the given limit. Values that are greater than limit are replaced with limit, and values that are less than -limit are replaced with –limit. The method returns the total number of values that were changed in the array. The main method has code to test your solution.
Open the
01-FRQSoundAfolder and editSound.java. Press Run to try it; Run Tests checks it (3 tests).
For teachers
Unit 4A - Free Response - Sound A
Students read CSAwesome2's “Unit 4A - Free Response - Sound A” in CS Room and work through its 1 Java coding exercise.
- Time: 1 class period
- Tests: 3 in 1 graded exercise, set up for you when you import the lesson
Goals
- Practice for Unit 4: Data Collections.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page, with its mixed-up code and multiple-choice questions, is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/soundA.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. Questions that need CS Awesome's interactive widgets link back to it. - Students do exercise 01 in its folder, pressing Run to try their code.
- 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
| Folder | File | Grading | Tests |
|---|---|---|---|
01-FRQSoundA | Sound.java | Autograded | 3 |
Teachers can download a reference solution after signing in.