-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay18.java
More file actions
311 lines (239 loc) · 9.15 KB
/
Day18.java
File metadata and controls
311 lines (239 loc) · 9.15 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// ---------------------------------------------------
// Author : Benjamin Kataliko Viranga
// Community : Stunt Business
// Community website : www.stuntbusiness.com
//
// 30 Days - Q&A Java basic
// Day 18 : Challenge X - Data processing - Read and Plot
// Day 18 | IG : https://www.instagram.com/benjivrik/
// ----------------------------------------------------
// what would be the output of this program ?
// Open a text file and write inside the following content:
// Angle Cosine Sine
// 0
// 15
// 30
// 45
// 60
// 75
// ...
// Generate your angle from 0 to 360.
// And add a 15 degrees span between each angle value.
// Parse the File you have just created and look for Angles, Cosine and Sine values
// use the library Jfreechart used in Day13 for your plot
// Plot the values with the Angles on the X-Axis and Cosine/Sine on the Y axis
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.Math;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
class Graph extends ApplicationFrame {
/**
*
*/
private static final long serialVersionUID = 8636866202142037430L;
private String chartTitle;
private HashMap<String,double[]> data;
public Graph( String applicationTitle , String chartTitle ) {
// initialize call the parent constructor ApplicationFrame
super(applicationTitle);
this.chartTitle = chartTitle;
// initialize your hasmap
this.data = new HashMap<String,double[]>();
}
/**
* Add data for the graph, data stored in the Hashmap<>
* @param rowKey
* @param dataArray
*/
public void addData(String rowKey, double[] dataArray)
{
this.data.put(rowKey, dataArray);
}
/**
*
* @return
*/
private DefaultCategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
// get the angles values from the dictionary
double[] angles = this.data.get("Angles");
//remove the angles so that you are only left with the sine and cosine values
this.data.remove("Angles");
// add data in the dictionary
for(String rowKey: this.data.keySet())
{
int index = 0; // index for positioning the number
// add the integers in your dataset
// each array have the same size
for(double value : this.data.get(rowKey))
{
dataset.addValue(value, rowKey,Double.toString(angles[index++]));
}
}
// add the angles array back into the dictionary
this.data.put("Angles", angles);
return dataset;
}
/**
*
*/
public void plot()
{
JFreeChart lineChart = ChartFactory.createLineChart(
this.chartTitle,
"Angles",
"Cosine / Sine Values",
createDataset(),
PlotOrientation.VERTICAL,
true,false,false);
ChartPanel chartPanel = new ChartPanel( lineChart );
chartPanel.setPreferredSize( new java.awt.Dimension( 680 , 480 ) );
setContentPane( chartPanel );
}
}
public class Day18
{
private static final String PATH = "text_data/";
/**
* @return an return an array of integers from the start to the end with span
* between those numbers
*/
private static int[] generateAngles(int start, int end, int span)
{
List<Integer> data = new ArrayList<Integer>();
// generate the appropriate angle vakues
for(int i = start; i <= end; i+=span)
{
data.add(i);
}
// from your list 'data' return an array of integers (int[])
// https://www.geeksforgeeks.org/stream-in-java/
// https://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java
return data.stream().mapToInt(i -> i).toArray();
}
public static void main(String[] args)
{
File directory = new File(PATH);
// create directory if it does not exist
if( !(directory.exists()))
{
directory.mkdir();
}
File file = new File(PATH + "Day18_data.txt");
// creating the file if it does not exist
if(!file.exists())
{
try
{
if(file.createNewFile())
{
System.out.println("\nFile successfully created : "+file.getName());
}
}catch(IOException e)
{
e.printStackTrace();
}
}
else // the file does not exist
{
System.out.println(
String.format("\nFile %s already exists.", file.getName())
);
}
// writing and reading processes
try
{
// get the values for your array
int[] angleValues = generateAngles(0, 360, 15);
//System.out.println(Arrays.toString(angleValues));
// Write inside the file
FileWriter writer = new FileWriter(file.getAbsolutePath());
System.out.println("\n----------- WRITING ------------\n");
writer.write("Angle\t\tCosine\t\tSine\n");
for( int i : angleValues)
{
double cosine = Math.cos( Math.toRadians(i));
double sine = Math.sin( Math.toRadians(i));
writer.write(
String.format("%d\t\t%f\t\t%f\n", i,cosine,sine )
);
}
writer.close();
Scanner sc = new Scanner(file);
System.out.println("\n----------- READING ------------\n");
// read the content of the file
// Info : check if a data exists in your array
// https://stackoverflow.com/questions/1128723/how-do-i-determine-whether-an-array-contains-a-particular-value-in-java
List<Double> cosine = new ArrayList<Double>();
List<Double> sine = new ArrayList<Double>();
List<Double> angles = new ArrayList<Double>();
while (sc.hasNextLine())
{
String[] line = sc.nextLine().split("\t");
// check if the line contains "Angle"
// meaning that is the first line
if(Arrays.stream(line).anyMatch(x -> x.equals("Angle")))
{
// skipe first line with (the titles (Angle, Cosine and Sine))
continue;
}
else
{
// remove the empty element in the array
line = Arrays.stream(line).filter(x -> !StringUtils.isBlank(x)).toArray(String[]::new);
/**
*
* index 0 : angle
* index 1 : cosine
* index 2 : sine
*
*/
// add values in the respective list
angles.add(Double.parseDouble(line[0]));
cosine.add(Double.parseDouble(line[1]));
sine.add(Double.parseDouble(line[2]));
System.out.println(Arrays.toString(line));
}
}
sc.close();
// change your list to array double
double[] anglesData = angles.stream().mapToDouble(i->i).toArray();
double[] cosineData = cosine.stream().mapToDouble(i->i).toArray();
double[] sineData = sine.stream().mapToDouble(i->i).toArray();
// Using the Chart Class from Day14.java
// Make your files are in the same package
// Package : https://www.w3schools.com/java/java_packages.asp
Graph sine_cosine = new Graph("Data processing", "Sine and Cosine");
// add data in the chart using the function addData()
sine_cosine.addData("Angles",anglesData);
sine_cosine.addData("Cosine",cosineData);
sine_cosine.addData("Sine",sineData);
// plot
sine_cosine.plot();
sine_cosine.pack();
RefineryUtilities.centerFrameOnScreen( sine_cosine ); // center your frame in your screen
sine_cosine.setVisible( true );
}
catch(IOException e)
{
e.printStackTrace();
}
// You will see dots on the X axis. (for displaying the angles)
// I did not fix it for this code :)
// https://stackoverflow.com/questions/45327114/jfreechart-displaying-three-dots-in-place-of-the-values-on-the-x-axis
System.out.println("End of program.");
}
}