Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   please debug this generic program (http://www.velocityreviews.com/forums/t621844-please-debug-this-generic-program.html)

asit 06-23-2008 05:35 PM

please debug this generic program
 
//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());
}
}

Andreas Leitgeb 06-23-2008 06:20 PM

Re: please debug this generic program
 
asit <lipun4u@gmail.com> wrote:
> [ 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?


asit 06-23-2008 06:34 PM

Re: please debug this generic program
 
On Jun 23, 11:24 pm, Eric Sosman <Eric.Sos...@sun.com> wrote:
> asit wrote:
> > //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[i].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's becauseyou
> don'tcomprehend.
>
> --
> Eric.Sos...@sun.com


Thanx for ur suggestion.....

Lew 06-23-2008 07:07 PM

Re: please debug this generic program
 
asit wrote:
> Thanx for ur suggestion.....


Please trim the parts you quote to the part that is relevant for your
reply.

Since "Ur" is a proper noun, it should be capitalized.
<http://en.wikipedia.org/wiki/Ur>

--
Lew

Arved Sandstrom 06-23-2008 09:36 PM

Re: please debug this generic program
 
"asit" <lipun4u@gmail.com> wrote in message
news:d3733cb6-21d4-46f0-ab45-4a8f60f98b59@w1g2000prd.googlegroups.com...
> //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




All times are GMT. The time now is 05:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.