0

I am trying to use java to pass an array to get the mean, median,mode , max an min in java. I am currently having an issue passing the array to a function and return its value so i can output the results. I believe i have the loops correct to solve the mean median and mode but i cannot get them to send and receive as wanted. How can I pass the array and send back the values needed?

UPDATE: i have updated the code it will compile and i can input the number of years but i get several errors following after that. it is also not printing the outputs

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'i' at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2646) at java.util.Formatter$FormatSpecifier.(Formatter.java:2675) at java.util.Formatter.parse(Formatter.java:2528) at java.util.Formatter.format(Formatter.java:2469) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream.java:871) at la5cs1110_woodspl_03.pkg17.pkg2016.La5cs1110_WoodsPl_03172016.main(La5cs1110_WoodsPl_03172016.java:56) Java Result: 1

    public static void main(String[] args) {
        int i;
        List<Double> hArray = new ArrayList<>();
        int nYears = 0, y = 0;
        double rMax = 0.00,rMin = 100.00;


        //get input check if between 1-80
        while(y == 0){
        String userData = JOptionPane.showInputDialog
                                   ("Enter number of years");
        nYears = Integer.parseInt(userData);

        if (nYears > 1 && nYears <= 80 )
           y = 1;

        }


        y = 0;
        while(y <= nYears){ 
            for(i = 0; i < 12; i++){
                Random rand = new Random();
                double rNum = rand.nextFloat() * (rMax - rMin) + rMin;
                 hArray.add(rNum);
            }
                double mean = getMean (hArray);
                double median = getMedian (hArray);
                double mode = getMode (hArray);
                double max = getMaxValue(hArray);
                double min = getMinValue (hArray);
           System.out.printf("In year %i the Mean = %d , mode = %d, median = %d," +
                  " max = %d, min = %d", y , mean, median, mode, max, min);
            y++;
        }

    }
    private static double getMean(List<Double> hArray) {
    double sum = 0;
    for (int i = 0; i < hArray.size(); i++) {
        sum += hArray.get(i);
    }
    return sum / hArray.size();
}
    //Median
    private static double getMedian(List<Double> hArray) {
    int middle = hArray.size()/2;
    if (hArray.size() %2 == 1) {
        return hArray.get(middle);
    } else {
        return (hArray.get(middle-1) + hArray.get(middle)) / 2.0;
    }
}
    //Mode
    public static double getMode(List<Double> hArray) {
    double maxValue = 0, maxCount = 0;

    for (int i = 0; i < hArray.size(); ++i) {
        int count = 0;
        for (int j = 0; j < hArray.size(); ++j) {
            if (hArray.get(j) == hArray.get(i)) ++count;
        }
        if (count > maxCount) {
            maxCount = count;
            maxValue = hArray.get(i);
        }
    }

    return maxValue;
}
    public static double getMaxValue(List<Double> hArray){  
      double maxValue = hArray.get(0);  
      for(int i=1;i < hArray.size();i++){  
      if(hArray.get(i) > maxValue){  
      maxValue = hArray.get(i);  

         }  

     }  
         return maxValue; 
    }
    public static double getMinValue(List<Double> hArray){  
     double minValue = hArray.get(0);  
     for(int i=1;i<hArray.size();i++){  
     if(hArray.get(i) < minValue){  
     minValue = hArray.get(i);  
        }  
     }  
    return minValue;  
 }  


}
Pwoods
  • 105
  • 2
  • 4
  • 15
  • I had asked earlier on here how to get an array to pass random floating point random numbers passed to. Which is how i have the array set up now. There can be a a very large or small array so i need i can not set the size – Pwoods Mar 24 '16 at 02:23
  • hArray is a list of Double, not a list of Double[]. I'm surprised this compiles – Tibrogargan Mar 24 '16 at 02:28
  • There's a big difference between an array and ArrayList. One of the differences is that when you want to get an element from an ArrayList in index `i`, you use `someArrayList.get(i)` and the method that gets an ArrayList or any List should define the parameter like this: `someMethod(List hArray)`. Again, because ArrayList isn't an array. – MaxG Mar 24 '16 at 02:29
  • What's the difference between a mean median and a nice median? ;-) – Andreas Mar 24 '16 at 02:29
  • Btw - there is an [apache commons](http://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/index.html) library that will do all of this for you. (Use the class browser to the left to find all the functions you need). – dwjohnston Mar 24 '16 at 02:36
  • I have edited the code and updated it using Tibrogargan's code, but I am still getting errors with the median returns as well as the if statement in mode. and the min and max values. – Pwoods Mar 24 '16 at 03:04

3 Answers3

1

Your hArray is a List. You should convert it to an array first.

getMean(hArray.toArray)

Check out this.

David S.
  • 10,578
  • 12
  • 62
  • 104
  • The methods still expect a `double[]` which is not the result of the `toArray` method, so the signatures must be changed to accept `Double[]` or in your case even `Object[]`, because that's the return type of `toArray` (if you don't pass any parameter). – Philipp Mar 24 '16 at 02:36
  • Yeah~I weep when people mix up `double` and `Double`. :( – David S. Mar 24 '16 at 02:38
1

Replace the section where you're trying to pass a single element from the array to your statistics functions with calls using the whole array and change the signature of the calls so they take a List<Double> param, not a double[]. Something like this:

   double mean = getMean (hArray);
   double median = getMedian (hArray);
   double mode = getMode (hArray);
   double max = getMaxValue(hArray);
   double min = getMinValue (hArray);

//Mean
private static double getMean(List<Double> hArray) {
    double sum = 0;
    for (int i = 0; i < hArray.size(); i++) {
        sum += hArray.get(i);
    }
    return sum / hArray.size();
}

See also: How do you calculate the variance, median, and standard deviation in C++ or Java?

Fix for median:

Copied directly from this above link with some minor modifications to use a List as a param

public Double median(List<Double> list) 
{
   Double[] array = list.toArray(new Double[list.size()]);
   Arrays.sort(data);

   if (data.length % 2 == 0) 
   {
      return (data[(data.length / 2) - 1] + data[data.length / 2]) / 2.0;
   } 
   else 
   {
      return data[data.length / 2];
   }
}

Fix for mode:

public Double mode(List<Double> list) 
{
    java.util.TreeMap<Double,Integer> map = new java.util.TreeMap<>();
    Double maxVal = null;
    int maxCount = 0;

    for (Double d : list) {
        int count = 0;
        if (map.containsKey(d)) {
            count = map.get(d) + 1;
        } else {
            count = 1;
        }
        map.put(d, count);

        if (count > maxCount) {
           maxVal = d;
           maxCount = count;
        }
    }
    return maxVal;
}
Community
  • 1
  • 1
Tibrogargan
  • 4,508
  • 3
  • 19
  • 38
  • I had changed that and the mean works fine now. I also changed the median, mode, max and min. I am now having problems with returns in median. maxvalue in mode and max. and then my min values – Pwoods Mar 24 '16 at 02:56
  • UPDATE, i only have a problem with the median returns now – Pwoods Mar 24 '16 at 03:14
1

This does not compile, you try to pass a Double to a method, which expects a double[]. So you have to change the parameter of your methods and use a List and just pass in the hArray (see Tibrogargan answer - i.e., you would have to modify each of your implementations) or do the following:

  1. create a Double[]

    Double[] hArray2 = hArray.toArray(new Double[hArray.size()]);
    
  2. change your methods' signature, so that they expect an Double[]

    private static double getMean(Double[] hArray) { ...}
    
  3. pass hArray2 instead of hArray

    double mean = getMean(hArray2);
    // ...
    

That should be it.

Philipp
  • 1,289
  • 1
  • 16
  • 37