/**
 *         A example program to print the x,y coordinate
 *         pairs of a cosine to a specified output file with
 *         input from the Display class.
 */

import uk.ac.ed.ph.sciprog.*;
import java.io.*;                        // Java io package

public class CosPrinter {

    public static void main(String args[]) throws IOException {

	//         Set up basic display to ask for range, number of
	//         points and output filename

	Display panel = new Display("Cos printer Demo");
	Input rangeInput = new Input("Range of plot in units of PI",2.0);
	Input pointsInput = new Input("Number of points",100);
	Input fileInput = new Input("Output filename", "cosdata.data");

	panel.addInput(rangeInput);
	panel.addInput(pointsInput);
	panel.addInput(fileInput);

	//             Loop to keep asking questions

	while (true) {

	    panel.waitForButtonPress();
	    
	    //         get the information

	    double range = Math.PI*rangeInput.getDouble();
	    int points = pointsInput.getInt();
	    String fileName = fileInput.getString();
	    
	    //        Open a PrintWriter


	    PrintWriter out = new PrintWriter(new FileWriter(fileName));
	    
	    //         Go round the loop writing to the file
	    //         with 4 sig figs

	    for(int i = 0; i < points; i++) {
		double theta = range*(double)i/(double)points;
		out.printf("%.4g  %.4g\n" , theta , Math.cos(theta));
	    }

	    out.close();              // Close the output

	    panel.println("Change inputs and filename for other plot");
	}
    }
}
