Prelab 9: Exploring Sound

1. Review background reading

2. Review loops and sound

Below is a method from the lecture notes, increaseVolume, that will be added to the Sound class.

1 public void increaseVolume() { 2 SoundSample[] samples = this.getSamples(); // get array 3 int index = 0; // starting index 4 SoundSample sample = null; // current sample obj 5 int value = 0; // value at sample // loop through SoundSample objects 6 while (index < samples.length) { 7 sample = samples[index]; // get current obj 8 value = sample.getValue(); // get the value 9 sample.setValue(value * 2); // set the value 10 index = index + 1; // increment index } } Answer the following questions
  1. What is samples? For a particular Sound, what does samples.length represent?
    
    
    
    
  2. What do the lines 7-9 do for a particular sample?

    
    
    
    
  3. Now consider another version of increaseVolume that uses a for-each loop. 1 public void increaseVolume() { 2 SoundSample[] samples = this.getSamples(); // get array 3 for (SoundSample sample : samples) { 4 sample.setValue(sample.getValue() * 2); } } While the code is shorter, it performs the same function.
    1. How many times will line 4 be executed?
      
      
      
      
      
    2. Why is the variable value
  4. Consider the changeVolume method in Sound.java. We would like the method to change the volume (amplitude) of the sound by multiplying the current values of the samples in the sound by a given factor. How can you modify the code of increaseVolume to work for an arbitrary factor?
    
    
    
    
    
    
    
    
    
    

Jeffrey R.N. Forbes
Last modified: Sat Mar 24 23:55:06 EDT 2007