please debug this generic program

Discussion in 'Java' started by asit, Jun 23, 2008.

  1. asit

    asit Guest

    //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());
    }
    }
    asit, Jun 23, 2008
    #1
    1. Advertising

  2. asit <> 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?
    Andreas Leitgeb, Jun 23, 2008
    #2
    1. Advertising

  3. asit

    asit Guest

    On Jun 23, 11:24 pm, Eric Sosman <> 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.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.....
    asit, Jun 23, 2008
    #3
  4. asit

    Lew Guest

    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
    Lew, Jun 23, 2008
    #4
  5. "asit" <> wrote in message
    news:...
    > //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
    Arved Sandstrom, Jun 23, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,022
    Chaitanya
    Feb 3, 2009
  2. KK
    Replies:
    2
    Views:
    516
    Big Brian
    Oct 14, 2003
  3. Replies:
    2
    Views:
    422
  4. minlearn
    Replies:
    2
    Views:
    444
    red floyd
    Mar 13, 2009
  5. Prateek Agarwal

    Please debug this program

    Prateek Agarwal, Jul 25, 2009, in forum: Ruby
    Replies:
    4
    Views:
    111
    Brian Candler
    Jul 26, 2009
Loading...

Share This Page