2

I am implementing Polynomial class with generalized type of polynomial coefficients. I have this code:

public class Polynomial<T> {

private HashMap<Integer, T > polynomial;

public Polynomial(T coefficient, Integer index) {
    polynomial = new HashMap<Integer, T>();
    if (coefficient!=0) 
        polynomial.put(index, coefficient);
}

public void sum(Polynomial<T> w) {
    for (Map.Entry<Integer, T> e : w.polynomial.entrySet()) {
        T tmp = polynomial.get(e.getKey());
        if(tmp==null)
            polynomial.put(e.getKey(), e.getValue());
        else {
            polynomial.remove(e.getKey());
            if (tmp+e.getValue()!=0)
                polynomial.put(e.getKey(), tmp+e.getValue());
        }
    }
}

...

}

which for obvious reasons does not compile. Operators: ==, !=, +, - and * are not defined for generalized type T. From what I know in Java I can't override operators. How can I solve this problem?

jmj
  • 237,923
  • 42
  • 401
  • 438
xan
  • 1,053
  • 1
  • 10
  • 29
  • 1
    Look at this question: http://stackoverflow.com/questions/8669838/java-generics-and-adding-numbers-together/8669901#8669901. – Novakov May 15 '12 at 12:07
  • I think a better model would be a Monomial, a single term with a coefficient and an exponent. A Polynomial is just a collection of Monomials. It'll be a better representation of something like y = c*x^1000 + 1. – duffymo May 15 '12 at 12:09

2 Answers2

2

Since generics in Java work differently from those in C++, you can't use operators. You need to assure your type T implements some interface and use the interface's methods for the calculations. Suppose you used Number interface from standard library, which allows you to call doubleValue() on you object and calculate based on that, you could use a definition such as as private HashMap<Integer, T extends Number >, in which case you will be able to access Number's methods from T.

You could also make your own classes based on a custom interface with methods such as add(), mul() etc. Since these are method calls, you can't use native types here, so much of the performance is lost and there is less reason to write code such as in the example in Java than in C++. Some libraries, like Trove, go as far a actually using code generation in place of Java Generics to get a C++-like preprocessor-style behavior for a set of parametrized classes.

Michał Kosmulski
  • 9,855
  • 1
  • 32
  • 51
1

You can be a little more specific as to what types you accept when you declare your class. This means that you can specify that the type parameter must be a Number:

public class Polynomial<T extends Number>

You can then use the methods in the Number class (intValue(), doubleValue(), etc.) to turn it into a primitive, and do arithmetic on that. This isn't ideal, I know, but should work OK if you use doubleValue() and your numbers aren't too big.

Harry Cutts
  • 1,352
  • 11
  • 25
  • is too much specific for me. The way I see it is when I write ComplexNumber class it also should work fine (with T =Integer or Double it should works fine too) with my Polynomial class. So I need some interface I think. But don't know how to do that, is it even possible? – xan May 15 '12 at 12:30
  • Hmm. I'm not aware that it is possible without wrapping Integer, Double etc. in classes that also implement the same interface as ComplexNumber. I always thought that Number and its subclasses needed more interfaces. – Harry Cutts May 15 '12 at 16:42