Results 1 to 7 of 7
Thread: Distance Between Two Points
- 09-08-2011, 05:09 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 14
- Rep Power
- 0
Distance Between Two Points
Been at this for a while, and I have no idea how to get to the end result. Here is what I have so far:
Point Class:
TestPoint Class:Java Code:public class Point { private int x; private int y; public Point() { } public Point(int x, int y) { this(); this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public void double getDistance(double x1, double x2, double y1, double y2) { double d = Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2)); } }
I know that printout is wrong, but I wouldn't know how to get the two points in there.Java Code:public class PointTest { public static void main(String[] args) { Point p1 = new Point(0,0); Point p2 = new Point(10,20); System.out.println("The distance between p1 and p2 is: " p1.getDistance(p2)); } }
Any suggestions? Thanks in advance.
- 09-08-2011, 05:13 AM #2
Re: Distance Between Two Points
The getDistance method has four doubles as parameters yet you are passing a single Point when you call it.
- 09-08-2011, 05:30 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
Re: Distance Between Two Points
Lets see. Your first problem is that you have your getDistance returning both void and double. You want an answer, so remove the void part.
Second problem is that you don't actually return anything in your getDistance method.
Your third problem is that you need a '+' between elements in your println:
Your fourth problem is that you are providing your getDistance method with a Point object as its augment instead of four doubles. There are several ways you could fix this. You could change the augments the getDistance method wants, or you can change the augments you are providing it. And there are again several ways to do that.Java Code:System.out.println("The distance between p1 and p2 is: " + p1.getDistance(p2));Last edited by Taikei_no_Yuurei; 09-08-2011 at 06:21 AM.
- 09-08-2011, 05:34 AM #4
Member
- Join Date
- Aug 2011
- Posts
- 14
- Rep Power
- 0
Re: Distance Between Two Points
Taikei_no_Yuurei,
I was about to post that I just did that. Thanks for your help. The main reason I wanted to post was to see if there is a difference between having p1.getDistance(...) and p2.getDistance(...)? It seems like it's bringing out the same output, but wasn't exactly sure.
- 09-08-2011, 05:35 AM #5
Re: Distance Between Two Points
I have to question why would you retrieve the x and y values from the Point object only to pass them back into the object as parameters?
- 09-08-2011, 05:39 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
Re: Distance Between Two Points
It isn't exactly the most finessed way to write the program, but it works. Ideally you would have the getDistance method simply use the numbers for its own point and only require you to pass on the two numbers from the other point (or the point itself and let the method do the extraction).
- 09-08-2011, 06:23 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Set distance from border to component
By Josep_16 in forum AWT / SwingReplies: 1Last Post: 08-22-2011, 12:21 AM -
[jFreeChart] Converting screen-points to points, which are relative to the axis
By fyaxic in forum AWT / SwingReplies: 1Last Post: 08-11-2011, 10:46 AM -
Calculating the distance between two points problem
By silverglade in forum New To JavaReplies: 16Last Post: 05-14-2011, 08:23 PM -
What Method and loops to get the distance
By sirdonclemo in forum New To JavaReplies: 2Last Post: 09-05-2010, 03:21 AM -
given number of points(cordinates) , find max points lie on the same line ?
By Hayzam in forum New To JavaReplies: 2Last Post: 08-24-2008, 12:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks