0

I am trying to write a simple program for a class called Number that takes a generic parameter. my idea here is I would like to store these Number in List so that I can traverse through it and find sum.

my list would be of type [2,3.4,4,5]. That is a mixture of int, doubles. my current code is below import java.util.*;

class Number<T> {
    T n;
    Number(T n) {
        this.n=n;
    }
    public T getnumber(){
        return n;
    }

    public String toString() {
        return n.toString();
    }
}

public class GenericsWildcards {

    public static void main(String[] args) {
        Number num1=new Number(5);
        Number num2= new Number(5.4);
        Number num3=new Number(1.2);
        List<Number> list=new ArrayList<Number>();
        list.add(num1);
        list.add(num2);
        list.add(num3);
        System.out.println(list);//prints correctly
        double sum=0;
        for (Number i : list){
            sum+=i.getnumber();//compile error here
        }
        System.out.println("sum is "+ sum);
        }

    }

The sysout prints correctly the list, but I am unable to getnumber since it is return of type Object. I tried casting it to double,sum+=(Double)i.getnumber(); but still did not help. how can I fix this? any idea for improvement on better implementation is very much appreciated. Thank you

brain storm
  • 30,124
  • 69
  • 225
  • 393

4 Answers4

1

You have left out the generic type parameter when you create instances of Number, that's why they return Objects.

Are you aware that Java's API already has a Number class that is the super class for all the reference types for all the primitive number types (Integer, Float, Double, Long, etc)?

Xabster
  • 3,710
  • 15
  • 21
1

You do not need to create your own Number class to accomplish this. You can just use the Java Library class of Number to accomplish this with a little help from autoboxing.

        public static void main(String[] args) {
            List<Number> list = new ArrayList<Number>();
            list.add(5);
            list.add(5.4);
            list.add(1.2);

            System.out.println(list);

            double sum = 0;
            for(Number i : list){
                sum += i.doubleValue();
            }

            System.out.println("sum is " + sum);
        }

I used doubleValue() to add the numbers together, while maintaining the decimal information.

tFrisch
  • 133
  • 4
0

You are using your Number as a raw type (you didn't specify T), so all genetic methods revert to Object, which is why getNumber() returns Object for you.

Step 1: Throw away your Number class and use java's java.lang.Number instead.

Step 2: Make use of auto-boxing:

Number num1 = 5; // Integer
Number num2 = 5.4; // Double
Number num3 = 1.2; // Double

Step 3: Sum the return of doubleValue() of the numbers in your list

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You don't want generics, you probably want subclassing, even then you are going to need to wrap the sum operation. For instance given your code, what you actually want to write is

Number<Integer> num1 = new Number<Integer>(5);
Number<Double> num2 = new Number<Double>(5.4);

Notice how we have to give the type for the generic, that is to say num1 is a Number<Integer> and num2 is a Number<Integer>, however this causes a problem if we want to create a list because num1 and num2 are different types.

Because Java didn't always have generices, classes like List, ArrayList, etc. used to just take objects, thus if you do not pass in a type it defaults to object. Therefore your code is equivalent to

Number<Object> num1 = new Number<Object>(5);
Number<Object> num2 = new Number<Object>(5.4);
...
List<Number<Object>> list = new ArrayList<Number<Object>>();

in fact it would be impossible to implement in Java what you described, however this is desirable because otherwise it would be impossible to know at compile time what type i.getnumber() was and so the program couldn't type check (how is it supposed to know that you only add ints and doubles).

P.S. you may be looking for the visitor pattern http://en.wikipedia.org/wiki/Visitor_pattern#Java_example

MWB
  • 171
  • 6