Results 1 to 4 of 4
Thread: Please help with my program
- 03-21-2011, 03:47 PM #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; 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, 04:53 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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, 08:42 PM #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
- 03-21-2011, 08:45 PM #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.
Similar Threads
-
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
Execute A program from a Program!
By Moncleared in forum Advanced JavaReplies: 2Last Post: 02-22-2009, 04:17 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks