Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java methods getting euclidean distance

public class Point
{ 
// Placeholders for xcoordinate, ycoordinate, and quadrants
int xcoord = 0;
int ycoord =0;
double distance = 0.0;
String quadrant = ("NW");


//moveUp changes the y coordinate 
void moveUp (int x) {
    int moveUp = ycoord + x;
    ycoord= moveUp;
    System.out.println(moveUp);
    }
// moveDown changes the y coordinate    
void moveDown (int y){
    int moveDown = ycoord - y;
    ycoord =moveDown;
    System.out.println(moveDown);}
// moveLeft changes the x coordinate    
void moveLeft (int f){
    int moveLeft = xcoord -f ;
    xcoord = moveLeft;
    System.out.println(moveLeft);}
// moveRight changes the x coordinate   
void moveRight (int h) {
    int moveRight  = xcoord +h ;
    xcoord = moveRight;
    System.out.println(moveRight);}

//  This takes the y coordinate and the xcoordinate and places it into a quadrant. Then it returns the quadrant.
String quadrant () {    
    if (xcoord >= 0 && ycoord  >=0){
        return quadrant = ("NE"); }
    else if ( xcoord < 0 && ycoord < 0 ) {
        return quadrant = ("SW");}
    else if (xcoord <0 && ycoord >0 ){
        return quadrant = ("NW");}
    else if (xcoord >0 && ycoord <0){
        return quadrant = ("SE");}
    else {
        return quadrant = ("Origin");}  
}
//euclidean distance (?)
Point distance (Point other) {
    Point result = new Point(); 
    result.ycoord = Math.abs (ycoord - other.ycoord);
    result.xcoord = Math.abs (xcoord- other.xcoord);    
    result.distance = Math.sqrt((result.ycoord)*(result.ycoord) +(result.xcoord)*(result.xcoord));
    System.out.println(result);
    return result;

    }

}   

Here is the whole code. What I'm having trouble with is for some reason my distance method isn't working properly and I don't know why. It returns Point@24a42c89, instead of any number. Please let me know why it is returning this instead of a number. Thanks so much. I'm a beginner and have little knowledge on Java.

like image 955
user3444564 Avatar asked Dec 20 '22 10:12

user3444564


2 Answers

You are getting a Point as a result because you are returning a Point object. There is no reason to declare a Point object when calculating the distance between two points. The results of all calculations here are numbers, so declare all variables as double:

double distance (Point other) {
    double deltaX = ycoord - other.ycoord;
    double deltaY = xcoord - other.xcoord;
    double result = Math.sqrt(deltaX*deltaX + deltaY*deltaY);
    return result; 
}

Note there is no need for Math.abs() since squaring two numbers always results in a non-negative number.

This also removes the need of storing a distance value in every point. This should make sense because a point is a pair of (x, y) coordinates, but does not have an inherent distance. Rather "distance" is a relationship between two points.

like image 193
Code-Apprentice Avatar answered Dec 22 '22 00:12

Code-Apprentice


Your method returns a Point because you return result which is an instance of Point. If you want a double (value) for the distance you should do it this way:

//euclidean distance (?)
double distance (Point other) {
    Point result = new Point(); 
    result.ycoord = Math.abs (ycoord - other.ycoord);
    result.xcoord = Math.abs (xcoord- other.xcoord);    
    result.distance = Math.sqrt((result.ycoord)*(result.ycoord) +(result.xcoord)*(result.xcoord));
    System.out.println(result);
    return result.distance; 
    }
} 
like image 30
Daniel Ribeiro Moreira Avatar answered Dec 22 '22 00:12

Daniel Ribeiro Moreira