Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Distance between 2 points

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Distance between 2 points

    So I've been given a practice test for one of my CSC courses that is asking me this:

    Provide the definition for the Java class Point so that it supports
    the three Java statements that appear below. Your Point class may not extend any
    other Java class. Recall that the distance between two points (x1,y1)and (x2,y2)is given by
    sqrt((x1−x2)2+(y1−y2)2)

    Point p1 = new Point(3,4);
    Point p2 = new Point(16,-6);
    double distance = p1.distanceTo(p2);

    //Complete the Point class below
    public class Point {

    My problem is I'm having trouble figuring out how to set up the equation. here's my code so far:

    public class Point {
    	Point p1, p2;
    	int x, y;
     
    	public Point(int a, int b) {
    		this.x = a;
    		this.y = b;
    	}
     
    	public int getX() {
    		return x;
    	}
     
    	public int getY() {
    		return y;
    	}
     
    	public double distanceTo(Point p) {
    		//where my equation would go
    		return p;
    	}
     
    	public static void main(String[] args) {
    		Point p1 = new Point(3,4);
    		Point p2 = new Point(16,-6);
    		double distance = p1.distanceTo(p2);
    		System.out.print(distance); //just to test
    	}
    }

    I was thinking something like this
    p = (p1.getX() - p2.getX())^2 + (p1.getY() - p2.getY())^2;
    but that throws the Type mismatch error (from Point to int) so I can't do it that way

    any suggestions/help would be great. thanks
    Last edited by captain; February 21st, 2012 at 11:54 PM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Distance between 2 points

    Perhaps you could post the actual code and compiler message. The line looks OK, but the devil with compiling lies in the detail.

    -----

    In the class definition x and y should be private because there's no reason for them not to be. And p1 and p2 should not be there at all since there is nothing (obvious) in the state of a point that consists of a pair of points.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Distance between 2 points

    that is my code. the only error I'm getting is a type mismatch where I would have been putting the equation that I posted above. I understand why its giving me that, but I'm not sure what the correct way to set it up is.

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Distance between 2 points

    public class Point {
    	Point p1, p2;
    	int x, y;
     
    	public Point(int a, int b) {
    		this.x = a;
    		this.y = b;
    	}
     
    	public int getX() {
    		return x;
    	}
     
    	public int getY() {
    		return y;
    	}
     
    	public double distanceTo(Point p) {
    		//where my equation would go
    		double lengthA = (double)(this.getX() - p.getX());
    		double lengthB = (double)(this.getY() - p.getY());
    		double distance = 0.0;
     
    		//FINISH FORMULA
     
    		return distance;
    	}
     
    	public static void main(String[] args) {
    		Point p1 = new Point(3,4);
    		Point p2 = new Point(16,-6);
    		double distance = p1.distanceTo(p2);
    		System.out.print(distance); //just to test
    	}
    }
    I changed your distanceTo formula to access points how you looked like you wanted to.
    since you called it with p1, when inside your method "this" refers to object p1.
    since you pass it p2, use p2 by name to access its getter methods to.
    they both have ints, and to keep precision you want to cast/change(?I forget if it is called casting with primitive types) it from into to double.

    note: you will need to use the Math class to fix your formula for distance
    Last edited by JonLane; February 22nd, 2012 at 12:56 AM.

Similar Threads

  1. lengthen distance between JMenuItem title and shortcut(accelerator)
    By KILL3RTACO in forum Java Theory & Questions
    Replies: 1
    Last Post: October 28th, 2011, 07:27 PM
  2. ArrayList Plotting Points
    By basketball8533 in forum Collections and Generics
    Replies: 1
    Last Post: October 18th, 2011, 03:59 PM
  3. I like plotting points :) HI!
    By fractalorbit in forum Member Introductions
    Replies: 1
    Last Post: September 5th, 2011, 10:49 AM
  4. Memoization (dyanamic programming) for edit distance recursion
    By rph12 in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 28th, 2011, 10:37 AM
  5. Getting all points in line
    By Mike in forum Java Theory & Questions
    Replies: 7
    Last Post: September 13th, 2010, 11:59 AM