Results 1 to 7 of 7
  1. #1
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default 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:
    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));
    	}
    }
    TestPoint Class:
    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));
    	}
    }
    I know that printout is wrong, but I wouldn't know how to get the two points in there.

    Any suggestions? Thanks in advance.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: Distance Between Two Points

    The getDistance method has four doubles as parameters yet you are passing a single Point when you call it.

  3. #3
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default 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:
    Java Code:
    System.out.println("The distance between p1 and p2 is: " + p1.getDistance(p2));
    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.
    Last edited by Taikei_no_Yuurei; 09-08-2011 at 06:21 AM.

  4. #4
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

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

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default 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?

  6. #6
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default 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).

  7. #7
    Join Date
    Sep 2011
    Posts
    11
    Rep Power
    0

    Default Re: Distance Between Two Points

    Quote Originally Posted by HSKrustofsky View Post
    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.
    No, the way you have things set up using getDistance from p1 and p2 are identical.

Similar Threads

  1. Set distance from border to component
    By Josep_16 in forum AWT / Swing
    Replies: 1
    Last Post: 08-22-2011, 12:21 AM
  2. Replies: 1
    Last Post: 08-11-2011, 10:46 AM
  3. Calculating the distance between two points problem
    By silverglade in forum New To Java
    Replies: 16
    Last Post: 05-14-2011, 08:23 PM
  4. What Method and loops to get the distance
    By sirdonclemo in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 03:21 AM
  5. Replies: 2
    Last Post: 08-24-2008, 12:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •