Results 1 to 4 of 4
  1. #1
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

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

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    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.

  3. #3
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    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

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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

  1. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  2. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  3. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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