473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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, 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.
Feb 13 '07 #1
1 5020
r035198x
13,262 8TB
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).
Feb 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
6
by: gong | last post by:
hi i recently looked at alexandrescu's book on c++, and i found it pretty much unintelligible. i have a few points which i wonder about. 1. as a developer, it is important, from a bottom line...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
5
by: Steve K. | last post by:
I know this code is garbage but it should give you an idea of what I'm after: <code> class Test { Type _t = null; public void SetSourceList<T>(Dictionary<string, TsourceList) { _t =...
7
dmjpro
by: dmjpro | last post by:
Now a days i am looking at Generics. Which is very interesting. But i have a confusion ....... class GenericClass<T>{ T obj; GenericClass(T t){ this.obj = t; } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.