java - Why can't I calculate with Number interface? -
i wanted make such class calculating average value:
public static class averagevalue<t extends number> { t data = 0; //error: incompatible types: int cannot converted t int count = 0; public averagevalue() {} public averagevalue(t data) { this.data = data; count = 1; } public void add(t num) { data+=num; //error: bad operand types binary operator '+' count++; } public t average() { return data/(t)count; //error: incompatible types: int cannot converted t } }
i not getting why have interface number if doesn't abstract number. because that's interfaces - abstract operations data without holding data themselves.
the above makes clear you'll not ever able make own number implementation (eg. number unlimited size , precision freaky experimental math programs).
i tried same number
instead of generic t
same results. difference number x = 0
valid.
is there way trick java compile or have "professional" java programmer , use doubles calculate averages on byte arrays?
i not getting why have interface
number
if doesn't abstract number
interface number
abstract number purposes of storing , converting representations. not abstract number purposes of making calculations, because type of result representation not defined.
the above makes clear you'll not ever able make own number implementation (eg. number unlimited size , precision freaky experimental math programs).
bigdecimal
without problem.
number x = 0
valid.
assigning integer
number
ok. assigning integer
extends number
(say, double
) not ok. that's why there difference.
is there way trick java compile or have go full
retardprofessional java programmer , use doubles calculate averages on byte arrays?
you need specify desired representation of result when compute average. abstract out , supply "averager" ingerface, information desired representation needs method in 1 way or other:
interface averagemaker<t extends number> { t initialresult(); t add(t a, number b); t dividebycount(t a, int b); } public static <t extends number, r extends number> r averagevalue(iterable<t> items, averagemaker<r> maker) { r res = maker.initialresult(); int count = 0; (t val : items) { res = maker.add(res, val); count++; } return maker.dividebycount(res, count); }
define several average makers, this:
static final averagemaker<double> doubleavg = new averagemaker<double>() { public double initialresult() { return 0.0; } public double add(double a, number b) { return + b.doublevalue(); } public double dividebycount(double a, int b) { return a/b; } }; static final averagemaker<integer> intavg = new averagemaker<integer>() { public integer initialresult() { return 0; } public integer add(integer a, number b) { return + b.intvalue(); } public integer dividebycount(integer a, int b) { return a/b; } };
now can use them in code averagevalue
method:
list<integer> = new arraylist<integer>(); a.add(4); a.add(8); a.add(91); a.add(18); double avgdouble = averagevalue(a, doubleavg); int avgint = averagevalue(a, intavg); system.out.println(avgdouble); // prints 30.25 system.out.println(avgint); // prints 30
Comments
Post a Comment