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()); //add more test cases } }

# distance between two points in java

Page 1 of 1## 9 Replies - 12578 Views - Last Post: 28 February 2011 - 10:49 PM

### #1

# distance between two points in java

Posted 28 February 2011 - 10:01 PM

##
**Replies To:** distance between two points in java

### #2

## 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

## 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

## Re: distance between two points in java

Posted 28 February 2011 - 10:11 PM

### #5

## 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

## Re: distance between two points in java

Posted 28 February 2011 - 10:25 PM

String result="" + distanceA;

### #7

## 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.

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

## 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

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

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

now you can set your String result accordingly

### #9

## 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

## 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

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; }

Page 1 of 1