<< Chapter < Page | Chapter >> Page > |
Listing 7 . The abstract method named getMelody. |
---|
abstract byte[] getMelody();}//end AudioSignalGenerator02 |
The sound that you heard when you listened to the audio file named WhiteNoise was actually produced by the getMelody method of the WhiteNoise class. That class begins in Listing 8 and the getMelody method begins in Listing 9 .
The first thing to notice about Listing 8 is that the WhiteNoise class extends (is a subclass of) the abstract AudioSignalGenerator02 class.
The constructor for the class named WhiteNoise , which is shown in Listing 8 , receives three incoming parameters and passes them up to the superclass named AudioSignalGenerator02 through the use of the super keyword where they are saved (see Listing 6 ) . In case you don't remember the super keyword, see Java1628 : The this and super Keywords. Also see Ap0090 : Self-assessment, the super keyword, final keyword, and static methods .
Listing 8 . Beginning of the class named WhiteNoise. |
---|
import java.io.*;
import java.nio.*;import java.util.*;
public class WhiteNoise extends AudioSignalGenerator02{public WhiteNoise(AudioFormatParameters01 audioParams,String[] args,byte[] melody){super(audioParams,args,melody);
}//end constructor |
Listing 9 shows the beginning of the overridden getMelody method. (Recall that an abstract version of this method is inherited from the class named AudioSignalGenerator02 , see Listing 7 )
Listing 9 . Beginning of the getMelody method. |
---|
byte[] getMelody(){//Set the audio parameters to mono
audioParams.channels = 1;//superfluous -- default valueSystem.out.println("audioParams.channels = " + audioParams.channels); |
Note that the getMelody method returns a reference to an array object containing elements of type Byte .
The code in Listing 1 declares an instance variable named melody as a reference to an array object containing elements of type Byte . The code in Listing 4 calls the getMelody method on a reference to an object of the WhiteNoise class and assigns the returned reference to the instance variable named melody that is declared in Listing 1 .
Listing 4 passes that reference to the playOrFileData method to cause the contents of the array to be played through the computerspeakers or saved in an audio file.
Now that you understand the overall structure, we need to examine the details of the code to determine how the method named getMelody actually returns a reference toan array object containing data that represents three seconds of monaural white noise.
Because the class named WhiteNoise extends the class named AudioSignalGenerator02 , it inherits a reference to an object of the class AudioFormatParameters01 . The name of the inherited reference is audioParams . As you learned earlier , this object contains several predefined audio parameters. As you can see in Listing 13 , the predefined values are all stored in public instance variables. Therefore, codein methods of the WhiteNoise class can modify the values stored in that object by joining the object'sreference to the name of the variable as shown in Listing 9 ( audioParams.channels for example).
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?