1. Member Join Date
Mar 2011
Posts
41
Rep Power
0

## Please help with my program

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

Java 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;
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);
}
}

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++) {
}

for (int i = 11; i <= 20; 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);
}
}```  Reply With Quote

2. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## 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.  Reply With Quote

3. Member Join Date
Mar 2011
Posts
41
Rep Power
0

## sorry, it won't compile.

Here is the error:

Java Code:
```MyMathClass.java:29: incompatible types
found   : T
required: double
for (double i : myList) {
^
1 error```  Reply With Quote

4. ## 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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•