please debug this generic program

A

asit

//Stats.java
package tutorial;

public class Stats<T> {
T[] nums;
Stats(T x){
nums=x;
}
double average() {
double sum=0;
for(int i=0;i<nums.length;i++)
sum += nums.doubleValue();
return sum;
}

}


//Demo.java
package tutorial;

public class Demo {
public static void main(String[] args){
int[] x= {1,2,3,4,5,6,7,8,9,10};
Stats<Integer> ob = new Stats<Integer>(x);
System.out.println(ob.average());
}
}
 
A

Andreas Leitgeb

asit said:
[ a sample of code, the errors in which are all
pointed out by the compiler. ]

Which ones of the compiler-errors did you not understand?
 
A

asit

asit said:
//Stats.java
package tutorial;
public class Stats<T> {
T[] nums;
Stats(T x){
nums=x;
}
double average() {
double sum=0;
for(int i=0;i<nums.length;i++)
sum += nums.doubleValue();
return sum;
}

//Demo.java
package tutorial;
public class Demo {
public static void main(String[] args){
int[] x= {1,2,3,4,5,6,7,8,9,10};
Stats<Integer> ob = new Stats<Integer>(x);
System.out.println(ob.average());
}
}

How can we debug the program if we don't know what
it's supposed to do? That is, if somebody offered code
that printed "Bonjour, Welt!" would you be happy?

Still, we can at least point out some of the things
that cannot possibly be right, no matter what the code
is supposed to do:

- The assignment in the constructor for Stats will
not compile. The l.h.s. is an array reference,
and the r.h.s. is not. I imagine that the argument
should have been a T[] rather than a T.

- The use of `nums.doubleValue()' will not compile.
`nums' is an array, and an array has no method
named doubleValue().

- Even if you change to `nums.doubleValue()' the
line still won't compile. You have not told Java
anything about the type T, so Java has no reason to
believe that a T object has a doubleValue() method.

- There's nothing ipso facto wrong about the value
returned by the average() method, except that you
seem to have chosen a misleading name ...

- In main(), x is of type int[]: a reference to an array
whose elements are `int' values. But the constructor
(if altered as suggested above) expects an Integer[]:
a reference to an array whose elements are references
to Integer objects. Not the same thing at all ...

Other remarks: It's unusual for a public class to have a
package-private constructor. Also, your life will be easier
if you use indentation to make your code's logical structure
more apparent. Ifyoudon'tthinkjudicioususeofwhitespaceandsi
milartricksofformattingareaidstocomprehension,it'sbecauseyou
don'tcomprehend.


Thanx for ur suggestion.....
 
A

Arved Sandstrom

asit said:
//Stats.java
package tutorial;

public class Stats<T> {
T[] nums;
Stats(T x){
nums=x;
}
double average() {
double sum=0;
for(int i=0;i<nums.length;i++)
sum += nums.doubleValue();
return sum;
}

}

//Demo.java
package tutorial;

public class Demo {
public static void main(String[] args){
int[] x= {1,2,3,4,5,6,7,8,9,10};
Stats<Integer> ob = new Stats<Integer>(x);
System.out.println(ob.average());
}
}

To add to Eric's comments (and in fact also Andreas' - the compiler errors
are informative) might I respectfully suggest that you're currently in over
your head trying to use generics. Not qualifying T to indicate that the type
has a 'doubleValue' method (Eric's point #3) is one thing - that's
legitimately a generics question - but most of the points raised by Eric are
much more fundamental, and indicate a struggle with basic concepts of Java.

I'd suggest rewriting this to use just Integer or Double - no generics - get
it to work, and understand why it works. Concentrate on getting arrays
right. *Then*, and only then, consider generifying Stats to accept any
Number type.

I'm not tearing you down here. You're just tackling a difficult subject
before you've mastered simpler ones.

AHS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top