<< Chapter < Page | Chapter >> Page > |
Listing 3 . The method named run. |
---|
void run(){
//Do not modify the code in this method.double xVal = xMin;
String outString = "";while(xVal<= xMax){
//Construct an output string contain comma-separated values of y as a// function of x.
outString += getYval(xVal) + ",";//Increment x
xVal += xInc;}//end while loopwriteOutputFile(fileName,outString);}//end run method |
Listing 4 shows the beginning of the method named getYval .
This is the method that evaluates the function. This is where you will make the most important changes to the code if you use this program as a template forother functions. You will need to modify the code in Listing 4 to cause it to properly evaluate your new function.
As I mentioned earlier, the function evaluated in this program is the classic sin(x)/x function, otherwise known a the sinc function. I chose it for this explanation because it is the most complicated of the functions illustrated by the audio files in the above list .
To understand the code in Listing 4 , you need to know how to use the sin method of the Math class.
The most complicated thing about this code is illustrated by the code in the if-else structure. As you can see, the code in the if clause divides something by xVal , which is the incoming value of x. This value can be zero, which will result in a "divide by zero" error. To eliminate that possibility, the else clause substitutes a very small but non-zero value for xVal when xVal is actually zero. This doesn't change the audio output, but it does preventthe division by zero problem.
Listing 4 . Beginning of the method named getYval. |
---|
double getYval(double xVal){
//Evaluate the function heredouble f = 0.25;
double result = 0;if(xVal != 0.0){
result = Math.sin(2*Math.PI*f*xVal)/xVal;}else{
//Don't divide by zeroresult = Math.sin(2*Math.PI*f*0.00001)/0.00001;
}//end else |
To reiterate, you will modify the code in that portion of the getYval method shown in Listing 4 if you use this program as a template for a new program for a different function.
The remainder of the getYval method is shown in Listing 5 . You should notmodify this code.
All of the computations are being performed as type double . A double value can produce many more digits to the right of the decimal point than are useful when converting the output values to audio. The code in Listing 5 limits the number of digits to the right of the decimal point to no more than three digits. I will leave it as an exercise for thestudent to look up the behavior of the rint method of the Math class and ponder on how the code in Listing 5 limits the number of decimal digits in the return value to no more than three digits.
Listing 5 . The end of the getYval method. |
---|
//Do not modify this code.
//Limit the return value to three decimal digitsreturn (Math.rint(1000.0*result))/1000.0;
}//end getY |
Although it is not possible to make quantitative measurements of the sinc function by listening to Sinc01 , it is possible to deduce important facts about the function, even at a high output pulse rate. (If you need to make quantitative measurements, the data is available for that purpose in the output text file.) You can reach the following conclusions by listening to Sinc01 (or a slowed-down version) .
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?