Connecting Tech Pros Worldwide Help | Site Map

Generics Help

authorityaction's Avatar
Newbie
 
Join Date: Feb 2008
Posts: 6
#1: Feb 4 '08
I am working on an assignment to write a generic class called MyStatisticsClass with type parameter T where T is a numeric object. with a method called standardDeviation that takes a ArrayList of type T and returns a double. I am getting the following error when I try to compile:

Expand|Select|Wrap|Line Numbers
  1. MyStatisticsClass.java:27: standardDeviation(java.util.ArrayList<T>) in MyStatisticsClass<T> cannot be applied to (java.util.ArrayList<java.lang.Double>)
  2.         Double result = standardDeviation(numbers);
Here are some tid bits of the code I have:

Expand|Select|Wrap|Line Numbers
  1.     public static void main(String[] args)
  2.     {
  3.         ArrayList<Double> numbers = new ArrayList<Double>();
  4.         //some random numbers are assigned to numbers here
  5.         Double result = standardDeviation(numbers);//this is causing the error
  6.     }
  7.  
  8. public double standardDeviation(ArrayList<T> a)
  9.     {
  10.         double average = calcAverage(a);
  11.         double top = 0.0;
  12.         //calculate the standard deviation
  13.         for (int i = 0; i < a.size(); i++)
  14.         {
  15.             top = top + Math.pow(a.get(i).doubleValue() - average, 2);
  16.         }
  17.         return Math.sqrt(top / average);
  18.     }
Anyone know what I am doing wrong? I can't seem to figure it out! Thanks for any help.
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Feb 4 '08

re: Generics Help


Your method standardDeviation should be static, right? Shouldn't that solve your problems?
authorityaction's Avatar
Newbie
 
Join Date: Feb 2008
Posts: 6
#3: Feb 4 '08

re: Generics Help


Quote:

Originally Posted by BigDaddyLH

Your method standardDeviation should be static, right? Shouldn't that solve your problems?

If I change it to static I get the following error:

Expand|Select|Wrap|Line Numbers
  1. MyStatisticsClass.java:37: non-static class T cannot be referenced from a static context
  2.     public static double standardDeviation(ArrayList<T> a)
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#4: Feb 4 '08

re: Generics Help


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

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class Example {
  4.     public static <V extends Number> double sum(Collection<V> c){
  5.           double sum = 0;
  6.           for(V v : c) {
  7.               sum += v.doubleValue();
  8.           }
  9.           return sum;
  10.     }
  11.  
  12.     public static <V extends Number> double average(Collection<V> c){
  13.         return sum(c)/c.size();
  14.     }
  15.  
  16.     public static <V extends Number> double standardDeviation(Collection<V> c){
  17.         double average = average(c);
  18.         double sum = 0.0;
  19.         for(V v : c) {
  20.             double diff = v.doubleValue() - average;
  21.             sum += diff * diff;
  22.         }
  23.         return Math.sqrt(sum / average);
  24.     }
  25.  
  26.     public static void main(String[] args) {
  27.         List<Double> data = new ArrayList<Double>();
  28.         Collections.addAll(data, 1.0, 2.0, 3.0, 4.0, 5.0);
  29.         double std = standardDeviation(data);
  30.     }
  31. }
authorityaction's Avatar
Newbie
 
Join Date: Feb 2008
Posts: 6
#5: Feb 5 '08

re: Generics Help


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

Expand|Select|Wrap|Line Numbers
  1. public static <T extends Number> double standardDeviation(ArrayList<T> a)
  2.  
  3. private static <T extends Number> double calcAverage(ArrayList<T> a)
Reply


Similar Java bytes