Writing a Generic Vector (Math) class in Java -
i need write vector class accept primitive number types in java. vector class should accept 2 components. here having trouble.
i must write function adds 2 vectors , return new vector.
if knows solution of allowing vector class accepts in primitive types , perform vector operations python, please point me in right direction!
something in pseudocode:
addvectors( v1, v2): return new vector( v1.getx + v2.getx, v1.gety + v2.gety)
here snipped code of vector class:
public class vector<t> { private t x; private t y; public vector(t x, t y){ this.x = x; this.y = y; } public t getx(){ return x; } public t gety(){ return y; } }
here go:
class vector{ boolean isinteger = false; boolean isfloat = false; boolean isdouble = false; object n1; object n2; public vector(int x, int y){ n1 = x; n2 = y; isinteger = true; } public vector(double x, double y){ n1 = x; n2 = y; isdouble = true; } public vector(float x, float y){ n1 = x; n2 = y; isfloat = true; } public object add(){ if(isinteger) return (int) n1 + (int) n2; else if(isfloat) return (float) n2 + (float) n1; else if(isdouble){ return (double) n2 + (double) n1; } return -1; }
}
Comments
Post a Comment