Question
LAB^: Sound (arrays) Sound can be represented as an array of integer values. Recorded sound often begins and ends with silence, represented by a value
LAB^: Sound (arrays)
Sound can be represented as an array of integer values.
Recorded sound often begins and ends with silence, represented by a value of 0.
The Sound class has been started for you. Write the method trimSilence(). This method takes no parameters and returns void. It should update the samples array by removing silence from the beginning and end.
The SoundDriver class has been provided to test your method.
Hint: Create private methods which return the number of leading (or trailing) zeros. Then call those methods to determine how big to make the new array.
SOUND JAVA:
public class Sound {
public static void main(String[] args) { int[] samples1 = new int[]{40, 2532, 17, -2300, -17, -4000, 2000, 1048, -420, 33, 15, -32, 2030, 3223}; Sound s1 = new Sound(samples1); System.out.println("The original array is " + Arrays.toString(s1.getSamples() ) ); System.out.println( s1.limitAmplitude(2000) + " sounds had to limit amplitude" ); System.out.println( "Updated array is " + Arrays.toString( s1.getSamples() ) ); System.out.println(""); int[] samples2 = new int[]{0, 0, 0 , 0 -14, 0, -35, -39, 0, -7, 16, 32, 37, 29, 0, 0}; Sound s2 = new Sound(samples2); System.out.println("Original Array: " + Arrays.toString( s2.getSamples() ) ); s2.trimSilenceFromBeginning(); System.out.println("Updated Array: " + Arrays.toString( s2.getSamples() ) );
}
private int[] samples; public int[] getSamples() { return samples; } public Sound(int[] samples) { this.samples = samples; }
public int limitAmplitude(int limit) { int cnt=0; for(int i=0;i
}
SOUND DRIVER.JAVA:
public class SoundDriver { public static void main(String[] args) { Sound sound1 = new Sound (new int[]{-16, 0, -25, -20, 0, 30, 42, 89, 73, 75}); Sound sound2 = new Sound (new int[]{0, 0, 0, -16, 0, -25, -20, 0, 30, 42, 89, 73, 75, 0, 0}); sound1.trimSilence(); sound2.trimSilence(); System.out.println("Sound 1 before trim: " + sound1); System.out.println("Sound 1 after trim: " + sound1); System.out.println("Sound 2 before trim: " + sound2); System.out.println("Sound 2 after trim: " + sound2);
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started