problems about generics
Hi, I'm new to this forum.
I've the following problem:
public interface Field<T> {
T getOne();
T getZero();
T plus(T x);
T times(T x);
}
public class DoubleField implements Field<Double> {
public DoubleField(double d) {
this.d = d;
}
public Double getOne() {
return 1.0;
}
public Double getZero() {
return 0.0;
}
public Double plus(Double x) {
return d + x.d;
}
public Double times(Double x) {
return d * x.d;
}
private double d;
}
Now I must implement a generic class Polynomial that rispects the following uses case :
DoubleField[ ] d = { new DoubleField(4), ...};
Polynomial<DoubleField> p = new Polynomial<DoubleField>(d);
System.out.println(p.eval(new DoubleField(3));
So...
public class Polynomial< I don't know > {
public Polynomial(//I don't hnow) {
}
// I don't think this type is right !
public T eval(Field<T> x) {
T val = x.getZero();
T pow = x.getOne();
for(int i = 0; i < coef.length; i++){
val.plus(coef.times(pow));
pow = x.times(pow);
}
return val;
}
private //Type [ ] coef;
}
Can you help me, please ?

