Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This skeleton code will be utilized to program out the required methods in Eclipse. It comes with a tester class with main in order to

image text in transcribedimage text in transcribedimage text in transcribed

This skeleton code will be utilized to program out the required methods in Eclipse. It comes with a tester class with main in order to test the methods you are required to implement.

import java.util.*;

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) { } public void trimSilenceFromBeginning() { }

}

1. Digital sounds can be represented as an array of integer values. For this question, you will write two unrelated methods of the Sound class A partial declaration of the Sound class is shown below. public class Sound /**the array of values in this sound; guaranteed not to be null */ private int[] samples; /** Changes those values in this sound that have an amplitude greater than limit. * Values greater than limit are changed to limit. * Values less than -limit are changed to -limit. * @param limit the amplitude limit Precondition: limit 2 0 * return the number of values in this sound that this method changed public int limitAmplitude (int limit) (/* to be implemented in part (a) */) *Removes all silence from the beginning of this sound * Silence is represented by a value of (0 Precondition: samples contains at least one nonzero value * Postcondition: the length of samples reflects the removal of starting silence public void trimSilenceFromBeginning () (/* to be implemented in part (b) */ // There may be instance variables, constructors, and methods that are not shown (a) The volume of a sound depends on the amplitude of each value in the sound. The amplitude of a value is its absolute value. For example, the amplitude of -2300 is 2300 and the amplitude of 4000 is 4000. Write the method limitAmplitude that will change any value that has an amplitude greater than the given limit. Values that are greater than limit are replaced with limit, and values that are less than -limit are replaced with -1imit. The method returns the total number of values that were changed in the array. For example, assume that the array samples has been initialized with the following values. 40 2532 17 2300 17-4000 20001048 -420 33 1532 2030 3223 When the statement int numChanges - limitAmplitude (2000) is executed, the value of numchanges will be 5, and the array samples will contain the following values 2000 17 -2000 -17 -2000 2000 1048 -420 33 15 -32 2000 2000 Complete method limitAmplitude below. /* * Changes those values in this sound that have an amplitude greater than 1imit Values greater than limit are changed to limit. Values less than -limit are changed to -limit. *@param limit the amplitude limit Precondition: limit20 @return the number of values in this sound that this method changed * public int limitAmplitude (int limit) (b) Recorded sound often begins with silence. Silence in a sound is represented by a value of 0 Write the method trimSilenceFromBeginning that removes the silence from the beginning of a sound. To remove starting silence, a new array of values is created that contains the same values as the original samples array in the same order but without the leading zeros. The instance variable samples is updated to refer to the new array. For example, suppose the instance variable samples refers to the following array Index 01 2345 6 78910 11 12 13 14 15 Value 0 140 35 -39 -7 16 32 37 290 After trimSilenceFromBeginning has been called, the instance variable samples will refer to the following array. Index0 1 2 3 45 6 78 9 10 11 Value-140-35 -39 0 -7 16 32 37 290 Complete method trimSilenceFromBeginning below /** Removes all silence from the beginning of this sound * Silence is represented by a value of 0 *Precondition: samples contains at least one nonzero value *Postcondition: the length of samples reflects the removal of starting silence public void trimSilenceFromBeginning ()

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

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions