Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > please debug this generic program

Reply
Thread Tools

please debug this generic program

 
 
asit
Guest
Posts: n/a
 
      06-23-2008
//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());
}
}
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      06-23-2008
asit <(E-Mail Removed)> 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?

 
Reply With Quote
 
 
 
 
asit
Guest
Posts: n/a
 
      06-23-2008
On Jun 23, 11:24 pm, Eric Sosman <(E-Mail Removed)> 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.
>
> --
> (E-Mail Removed)


Thanx for ur suggestion.....
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-23-2008
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
 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      06-23-2008
"asit" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> //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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please debug this program Prateek Agarwal Ruby 4 07-26-2009 04:15 AM
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments