Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: What the heck is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default What the heck is wrong with my code?

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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What the heck is wrong with my code?

    but some error always pops up.
    What error? Compile time? Run time? Posting them fully will facilitate you getting help.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What the heck is wrong with my code?

    Sorry, here is the compile error:

    MyMathClass.java:29: incompatible types
    found   : T
    required: double
    		for (double i : myList) {
    		                ^
    1 error

    Thanks!

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What the heck is wrong with my code?

    The variable myList is specified by a generic - the compile time check prevents you from assuming it is a double (in other words doesn't allow you to iterate over the list that way). You know its a Number (eg T extends Number), so use the methods the class Number provides to retrieve the double value.

  5. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (March 21st, 2011)

Similar Threads

  1. What is Wrong with my code
    By xew123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:59 AM
  2. What's wrong with my code?
    By lindmando in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2011, 11:13 PM
  3. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM
  4. What is wrong with my code?
    By phantom06 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2011, 05:21 AM
  5. JEvilProgram and what the heck is this error?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2010, 04:27 AM