-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetDataPoints.java
More file actions
106 lines (97 loc) · 3.55 KB
/
GetDataPoints.java
File metadata and controls
106 lines (97 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.io.*;
/**
* This class can be used to get the data points to plot for an equation. LENGTH is the number of
* points needed, and the equation should be entered below in the respective method. Currently,
* the equation is 200 * Math.sin(i) + 200. Compile and run this file with javac/java, and provide
* a file name if a seperate file is needed to be automatically created.
*
* Example usage: java GetDataPoints euler.txt
* java GetDataPoints
*/
public class GetDataPoints {
private static final int LENGTH = 100;
/**
* Customizable function for this class to generate data points based off of an equation.
*
* @param x The input value for the function
* @return Resulting y value or f(x)
*/
private static double equation(int x) {
return 200 * Math.sin(x) + 200;
}
/**
* Prints all the points to a new or existing output file. Further details are explained
* below.
*
* @param outputFileName File name for output
*/
private static void printPoints(String outputFileName) {
FileWriter fileWriter = null;
// Open output file if output file name isn't null
if (outputFileName != null) {
try {
fileWriter = new FileWriter(new File(outputFileName));
fileWriter.write("x ");
} catch (IOException e) {
System.out.println("IO error while opening file: " + outputFileName);
System.exit(1);
}
} else
System.out.print("x ");
// Prints x values
for (int x = 1; x <= LENGTH; x++) {
if (outputFileName != null) {
try {
fileWriter.write(x + " ");
} catch (IOException e) {
System.out.println("IO error while writing to file: " + outputFileName);
System.exit(1);
}
} else
System.out.print(x + " ");
}
// Prints blank lines
if (outputFileName != null) {
try {
fileWriter.write("\ny ");;
} catch (IOException e) {
System.out.println("IO error while writing to file: " + outputFileName);
System.exit(1);
}
} else {
System.out.println();
System.out.print("\ny ");
}
// Prints y values based on the equation function
for (int x = 1; x <= LENGTH; x++) {
if (outputFileName != null) {
try {
fileWriter.write(equation(x) + " ");
} catch (IOException e) {
System.out.println("IO error while writing to file: " + outputFileName);
System.exit(1);
}
} else
System.out.print(equation(x) + " ");
}
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
System.out.println("Error while closing file: " + outputFileName);
}
}
}
/**
* Main method. Either creates a new file or prints to terminal if no file name is specified.
*
* @param args Optional file name for outputting to a file
*/
public static void main(String[] args) {
if (args.length > 0 && args[0] instanceof String && args[0].endsWith(".txt")) {
printPoints(args[0]);
System.out.println("File created.");
} else
printPoints(null);
}
}