Generic class confusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bjwillykajilly
    New Member
    • Nov 2006
    • 12

    #1

    Generic class confusion

    Well, I got an assignment due this morning, so i guess ill end up turning it in a day late eh. anyways. I have a couple problems that I don't know what to do with. the objective is here:

    Write a generic class, MyStatisticsCla ss, with a type parameter T where T is a numeric object type (e.g., Integer, Double, or any class that extends java.lang.Numbe r). Add a method named standardDeviati on that takes an ArrayList of type T and returns as a double the standard deviation of the values in the ArrayList. Use the doubleValue( ) method in the Number class to retrieve the value of each number as a double. The standard deviation of a list of numbers n1, n2, ..., nm is defined as the square root of the average of the following numbers:
    (n1 - a)2, (n2 - a)2, ..., (nm - a)2, where a is the average of the numbers n1, n2, ..., nm.

    and here's my program that i've written, and i have no idea if i'm even close.

    import java.util.Array List;
    import java.util.Scann er;

    public class MyStatisticsCla ss<T>
    {
    public static void main(String[] args)
    {
    ArrayList<Integ er> myList1 = new ArrayList<Integ er>();
    ArrayList<Doubl e> myList2 = new ArrayList<Doubl e>();
    ArrayList<Doubl e> myList3 = new ArrayList<Doubl e>();

    // Test to see if it works
    Scanner scan = new Scanner(System. in);
    for(int i=0; i<5; i++);
    {
    myList1.add(sca n.nextInt());
    }
    for(double j=0; j<5; j++)
    {
    myList2.add(j+( j+0.5));
    }
    for(double k=0; k<5; k++)
    {
    myList3.add(k+( k+0.1));
    }


    double a1 = standardDeviati on(myList1);
    double a2 = standardDeviati on(myList2);
    double a3 = standardDeviati on(myList3);
    }

    // Standard Deviation method
    public double standardDeviati on(ArrayList<T> list)
    {
    double sum1=0;
    double sum2=0;
    double preSum=0;
    double deviation=0;

    // collect the sum of all the numbers in the list
    for(int i=0;i<list.size ();i++)
    {
    T temp = list.get(i);
    sum1 += temp.doubleValu e();
    }

    double average1=sum1/list.size(); // average all of the first sum

    // collect the sum of the numbers - the average all squared
    for(int j=0;j<list.size ();j++)
    {
    T temp = list.get(j);
    preSum = temp.doubleValu e()-average1;
    sum2+= preSum*preSum;
    }

    double average2=sum2/list.size(); // average the second sum
    return average2; // return the standard deviation as a double.
    }
    }

    and, here's the errors, as recieved from jgrasp v1.8 and jdk 6:

    ----jGRASP exec: javac -g -Xlint I:\MSU\Spring 06\csc 232\Assignment 1\MyStatisticsC lass.java

    MyStatisticsCla ss.java:44: standardDeviati on(java.util.Ar rayList<T>) in MyStatisticsCla ss<T> cannot be applied to (java.util.Arra yList<java.lang .Integer>)
    double a1 = standardDeviati on(myList1);
    ^
    MyStatisticsCla ss.java:45: standardDeviati on(java.util.Ar rayList<T>) in MyStatisticsCla ss<T> cannot be applied to (java.util.Arra yList<java.lang .Double>)
    double a2 = standardDeviati on(myList2);
    ^
    MyStatisticsCla ss.java:46: standardDeviati on(java.util.Ar rayList<T>) in MyStatisticsCla ss<T> cannot be applied to (java.util.Arra yList<java.lang .Double>)
    double a3 = standardDeviati on(myList3);
    ^
    MyStatisticsCla ss.java:61: cannot find symbol
    symbol : method doubleValue()
    location: class java.lang.Objec t
    sum1 += temp.doubleValu e();
    ^
    MyStatisticsCla ss.java:61: inconvertible types
    found : <nulltype>
    required: double
    sum1 += temp.doubleValu e();
    ^
    MyStatisticsCla ss.java:70: cannot find symbol
    symbol : method doubleValue()
    location: class java.lang.Objec t
    preSum = temp.doubleValu e()-average1;
    ^
    6 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    Sorry for such a ginormous post, please any and all help, any question as to whats goin on just ask, its a pretty simple program, so it should be mostly self explanitory, thanks for the help in advance.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by bjwillykajilly
    Well, I got an assignment due this morning, so i guess ill end up turning it in a day late eh. anyways. I have a couple problems that I don't know what to do with. the objective is here:

    Write a generic class, MyStatisticsCla ss, with a type parameter T where T is a numeric object type (e.g., Integer, Double, or any class that extends java.lang.Numbe r). Add a method named standardDeviati on that takes an ArrayList of type T and returns as a double the standard deviation of the values in the ArrayList. Use the doubleValue( ) method in the Number class to retrieve the value of each number as a double. The standard deviation of a list of numbers n1, n2, ..., nm is defined as the square root of the average of the following numbers:
    (n1 - a)2, (n2 - a)2, ..., (nm - a)2, where a is the average of the numbers n1, n2, ..., nm.

    and here's my program that i've written, and i have no idea if i'm even close.

    import java.util.Array List;
    import java.util.Scann er;

    public class MyStatisticsCla ss<T>
    {
    public static void main(String[] args)
    {
    ArrayList<Integ er> myList1 = new ArrayList<Integ er>();
    ArrayList<Doubl e> myList2 = new ArrayList<Doubl e>();
    ArrayList<Doubl e> myList3 = new ArrayList<Doubl e>();

    // Test to see if it works
    Scanner scan = new Scanner(System. in);
    for(int i=0; i<5; i++);
    {
    myList1.add(sca n.nextInt());
    }
    for(double j=0; j<5; j++)
    {
    myList2.add(j+( j+0.5));
    }
    for(double k=0; k<5; k++)
    {
    myList3.add(k+( k+0.1));
    }


    double a1 = standardDeviati on(myList1);
    double a2 = standardDeviati on(myList2);
    double a3 = standardDeviati on(myList3);
    }

    // Standard Deviation method
    public double standardDeviati on(ArrayList<T> list)
    {
    double sum1=0;
    double sum2=0;
    double preSum=0;
    double deviation=0;

    // collect the sum of all the numbers in the list
    for(int i=0;i<list.size ();i++)
    {
    T temp = list.get(i);
    sum1 += temp.doubleValu e();
    }

    double average1=sum1/list.size(); // average all of the first sum

    // collect the sum of the numbers - the average all squared
    for(int j=0;j<list.size ();j++)
    {
    T temp = list.get(j);
    preSum = temp.doubleValu e()-average1;
    sum2+= preSum*preSum;
    }

    double average2=sum2/list.size(); // average the second sum
    return average2; // return the standard deviation as a double.
    }
    }

    and, here's the errors, as recieved from jgrasp v1.8 and jdk 6:

    ----jGRASP exec: javac -g -Xlint I:\MSU\Spring 06\csc 232\Assignment 1\MyStatisticsC lass.java

    MyStatisticsCla ss.java:44: standardDeviati on(java.util.Ar rayList<T>) in MyStatisticsCla ss<T> cannot be applied to (java.util.Arra yList<java.lang .Integer>)
    double a1 = standardDeviati on(myList1);
    ^
    MyStatisticsCla ss.java:45: standardDeviati on(java.util.Ar rayList<T>) in MyStatisticsCla ss<T> cannot be applied to (java.util.Arra yList<java.lang .Double>)
    double a2 = standardDeviati on(myList2);
    ^
    MyStatisticsCla ss.java:46: standardDeviati on(java.util.Ar rayList<T>) in MyStatisticsCla ss<T> cannot be applied to (java.util.Arra yList<java.lang .Double>)
    double a3 = standardDeviati on(myList3);
    ^
    MyStatisticsCla ss.java:61: cannot find symbol
    symbol : method doubleValue()
    location: class java.lang.Objec t
    sum1 += temp.doubleValu e();
    ^
    MyStatisticsCla ss.java:61: inconvertible types
    found : <nulltype>
    required: double
    sum1 += temp.doubleValu e();
    ^
    MyStatisticsCla ss.java:70: cannot find symbol
    symbol : method doubleValue()
    location: class java.lang.Objec t
    preSum = temp.doubleValu e()-average1;
    ^
    6 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    Sorry for such a ginormous post, please any and all help, any question as to whats goin on just ask, its a pretty simple program, so it should be mostly self explanitory, thanks for the help in advance.
    1.) Please use code tags when posting code.
    2.)The line
    Code:
    public class MyStatisticsClass<T>
    does not say what is required. T is supposed to be a subclass of the Number class. Correct this and anywhere else you feel should be corrected to reflect this and post (using code tags).

    Comment

    Working...