<< Chapter < Page Chapter >> Page >

To use this program as a template and modify it to handle other functions, you only need to modify the instance variables in the class named Runner and modify the code in the method named getYval . You should not modify any of the other code.

Beginning of the program named Sinc01

The beginning of the program named Sinc01 is shown in Listing 1 . This is the driver class containing the main method. There is nothing new or unusual about this code. If you use thisprogram as a template for a new program to evaluate a different function, you should not modify the code in Listing 1 .

Listing 1 . Beginning of the program named Sinc01.
import java.io.BufferedWriter; import java.io.File;import java.io.FileWriter; import java.io.IOException;public class Sinc01{//Driver class public static void main(String[]args){ //Do not modify the code in this method.Runner obj = new Runner(); obj.run();}//end main }//end class Sinc01

Beginning of the class named Runner

The code in Listing 1 instantiates a new object of the class named Runner . The class named Runner begins in Listing 2 .

The first three instance variables in Listing 2 declare and initialize three variables that control the range of x for which the function will be evaluated,and the incremental steps in x for which the function will be evaluated. You may or may not want to change these values when writing a program to evaluate adifferent function. For example you might want to evaluate the function only for positive values of x, in which case you would probably set the value for xMin to 0.

The last statement in Listing 2 establishes the name of the text file that will be written into the subfolder named Data to contain the results of the evaluation. You probably will want to modify this to cause the name of theoutput file to be descriptive of the function being evaluated.

Listing 2 . Beginning of the class named Runner.
class Runner{ //Modify the following instance variables as needed.double xMin = -20;//Minimum value for x double xMax = 20;//Maximum value for xdouble xInc = 0.25;//Used to determine x-values for evaluation of y-value String fileName = "Data/Sinc01.txt";//Output file name in Data folder

The method named run

After instantiating a new object of the Runner class, the code in Listing 1 calls the run method on that new object. The run method is shown in its entirety in Listing 3 . You should not modify the code in the run method

The code in Listing 3 is straightforward. It repeatedly calls a method named getYval , once for each incremental value of x between the limits specified in Listing 2 . It creates an output string by concatenating the values returned by getYval , separating those values by commas with no spaces. When getYval has been called once for each incremental value of x within the specified limits, the method named writeOutputFile is called to write the string into a text file with the name specified in Listing 2 . You can view the method named writeOutputFile in Listing 16 . You should not modify the method named writeOutputFile .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Accessible objected-oriented programming concepts for blind students using java. OpenStax CNX. Sep 01, 2014 Download for free at https://legacy.cnx.org/content/col11349/1.17
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?

Ask