2011 Sound

<< 2012 GrayImage | APQuestionsTrailIndex | 2011 AttractiveCritter >>

Question 1 from 2011 asks you to write two methods of the Sound class, limitAmplitude and trimSilence.

Click here to see the questions from 2011

Sound.java

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 >= 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.
       // In fact.. the ones below are written by Fr Chris to make this thing work...
       public Sound(int[] a){
       	    samples=a;
	   }
	   public String toString(){
		   String line1="Index->",line2="Value->";
		   for(int i=0; i<samples.length; i++){
			   line1+=String.format(" %5d ", i);
			   line2+=String.format("[%5d]", samples[i]);
		   }
		   return line1+"\n"+line2;
	   }
}

SoundTester.java


public class SoundTester 
{
	public static void main(String[] args){
		int[] a = {40,2532,17,-2300,-17,-4000,2000,1048,-420,33,15,-32,2030,3223};
		int[] aResult = {40,2000,17,-2000,-17,-2000,2000,1048,-420,33,15,-32,2000,2000};
		int[] b = {0,0,0,0,-14,0,-35,-39,0,-7,16,32,37,29,0,0};
		int[] bResult = {-14,0,-35,-39,0,-7,16,32,37,29,0,0};
		Sound partA = new Sound(a);
		Sound partB = new Sound(b);
		Sound partAResult = new Sound(aResult);
		Sound partBResult = new Sound(bResult);
		System.out.println("Part A Example:\n"+partA);
		System.out.println("\nAfter calling limitAmplitude(2000) you should get:\n"+partAResult);
		int numChanges = partA.limitAmplitude(2000);
		System.out.println("your code produced:\n"+partA);
		System.out.println("It should report making 5 changes, your code reported "+numChanges+" changes");

		System.out.println("\nPart B Example:\n"+partB);
		System.out.println("\nAfter calling trimSilenceFromBeginning() you should get:\n"+partBResult);
		partB.trimSilenceFromBeginning();
		System.out.println("your code produced:\n"+partB);

	}
}

If all is well, the Tester output should look like:


Part A Example:
Index->     0      1      2      3      4      5      6      7      8      9     10     11     12     13 
Value->[   40][ 2532][   17][-2300][  -17][-4000][ 2000][ 1048][ -420][   33][   15][  -32][ 2030][ 3223]

After calling limitAmplitude(2000) you should get:
Index->     0      1      2      3      4      5      6      7      8      9     10     11     12     13 
Value->[   40][ 2000][   17][-2000][  -17][-2000][ 2000][ 1048][ -420][   33][   15][  -32][ 2000][ 2000]
your code produced:
Index->     0      1      2      3      4      5      6      7      8      9     10     11     12     13 
Value->[   40][ 2000][   17][-2000][  -17][-2000][ 2000][ 1048][ -420][   33][   15][  -32][ 2000][ 2000]
It should report making 5 changes, your code reported 5 changes

Part B Example:
Index->     0      1      2      3      4      5      6      7      8      9     10     11     12     13     14     15 
Value->[    0][    0][    0][    0][  -14][    0][  -35][  -39][    0][   -7][   16][   32][   37][   29][    0][    0]

After calling trimSilenceFromBeginning() you should get:
Index->     0      1      2      3      4      5      6      7      8      9     10     11 
Value->[  -14][    0][  -35][  -39][    0][   -7][   16][   32][   37][   29][    0][    0]
your code produced:
Index->     0      1      2      3      4      5      6      7      8      9     10     11 
Value->[  -14][    0][  -35][  -39][    0][   -7][   16][   32][   37][   29][    0][    0]