how to use different class function in java -
hi im new java unsure how use class function in java . teacher gave point2d class , wants use function in there. there function call distanceto
// return euclidean distance between point , point public double distanceto(final point2d that) { final double dx = this.x - that.x; final double dy = this.y - that.y; return math.sqrt(dx*dx + dy*dy); } i not sure how suppose implement this. code
public static int calc(int amount) { (int t = 0; t < amount; t++) { double current = 0; double x = stdrandom.random(); double y = stdrandom.random(); point2d p = new point2d(x, y); if ( current < distanceto(point2d p ) ) { } i tried use distanceto(p) , distanceto(poin2d) , nothing works.
thanks in advance
public static int calc(int amount) static, , distanceto not.
for not being static, distanceto requires enclosing instance of object, say: new point2d().distanceto(...).
you can call distanceto point2d have, p2:
p2.distanceto(p); or can try turning distanceto static method, receive 2 points arguments:
public static double distanceto(final point2d one, final point2d that) { final double dx = one.x - that.x; final double dy = one.y - that.y; return math.sqrt(dx*dx + dy*dy); } and call using just:
distanceto(p, p2); ps.: alternative, maybe solution turn calc non-static. should try it, maybe.
Comments
Post a Comment