comment my program! + some questions
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.
Code :
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);
}
Code :
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;
}
}
Code :
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.
*/
}
Re: comment my program! + some questions
If you think the blank lines are causing problems then print out all of the values (and the running sum) each time through the inner loop. Make sure the values agree with the numbers in the file and the sum agrees with manual calculations (with a calculator) for each new value.
You only need to check the first two groups.
I gave pseudo-code in your previous thread. When I implemented it (exactly) I got the correct average for each group.
Bottom line:
Set the sum variable equal to zero each time before the inner loop that adds the next 49 values into sum.
Cheers!
Z
Re: comment my program! + some questions
o ok thank, yeah I followed your pseudo code, but I see now. the sum gets stored I was thinking after it finished doing the calculation, it goes back to 0.