java - Trying to calculate the distance between the position of two objects -


i have these 2 instance variables set within constructor of object be:

    (int)(math.random()*100); 

the 2 instance variables are:

    private double xpos;     private double ypos; 

the class called soldier , have 3 other classes inherit soldier, (only constructor @ moment)

the output getting @ moment is:

my name is: cavalier @ position: (45.0,56.0)

my name is: crossbowman @ position: (15.0,91.0)

my name is: halberdier @ position: (67.0,8.0)

i trying calculate distance in x , y positions of objects

the method trying is:

    protected soldier distancebetween(soldier x, soldier y){         double distbetween = (x.xpos - y.xpos)+(x.ypos-y.ypos);         return this;      } 

the method trying achieve 2 objects inherit soldier taken distbetween paremeters, example use names: halberdier, cavalier , crossbowman.

when call method:

    cavalier.distancebetween(cavalier,crossbowman); 

i want calculate distance between x , y coordinates

how achieve this?

completely wrong.

you need return value, not soldier.

this more general:

public static double distancebetween(double x1, double y1, double x2, double y2) {     double dx = math.abs(x2-x1);     double dy = math.abs(y2-y1);      if (dx > dy) {         double r = dy/dx;         return dx*math.sqrt(1.0 + r*r);      } else {         double r = dx/dy;         return dy*math.sqrt(1.0 + r*r);      } } 

the ^ operator not exponentiation; it's xor.

you can override method way:

public static double distancebetween(soldier s1, solder s2) {     return distancebetween(s1.xpos, s1.ypos, s2.xpos, s2.ypos); } 

since having trouble, i'll spell out you:

/**  * distance calc  * user: mduffy  * date: 11/1/2015  * time: 9:58  * @link http://stackoverflow.com/questions/33462961/trying-to-calculate-the-distance-between-the-position-of-two-objects/33463222?noredirect=1#comment54712511_33463222  */ public class soldier {      public final double xpos;     public final double ypos;      public static void main(string[] args) {         soldier s = new soldier(0, 0);         cavalier c = new cavalier(3, 4);         system.out.println(string.format("distance: %f10.3", s.distancebetween(c)));     }      public soldier(double xpos, double ypos) {         this.xpos = xpos;         this.ypos = ypos;     }      public double distancebetween(soldier s) {         return distancebetween(this.xpos, this.ypos, s.xpos, s.ypos);     }      public static double distancebetween(double x1, double y1, double x2, double y2) {         double dx = math.abs(x2-x1);         double dy = math.abs(y2-y1);         if (dx > dy) {             double r = dy/dx;             return dx*math.sqrt(1.0 + r*r);         } else {             double r = dx/dy;             return dy*math.sqrt(1.0 + r*r);         }     }      public static double distancebetween(soldier s1, soldier s2) {         return distancebetween(s1.xpos, s1.ypos, s2.xpos, s2.ypos);     } }  class cavalier extends soldier {     public cavalier(double x, double y) {         super(x, y);     } } 

if want different distance calculation methods (e.g. euclidian, manhattan, pearson, spherical, etc.) can have interface lets change adding new implementation:

public interface distancecalculator {     double distance(double x1, double y1, double x2, double y2); } 

now can switch adding new code rather modifying existing code.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -