Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: comment my program! + some questions

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default 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.




    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.
         */
     
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default 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

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    leonne (January 17th, 2013)

  4. #3
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default 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.

Similar Threads

  1. questions
    By fahid in forum Java Theory & Questions
    Replies: 1
    Last Post: December 5th, 2012, 04:42 AM
  2. List of my Java3D Questions, and Proguard questions
    By Zachary1234 in forum Java SE APIs
    Replies: 0
    Last Post: November 16th, 2012, 09:40 PM
  3. Few questions
    By CSUTD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2011, 10:23 PM
  4. [SOLVED] Some serious questions,
    By Time in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2010, 03:52 AM
  5. [SOLVED] Please comment
    By tonil in forum Java Theory & Questions
    Replies: 8
    Last Post: August 18th, 2009, 06:51 PM