Other 2011 FR Questions FR other years Be Prepared Home
A-1
Part (a)
  public int limitAmplitude(int limit)
  {  
    int count = 0;

    for (int i = 0; i < samples.length; i++)
    {
      if (samples[i] > limit)
      {
        samples[i] = limit;
        count++;
      }
      else if (samples[i] < -limit)
      {
        samples[i] = -limit;
        count++;
      }
    }
    return count;
  }

Part (b)
  public void trimSilenceFromBeginning()
  {
    int n = 0;
    while (samples[n] == 0)
      n++;

    int[] newSamples = new int[samples.length - n];

    for (int i = 0; i < newSamples.length; i++) 1
      newSamples[i] = samples[i+n];

    samples = newSamples;
Notes:
  1. The array copying part of the code can be implemented in many different ways.  For example:
        ...
        for (int i = 0; i < newSamples.length; i++)
        {
          newSamples[i] = samples[n];
          n++;
        }
        samples = newSamples;
    
    or:
        ...
        for (int i = 0; i < newSamples.length; i++, n++)
          newSamples[i] = samples[n];
        samples = newSamples;
    
    or simply:
        ...
        samples = Arrays.copyOfRange(samples, n, samples.length);
    
    (Arrays.copyOfRange is not in the AP subset, but the question doesn't say it can't be used, so this solution should receive full credit.)

Other 2011 FR Questions | Back to Contents

Copyright © 2011 by Skylight Publishing
support@skylit.com