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
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