Connecting Tech Pros Worldwide Forums | Help | Site Map

Generic class confusion

Newbie
 
Join Date: Nov 2006
Posts: 12
#1: Feb 13 '07
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, MyStatisticsClass, with a type parameter T where T is a numeric object type (e.g., Integer, Double, or any class that extends java.lang.Number). Add a method named standardDeviation 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.ArrayList;
import java.util.Scanner;

public class MyStatisticsClass<T>
{
public static void main(String[] args)
{
ArrayList<Integer> myList1 = new ArrayList<Integer>();
ArrayList<Double> myList2 = new ArrayList<Double>();
ArrayList<Double> myList3 = new ArrayList<Double>();

// Test to see if it works
Scanner scan = new Scanner(System.in);
for(int i=0; i<5; i++);
{
myList1.add(scan.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 = standardDeviation(myList1);
double a2 = standardDeviation(myList2);
double a3 = standardDeviation(myList3);
}

// Standard Deviation method
public double standardDeviation(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.doubleValue();
}

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.doubleValue()-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\MyStatisticsClass.java

MyStatisticsClass.java:44: standardDeviation(java.util.ArrayList<T>) in MyStatisticsClass<T> cannot be applied to (java.util.ArrayList<java.lang.Integer>)
double a1 = standardDeviation(myList1);
^
MyStatisticsClass.java:45: standardDeviation(java.util.ArrayList<T>) in MyStatisticsClass<T> cannot be applied to (java.util.ArrayList<java.lang.Double>)
double a2 = standardDeviation(myList2);
^
MyStatisticsClass.java:46: standardDeviation(java.util.ArrayList<T>) in MyStatisticsClass<T> cannot be applied to (java.util.ArrayList<java.lang.Double>)
double a3 = standardDeviation(myList3);
^
MyStatisticsClass.java:61: cannot find symbol
symbol : method doubleValue()
location: class java.lang.Object
sum1 += temp.doubleValue();
^
MyStatisticsClass.java:61: inconvertible types
found : <nulltype>
required: double
sum1 += temp.doubleValue();
^
MyStatisticsClass.java:70: cannot find symbol
symbol : method doubleValue()
location: class java.lang.Object
preSum = temp.doubleValue()-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.

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Feb 13 '07

re: Generic class confusion


Quote:

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, MyStatisticsClass, with a type parameter T where T is a numeric object type (e.g., Integer, Double, or any class that extends java.lang.Number). Add a method named standardDeviation 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.ArrayList;
import java.util.Scanner;

public class MyStatisticsClass<T>
{
public static void main(String[] args)
{
ArrayList<Integer> myList1 = new ArrayList<Integer>();
ArrayList<Double> myList2 = new ArrayList<Double>();
ArrayList<Double> myList3 = new ArrayList<Double>();

// Test to see if it works
Scanner scan = new Scanner(System.in);
for(int i=0; i<5; i++);
{
myList1.add(scan.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 = standardDeviation(myList1);
double a2 = standardDeviation(myList2);
double a3 = standardDeviation(myList3);
}

// Standard Deviation method
public double standardDeviation(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.doubleValue();
}

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.doubleValue()-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\MyStatisticsClass.java

MyStatisticsClass.java:44: standardDeviation(java.util.ArrayList<T>) in MyStatisticsClass<T> cannot be applied to (java.util.ArrayList<java.lang.Integer>)
double a1 = standardDeviation(myList1);
^
MyStatisticsClass.java:45: standardDeviation(java.util.ArrayList<T>) in MyStatisticsClass<T> cannot be applied to (java.util.ArrayList<java.lang.Double>)
double a2 = standardDeviation(myList2);
^
MyStatisticsClass.java:46: standardDeviation(java.util.ArrayList<T>) in MyStatisticsClass<T> cannot be applied to (java.util.ArrayList<java.lang.Double>)
double a3 = standardDeviation(myList3);
^
MyStatisticsClass.java:61: cannot find symbol
symbol : method doubleValue()
location: class java.lang.Object
sum1 += temp.doubleValue();
^
MyStatisticsClass.java:61: inconvertible types
found : <nulltype>
required: double
sum1 += temp.doubleValue();
^
MyStatisticsClass.java:70: cannot find symbol
symbol : method doubleValue()
location: class java.lang.Object
preSum = temp.doubleValue()-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
Expand|Select|Wrap|Line Numbers
  1. public class MyStatisticsClass<T>
  2.  
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).
Reply