Question: 8.22 LAB^: Sound (arrays) JAVA Sound can be represented as an array of integer values. Recorded sound often begins and ends with silence, represented by

8.22 LAB^: Sound (arrays) JAVA

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 { private int[] samples; public Sound (int[] samples) { // Instantiate instance variable with same length as parameter this.samples = new int[samples.length]; // Copy parameter array to instance variable for (int i = 0; i < samples.length; i ++) { this.samples[i] = samples[i]; } } /** * toString method returns a String with samples separated by a comma - ends with a comma */ public String toString() { String s = ""; for (int sample : samples) { s += sample + ", "; } return s; }

/** * trimSilence removes soundlevels of 0 from the beginning and end of the samples instance variable */ public void trimSilence() { // TODO: Complete this method } }

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!