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= limit) { samples[i] = limit; cnt++; } //assign negative limit value to temp variable and check negative limit value int temp= -limit; if(samples[i] <= temp ) { samples[i] = temp; cnt++; } } //return count of total exceed value of limit return cnt; } public void trimSilenceFromBeginning() { //cnt is use to count the starting zeros of array 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!