# distance between two points in java

Page 1 of 1

## 9 Replies - 13360 Views - Last Post: 28 February 2011 - 10:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=219500&amp;s=a52c056175492ed4c4798096533ad9b8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 umrk

Reputation: 0
• Posts: 11
• Joined: 23-February 11

# distance between two points in java

Posted 28 February 2011 - 10:01 PM

```package unit5;

import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;

public class Distance
{       //instantiates variables
private int x1,y1,x2,y2;

public Distance()
// default coordinates
{
setCoordinates(0,0,0,0);

}

public Distance(int xOne, int yOne, int xTwo, int yTwo)
//.
{
setCoordinates(x1,y1,x2,y2);

}

public void setCoordinates(int xOne, int yOne, int xTwo, int yTwo)
//sets the
{
x1=xOne;
y1=yOne;
x2=xTwo;
y2=yTwo;

}

public String determineClosest( )
{
double distanceA = 0.0, distanceB = 0.0;
String result="";

return result;
}

public String toString()
{
return "";
}
}

The directions are to find which coordinates are closer to the point of origin(0,0).
I am confused on how to write an If statement that would accomplish the  task. As this is my homework, please do not just give me the answer. Try to address where my problem is and suggest a possible solution.

here is the runner:

package unit5;
//� A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Class -
//Lab  -

import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;

public class Lab05e
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner( System.in );

out.print("Enter X1 :: ");
int xOne = keyboard.nextInt();
out.print("Enter Y1 :: ");
int yOne = keyboard.nextInt();
out.print("Enter X2 :: ");
int xTwo = keyboard.nextInt();
out.print("Enter Y2 :: ");
int yTwo = keyboard.nextInt();

Distance test = new Distance();
test.setCoordinates(xOne, yOne, xTwo, yTwo);
out.println(test.determineClosest());

}
}

```

Is This A Good Question/Topic? 0

## Replies To: distance between two points in java

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: distance between two points in java

Posted 28 February 2011 - 10:07 PM

http://en.wikipedia....agorean_theorem

```   x1,y1
| \
|  \
A |   \ C
|    \
|---- x2,y2
B

```

This post has been edited by pbl: 28 February 2011 - 10:08 PM

### #3 umrk

Reputation: 0
• Posts: 11
• Joined: 23-February 11

## Re: distance between two points in java

Posted 28 February 2011 - 10:09 PM

hmm. let me try to work it out. remember, i am slow at Java aha

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: distance between two points in java

Posted 28 February 2011 - 10:11 PM

umrk, on 01 March 2011 - 12:09 AM, said:

hmm. let me try to work it out. remember, i am slow at Java aha

This is only math

A = y2 - y1
B = x2 - x1

C = Math.sqrt(A^2 + b^2)

### #5 umrk

Reputation: 0
• Posts: 11
• Joined: 23-February 11

## Re: distance between two points in java

Posted 28 February 2011 - 10:23 PM

would this also work?
```public String determineClosest( )
{
double distanceA = 0.0, distanceB = 0.0;
distanceA = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

String result="";

return result;
}

```

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: distance between two points in java

Posted 28 February 2011 - 10:25 PM

String result="" + distanceA;

### #7 umrk

Reputation: 0
• Posts: 11
• Joined: 23-February 11

## Re: distance between two points in java

Posted 28 February 2011 - 10:29 PM

that would only print the distance of the points, for my assignment, i need to write an If statement that would basically state,
if {distanceA to (0,0)> than distanceB to (0,0)}
{
String result = B is closer to (0,0)
}

that is what i am trying to do. sorry if that's confusing.

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: distance between two points in java

Posted 28 February 2011 - 10:35 PM

Even easier with one point being 0,0:

distanceA = Math.sqrt((x2 * x2) + (y2 * y2));
distanceB = Math.sqrt((x1 * x1) + (y1 * y1));

now you can set your String result accordingly

### #9 umrk

Reputation: 0
• Posts: 11
• Joined: 23-February 11

## Re: distance between two points in java

Posted 28 February 2011 - 10:40 PM

```public String determineClosest( )
{
double distanceA = 0.0, distanceB = 0.0;
distanceA = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

distanceB = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

if(distanceA>distanceB)
{
String result=(distanceA + " is closer to (0,0)");

}

if(distanceA<distanceB)
{
String result=(distanceA + " is closer to (0,0)");
}

```

something like this?

### #10 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: distance between two points in java

Posted 28 February 2011 - 10:49 PM

Yes and no
Now you declare String inside a {} it can't be seen oustide it
You use twice distanceA in result
You will have to declare it outside
Also as you are playing with int they may be at the same distance

```public String determineClosest( )
{
double distanceA = 0.0, distanceB = 0.0;
distanceA = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));

distanceB = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
String result = "";
if(distanceA>distanceB)
{
result= distanceA + " is closer to (0,0)";

}
else if(distanceA<distanceB)
{
result= distanceB + " is closer to (0,0)";
}
else
{
result = "They are a the same distance";
}
return result;
}

```