Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Oracle Solaris 11 System Administration

Authors: Bill Calkins

1st Edition

0133007170, 9780133007176

More Books

Students also viewed these Databases questions

Question

Find y'. y= |x + X (x) (x) X 1 02x+ 2x 1 O 2x + 1/3 Ex 2x +

Answered: 1 week ago