• 03-21-2011, 04:47 PM
TopNFalvors
Greetings,

I've have the program below that I'm having much difficulty getting to work properly.

It is supposed to take an arrayList of numbers, integer, double, etc...and then calculate the standard deviation of those numbers using a generic method.

So far I've gotten no where.

I'm posting my code below, and if anyone could be so kind as to take a look at the code for me, I would greatly appreciate it.

Thank you

Code:

``` import java.util.ArrayList; class MyMathClass <T extends Number> {         private ArrayList<T> myArrayList = new ArrayList<T>(10);         private ArrayList<T> myList = new ArrayList<T>(10);                 public MyMathClass(ArrayList<T> al)         {                 myArrayList = al;                 myList = al;         }                 public double standardDeviation(ArrayList<T> list)         {                                 double n = 0;                 double avg = 0.0;                 double s = 0.0;                 double total = 0.0;                 double answer = 0.0;                 myList = list;                                                 for (double i : myList) {                         n++;                         double d = i - avg;                         avg = avg + d / n;                         s = s +  d * (i - avg);                         System.out.println(s);                 }                 total = (s / n);                 answer = Math.sqrt(total);                 return answer;         } } class MyMathClassTest {         public static void main(String args[])         {                 System.out.println("Calculating Standard Deviation...\n");                 double resultDouble = 0.0;                 double resultInteger = 0.0;                                 ArrayList<Double> doubleArray = new ArrayList<Double>(10);                 ArrayList<Integer> integerArray = new ArrayList<Integer>(10);                                 for (double i = 1.0; i <= 10.0; i++) {                         doubleArray.add(i);                 }                                 for (int i = 11; i <= 20; i++) {                         integerArray.add(i);                 }                                         MyMathClass<Double> myDouble = new MyMathClass<Double>(doubleArray);                 MyMathClass<Integer> myInteger = new MyMathClass<Integer>(integerArray);                                 resultDouble = myDouble.standardDeviation(doubleArray);                           System.out.println("\nanswer for double = " + resultDouble);                                        resultInteger = myInteger.standardDeviation(integerArray);                                 System.out.println("\nanswer for integer = " + resultInteger);         } }```
• 03-21-2011, 05:53 PM
Tolls
Where's it going wrong?
Errors?
Exceptions?
Does it compile?

People won't simply look at your code without an idea of what problem they are supposed to be identifying.
• 03-21-2011, 09:42 PM
TopNFalvors
sorry, it won't compile.

Here is the error:

Code:

``` MyMathClass.java:29: incompatible types found  : T required: double                 for (double i : myList) {                                 ^ 1 error```
• 03-21-2011, 09:45 PM
Zack
Because in that class, the 'myList' variable is declared as an ArrayList of type T. That means that iterating through it, you will receive only T-type objects, not double-type.

My best suggestion would be to loop through each item as T, for (T i : myList) { and cast i to a double.