Generics Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • authorityaction
    New Member
    • Feb 2008
    • 6

    Generics Help

    I am working on an assignment to write a generic class called MyStatisticsCla ss with type parameter T where T is a numeric object. with a method called standardDeviati on that takes a ArrayList of type T and returns a double. I am getting the following error when I try to compile:

    [code=java]MyStatisticsCla ss.java:27: standardDeviati on(java.util.Ar rayList<T>) in MyStatisticsCla ss<T> cannot be applied to (java.util.Arra yList<java.lang .Double>)
    Double result = standardDeviati on(numbers);[/code]

    Here are some tid bits of the code I have:

    [code=java] public static void main(String[] args)
    {
    ArrayList<Doubl e> numbers = new ArrayList<Doubl e>();
    //some random numbers are assigned to numbers here
    Double result = standardDeviati on(numbers);//this is causing the error
    }

    public double standardDeviati on(ArrayList<T> a)
    {
    double average = calcAverage(a);
    double top = 0.0;
    //calculate the standard deviation
    for (int i = 0; i < a.size(); i++)
    {
    top = top + Math.pow(a.get( i).doubleValue( ) - average, 2);
    }
    return Math.sqrt(top / average);
    }[/code]

    Anyone know what I am doing wrong? I can't seem to figure it out! Thanks for any help.
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Your method standardDeviati on should be static, right? Shouldn't that solve your problems?

    Comment

    • authorityaction
      New Member
      • Feb 2008
      • 6

      #3
      Originally posted by BigDaddyLH
      Your method standardDeviati on should be static, right? Shouldn't that solve your problems?
      If I change it to static I get the following error:

      [code=java]MyStatisticsCla ss.java:37: non-static class T cannot be referenced from a static context
      public static double standardDeviati on(ArrayList<T> a)[/code]

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Your static method should be generic. I don't know about the class:

        [CODE=Java]import java.util.*;

        public class Example {
        public static <V extends Number> double sum(Collection< V> c){
        double sum = 0;
        for(V v : c) {
        sum += v.doubleValue() ;
        }
        return sum;
        }

        public static <V extends Number> double average(Collect ion<V> c){
        return sum(c)/c.size();
        }

        public static <V extends Number> double standardDeviati on(Collection<V > c){
        double average = average(c);
        double sum = 0.0;
        for(V v : c) {
        double diff = v.doubleValue() - average;
        sum += diff * diff;
        }
        return Math.sqrt(sum / average);
        }

        public static void main(String[] args) {
        List<Double> data = new ArrayList<Doubl e>();
        Collections.add All(data, 1.0, 2.0, 3.0, 4.0, 5.0);
        double std = standardDeviati on(data);
        }
        }[/CODE]

        Comment

        • authorityaction
          New Member
          • Feb 2008
          • 6

          #5
          Thanks for that code! I changed to the following and everything worked.

          [code=java]public static <T extends Number> double standardDeviati on(ArrayList<T> a)

          private static <T extends Number> double calcAverage(Arr ayList<T> a)[/code]

          Comment

          Working...