Hi everyone,
I've been working on this for days. My "program" is supposed to take an integer or double arrayList of numbers, and then calculate the standard deviation of those numbers using a generic method.
My code is listed below. I've tried fiddling with this for hours, but some error always pops up.
If anyone sees anything wrong, could you please show me the error(s) of my way?
Thanks!
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); } }