Hey, Just finished my program that takes data from a radio telescope and graphs it.
I got some questions about my data class. For some reason it wont calculate the correct average, its very close but its not correct. I am wounding why this is? I was thinking maybe it has something to do with my data text file. Every 10 lines there is a blank line with no data, would this be causing the problem? I made a smaller data file with one line and the average was correct. I also checked the output to show me what numbers it was using in the calculation and they seemed correct yet wrong avg.
for the graph class, I originally was thinking of using Enumeration elements for the x y data in the graph, but for some reason I would get a compile error, so I ditched that idea, which is why there some useless code here.
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; import org.jfree.ui.RefineryUtilities; public class astro { public static void main(String[] args) { data d = new data(0); d.open(); d.read(); d.wright(); final graph demo = new graph("Sun Data"); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); }
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Hashtable; import java.util.Scanner; public class data { static Scanner x; double[] data = new double[50]; double sum; public static Hashtable h = new Hashtable(); String file = "Data.txt"; int key = 1; double xaxis; double yaxis; //setter public data(double yyaxis){ this.yaxis=yyaxis; } //opens the data file public void open(){ try{ x = new Scanner(new File("C:/Users/chris/Desktop/data.txt")); } catch(Exception e){ System.out.print("no file"); } } public void read() { //reads the data in the text file and sets it to an array. while(x.hasNextDouble()){ for(int i=1; i<=49; i++){ if(x.hasNextDouble() == false){ System.out.println("error in data"); System.exit(0); } data[i] = x.nextDouble(); // Calculates the average for every 49 set of data. //Stores that average to a hashtable if(i == 49){ for (int ii = 1; ii<=49; ii++){ sum = data[ii] + sum; } sum = sum / 49; data av = new data(sum); h.put(key, av.getHash()); key++; } } } //test to see the data its using to calculate System.out.printf("the average is " + sum + "size "+ h.size() + " ave " + h.get(995) + " " + h.get(996)+ " data used for average: " ); } // wrights the data to a external text file public void wright(){ try { PrintWriter outputStream = new PrintWriter(file); for(int i=1; i<=49; i++){ System.out.print(" "+data[i] + " "); outputStream.print (" " + data[i] + " "); } outputStream.println(""); outputStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } } //getter for hashtable public double getHash(){ return this.yaxis; } }
import javax.swing.JFrame; import java.awt.Color; import java.util.Enumeration; import java.util.Hashtable; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; //import org.jfree.ui.Spacer; /** * A simple demonstration application showing how to create a line chart using data from an * {@link XYDataset}. * */ public class graph extends ApplicationFrame { double Xaxis; double Yaxis; int key=1; //Hashtable q = new Hashtable(); /** * Creates a new demo. * * @param title the frame title. */ public graph(final String title) { super(title); final XYDataset dataset = createDataset(); final JFreeChart chart = createChart(dataset); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); setContentPane(chartPanel); } // public void Hashs(){ // Enumeration elements = data.h.elements(); // while(elements.hasMoreElements()) { // data e = (data) elements.nextElement(); // q.put(key, data.h.get(key)); // key++; // } // } /** * Creates a sample dataset. * * @return a sample dataset. */ private XYDataset createDataset() { // final XYSeries series2 = new XYSeries("sun"); // Enumeration elements = q.elements(); // while(elements.hasMoreElements()) { // data e = (data) elements.nextElement(); for (int i = 1; i<= data.h.size(); i++){ series2.add(i,(Double) data.h.get(i) ); // key++; } final XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(series2); return dataset; } /** * Creates a chart. * * @param dataset the data for the chart. * * @return a chart. */ private JFreeChart createChart(final XYDataset dataset) { // create the chart... final JFreeChart chart = ChartFactory.createXYLineChart( "Sun Data", // chart title "X", // x axis label "Y", // y axis label dataset, // data PlotOrientation.VERTICAL, true, // include legend true, // tooltips false // urls ); // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... chart.setBackgroundPaint(Color.white); // final StandardLegend legend = (StandardLegend) chart.getLegend(); // legend.setDisplaySeriesShapes(true); // get a reference to the plot for further customisation... final XYPlot plot = chart.getXYPlot(); plot.setBackgroundPaint(Color.lightGray); // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); renderer.setSeriesLinesVisible(0, false); renderer.setSeriesShapesVisible(1, false); plot.setRenderer(renderer); // change the auto tick unit selection to integer units only... final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // OPTIONAL CUSTOMISATION COMPLETED. return chart; } /** * Starting point for the demonstration application. * * @param args ignored. */ }