Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Arraylists and Frequency Histograms.

807607Nov 15 2006 — edited Nov 16 2006
I'm about to snap. I seriously am. I don't know what's wrong with this program:

import java.util.ArrayList;
public class StatsData {
    
    /** Creates a new instance of StatsData */
    public StatsData() {
        data = new ArrayList<Double>();
        
    }
    
    public int count(){
   int count = 0;
   for (int i = 1; i < data.size(); i++) 
       {
         if (count < data.size()) 
             count++;
       }
 return count;
    }
    
    public double max()
{

    double max = 0;
     for (int i = 0; i < data.size(); i++) 
      {
         double a = data.get(i);
         if ( a > max)
          max = a;
      }       
    return max;
   }
     public double min()
{

    double min = 99999;
     for (int i = 0; i < data.size(); i++) 
      {
         double a = data.get(i);
         if (min > a)
          min = a;
      }       
    return min;
   }

    public double mean(){
        double mean1 = 0.0;
        double mean2;
        for (int i = 0; i < data.size(); i++) 
       {
        mean1 += data.get(i) + mean1;
        }
        mean2 = mean1 / this.count();
        return mean2;
    }
    public double variance(){
        double deviation = 0.0;
        double devsum = 0.0;
        for (int i = 0; i < data.size(); i++) {
        deviation = (double)data.get(i) - this.mean();
        devsum += Math.pow(deviation, 2);
        }
        return (devsum / (data.size() - 1));
    }
    
    public void histogram(int numClasses){
       
        int frequency[];
        int k = 0;
        double midpoint = 0;
        frequency = new int[numClasses];
        
        int classWidth = 0;
        classWidth = (((int)this.max() - (int)this.min()) + 1) / numClasses; 
      
        for (int i = 0; i < data.size() - 1; i++){
        if ((0 <= i) && (i < numClasses)){
            if ((((this.min() - 0.5) + (data.get(i)*classWidth)) <= data.get(i)) && ( data.get(i) < ((this.min() - 0.5) + ((data.get(i)+1)*classWidth))))
            k = (int)((data.get(i) - (this.min() - 0.5) / classWidth));
            frequency[k]++;}}
         
           
        for (int i = 0; i < numClasses; i++){
            midpoint = (int)this.min() - 0.5 + (k + 0.5) * classWidth;
               System.out.println(""+midpoint+" |");
           for (int j = 1; j < frequency; j++){
System.out.println("*");
}
System.out.println();
}
}
public ArrayList<Double> data;

}


I input an ArrayList full of numbers in another class and it's supposed to print out a Frequency Histogram. Something like this:

Assuming numClasses = 10:

Frequency Histogram
23.9 | *
26.8 | *
29.7 | **
32.7 | *
35.6 | **
38.5 | ******
41.4 | *****
44.4 | ***
47.3 | **
50.2 | **

But the way I have it set up, it's printing it out like this.

94999.0 |
94999.0 |
94999.0 |
94999.0 |
94999.0 |
94999.0 |

And I know that means I messed up the min() method abd I know that's not the only thing I messed up, but I don't know how to fix it and I have so much other homework to do. I really need help. Please help, someone.

More details on the Histogram Method:

The approach we will take to generate the histogram is to first create an
integer array containing the frequencies of data per class. When there are
numClasses number of elements in the frequency array, the classWidth is
given by the formula classWidth =
max&#8722;min +1 * numClasses . An element x belongs to
class i if (min &#8722; 0 . 5)+ i � classWidth x < (min &#8722; 0 . 5)+( i +1)�classWidth ,
where 0 i < numClasses . So we determine the class to which an element
belongs by the formula i =
x i &#8722;min&#8722;0 . 5 * classWidth , ignoring the fractional part of the
number. So if i = 0 . 7, the the number belongs to class 0. The number of
asterisks on the a bar of the histogram corresponds to the frequency of a
class; that is, the tally of data elements that fall in the class interval given
above. Additionally, we will print the midpoint of each class. The midpoint
of class
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 14 2006
Added on Nov 15 2006
45 comments
1,577 views