# Distance between 2 points

• (2 Pages)
• 1
• 2

## 22 Replies - 59831 Views - Last Post: 29 May 2014 - 12:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=92885&amp;s=361d85eb6cb4024f028fefc4ea1e2a90&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 needHELP!

Reputation: 0
• Posts: 17
• Joined: 07-March 09

# Distance between 2 points

Posted 14 March 2009 - 07:03 PM

hey just having some trouble with a homework question:

For this question you will use the Point class from the Java API, which represents points in
2-dimensional space, each of which has an x and y coordinate.
You must write a program called Distance, which does the following:
1. Reads in the coordinates (separated by spaces) of two points
2. Creates two Point objects with the values entered by the user
3. Uses the distance method of the Point class to calculate the distance between the two
points
4. Prints out the distance

Details on how to create Point classes can be found in the Java API documentation. However,
for this question the only two methods you need to know about it are the following:
 Point(int x, int y) – Constructor; creates a new point
 double distance(Point other) – Calculates the distance between this point and another
point
4

i think what i have so far will work, im jsut having problems creating 2 new objects . it points at new and says incompatible types.
and also points at +distance and says cannot be de refernce from a static context
does everything look right?
thanks

```import java.awt.Point;
import java.util.Scanner;

public class Distance{

private int pointX;
private int pointY;
private double distance;

public Distance(int x,int y)
{
pointX=x;
pointY=y;
}

public int getX()
{
return pointX;
}

public int getY()
{
return pointY;
}

public void distance(double pointX, double pointY)
{
distance=Math.sqrt((pointX)*( pointX) + (pointY)*(pointY));
}

public double getDistance()
{
return distance;
}

public static void main(String[] args){

Scanner console=new Scanner(System.in);

Distance pointp1=new Distance();
System.out.print("Enter point: ");
int x1=console.nextInt();
Distance pointp2=new Distance();
System.out.print("Enter point: ");
int x2=console.nextInt();

System.out.println("Distance between the two points is: "+distance);
}
}

```

This post has been edited by needHELP!: 14 March 2009 - 07:07 PM

Is This A Good Question/Topic? 0

## Replies To: Distance between 2 points

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Distance between 2 points

Posted 14 March 2009 - 07:19 PM

you need to provide a no arg constructor for your class
```import java.awt.Point;
import java.util.Scanner;

public class Distance{

private int pointX;
private int pointY;
private double distance;

public Distance()
{}
public Distance(int x,int y)
{
pointX=x;
pointY=y;
}

public int getX()
{
return pointX;
}

public int getY()
{
return pointY;
}

public void distance(double pointX, double pointY)
{
distance=Math.sqrt((pointX)*( pointX) + (pointY)*(pointY));
}

public double getDistance()
{
return distance;
}

public static void main(String[] args){

Scanner console=new Scanner(System.in);

Distance pointp1=new Distance();
System.out.print("Enter point: ");
int x1=console.nextInt();
Distance pointp2=new Distance();
System.out.print("Enter point: ");
int x2=console.nextInt();

System.out.println("Distance between the two points is: "+distance);
}
}

```

you will still get an error at the printing part in the main though

### #3 needHELP!

Reputation: 0
• Posts: 17
• Joined: 07-March 09

## Re: Distance between 2 points

Posted 14 March 2009 - 07:38 PM

mostyfriedman, on 14 Mar, 2009 - 06:19 PM, said:

you will still get an error at the printing part in the main though

ok thanks
```Distance pointp1=new Distance();
System.out.print("Enter point: "+pointp1.getX()+pointp1.getY());
int x1=console.nextInt();
Distance pointp2=new Distance();
System.out.print("Enter point: "+pointp2.getX()+pointp2.getY());
int x2=console.nextInt();

System.out.println("Distance between the two points is: "+pointp1.getDistance()+pointp2.getDistance());
}
}

```

i added this to get the points st eh user can enter 2 coordinates for both, but it prints out 00 first and wont let me enter 2 points in

This post has been edited by needHELP!: 14 March 2009 - 07:52 PM

### #4 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Distance between 2 points

Posted 14 March 2009 - 08:07 PM

btw, your doing it wrong...for the instance variables, you need 2 Point objects...but you have distance and x and y which is wrong

### #5 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4187
• Posts: 18,394
• Joined: 24-September 08

## Re: Distance between 2 points

Posted 14 March 2009 - 08:30 PM

I think the equation is actually sqrt((x2-x1)^2 + (y2-y1)^2)

http://www.purplemat...es/distform.htm

That might be a problem.

### #6 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Distance between 2 points

Posted 14 March 2009 - 08:37 PM

NeoTifa, on 14 Mar, 2009 - 07:30 PM, said:

I think the equation is actually sqrt((x2-x1)^2 + (y2-y1)^2)

http://www.purplemat...es/distform.htm

That might be a problem.

exactly, this is why he needs 2 Point objects as instance variables

### #7 casingh

Reputation: 3
• Posts: 29
• Joined: 19-January 09

## Re: Distance between 2 points

Posted 14 March 2009 - 09:25 PM

mostyfriedman, on 14 Mar, 2009 - 06:19 PM, said:

you need to provide a no arg constructor for your class
```import java.awt.Point;
import java.util.Scanner;

public class Distance{

private int pointX;
private int pointY;
private double distance;

public Distance()
{}
public Distance(int x,int y)
{
pointX=x;
pointY=y;
}

public int getX()
{
return pointX;
}

public int getY()
{
return pointY;
}

public void distance(double pointX, double pointY)
{
distance=Math.sqrt((pointX)*( pointX) + (pointY)*(pointY));
}

public double getDistance()
{
return distance;
}

public static void main(String[] args){

Scanner console=new Scanner(System.in);

Distance pointp1=new Distance();
System.out.print("Enter point: ");
int x1=console.nextInt();
Distance pointp2=new Distance();
System.out.print("Enter point: ");
int x2=console.nextInt();

System.out.println("Distance between the two points is: "+distance);
}
}

```

you will still get an error at the printing part in the main though

As the last post said, you need to use Point Objects instead of individual variables. Try using the javadoc to find out more about the Point class. Java Documentation

### #8 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4187
• Posts: 18,394
• Joined: 24-September 08

## Re: Distance between 2 points

Posted 14 March 2009 - 09:38 PM

Oh, I didn't read your post. Lol. I was just going off of his code and said missing squares inside the squareroot.

### #9 pbl

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

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

## Re: Distance between 2 points

Posted 15 March 2009 - 10:52 AM

getDistance() should evaluate the actual distance

### #10 pbl

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

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

## Re: Distance between 2 points

Posted 15 March 2009 - 10:58 AM

NeoTifa, on 14 Mar, 2009 - 07:30 PM, said:

I think the equation is actually sqrt((x2-x1)^2 + (y2-y1)^2)

http://www.purplemat...es/distform.htm

That might be a problem.

You are right Tifa: Pythagore therorem
so your single instance of Distance cannot evaluate distance

you'll have to do:

```double getDistance(Distance otherPoint) {
{
int x = pointX - otherPoint.pointX;
x *= x;
int y  = pointY - otherPoint.pointY;
y *= y;
return Math.sqrt(x + y);
}

```

but your problem is that you never evaluate the distance

### #11 needHELP!

Reputation: 0
• Posts: 17
• Joined: 07-March 09

## Re: Distance between 2 points

Posted 16 March 2009 - 03:46 AM

okj thanks
i now have this
```double getDistance(int x, int y)
{
int x = pointX - x.pointX;
x *= x;
int y  = pointY - y.pointY;
y *= y;
return Math.sqrt(x + y);
}

public static void main(String[] args){

Scanner console=new Scanner(System.in);

Point pointp1=new Point();
System.out.print("Enter points: "+pointp1.getX()+""+pointp1.getY());
int point=console.nextInt();
Point pointp2=new Point();
System.out.print("Enter points: "+pointp2.getX()+""+pointp2.getY());
int point1=console.nextInt();

System.out.println("Distance between the two points is: "+pointp1.getDistance()+pointp2.getDistance());
}
}

```

i have made it point objects not distance but when i compile it points at the last print statement and says cannot find symbol when the pointer points ot .getDistance

This post has been edited by needHELP!: 16 March 2009 - 03:54 AM

### #12 shreeharil

Reputation: 0
• Posts: 3
• Joined: 16-March 09

## Re: Distance between 2 points

Posted 16 March 2009 - 05:30 AM

needHELP!, on 16 Mar, 2009 - 02:46 AM, said:

okj thanks
i now have this
```double getDistance(int x, int y)
{
int x = pointX - x.pointX;
x *= x;
int y  = pointY - y.pointY;
y *= y;
return Math.sqrt(x + y);
}

public static void main(String[] args){

Scanner console=new Scanner(System.in);

Point pointp1=new Point();
System.out.print("Enter points: "+pointp1.getX()+""+pointp1.getY());
int point=console.nextInt();
Point pointp2=new Point();
System.out.print("Enter points: "+pointp2.getX()+""+pointp2.getY());
int point1=console.nextInt();

System.out.println("Distance between the two points is: "+pointp1.getDistance()+pointp2.getDistance());
}
}

```

i have made it point objects not distance but when i compile it points at the last print statement and says cannot find symbol when the pointer points ot .getDistance

dats coz u havent givn any arguments to getDistance.

### #13 baavgai

• Dreaming Coder

Reputation: 7154
• Posts: 14,901
• Joined: 16-October 07

## Re: Distance between 2 points

Posted 16 March 2009 - 05:42 AM

You're getting good advice and ignoring it. Begin at the beginning...

You are making this assumption:
```private int pointX;
private int pointY;

```

A lot:
```Point pointp1=new Point();
// it's new, what do you expect to see
System.out.print("Enter points: "+pointp1.getX()+""+pointp1.getY());
// this value goes where, why only one?
int point=console.nextInt();

```

Get your head around this first: a point contains two values, called "coordinates".

1. Reads in the coordinates (separated by spaces) of two points
2. Creates two Point objects with the values entered by the user

We can do this all at once. Using code you've basically written. Consider this:
```Point getPointFromUser(Scanner console) {
System.out.print("Enter point (x y): ");
int x=console.nextInt();
int y=console.nextInt();
return new Point(x,y);
}

```

And the point class has a distance method. To write any more would be to finish the assignment.

Good luck.

### #14 needHELP!

Reputation: 0
• Posts: 17
• Joined: 07-March 09

## Re: Distance between 2 points

Posted 16 March 2009 - 05:32 PM

baavgai, on 16 Mar, 2009 - 04:42 AM, said:

You're getting good advice and ignoring it. Begin at the beginning...

You are making this assumption:
```private int pointX;
private int pointY;

```

A lot:
```Point pointp1=new Point();
// it's new, what do you expect to see
System.out.print("Enter points: "+pointp1.getX()+""+pointp1.getY());
// this value goes where, why only one?
int point=console.nextInt();

```

Get your head around this first: a point contains two values, called "coordinates".

1. Reads in the coordinates (separated by spaces) of two points
2. Creates two Point objects with the values entered by the user

We can do this all at once. Using code you've basically written. Consider this:
```Point getPointFromUser(Scanner console) {
System.out.print("Enter point (x y): ");
int x=console.nextInt();
int y=console.nextInt();
return new Point(x,y);
}

```

And the point class has a distance method. To write any more would be to finish the assignment.

Good luck.

ok thanks for outlining, i got my head around it last nite and FINALLY got this:

```import java.util.Scanner;
import java.awt.Point;

public class Distance{

private int pointX;
private int pointY;

public Distance(int x,int y)
{
pointX=x;
pointY=y;
}

public int getX()
{
return pointX;
}

public int getY()
{
return pointY;
}

public double getDistance(Distance otherPoint)
{
int x = pointX - otherPoint.pointX;
x = x*x;
int y  = pointY - otherPoint.pointY;
y = y*y;
double ddistance = Math.sqrt(x+y);

return ddistance;
}

public static void main(String[] args) {

Scanner console=new Scanner(System.in);

System.out.print("Enter the coordinates for point 1: (x and y) ");
int pointX=console.nextInt();
int pointY = console.nextInt();
Distance pointp1 = new Distance(pointX, pointY);
System.out.print("Enter the coordinates for point 2: (x and y) ");
int pointX2 =console.nextInt();
int pointY2=console.nextInt();
Distance pointp2 = new Distance(pointX2, pointY2);

System.out.println("The Distance between the two points is: " + pointp1.getDistance(pointp2));
}

}

```

Thanks to everyone for their input

This post has been edited by needHELP!: 16 March 2009 - 05:32 PM

### #15 Sfrius

Reputation: 0
• Posts: 4
• Joined: 28-May 14

## Re: Distance between 2 points

Posted 29 May 2014 - 10:23 AM

Hey, If I can make one suggestion...IF you are trying to find the distance between two points its

```Math.sqrt(Math.pow((getX1()-getX2()), 2)+Math.pow((getY1()-getY2()), 2))
```

Your program only obtains 2 numbers from the user... if you are trying to find the distance between two numbers...that would be simple 1 and 4 would be 4-1. I don't think you're trying to do that.
You have to find the hypotenuse of a right degree triangle that the points make and you need x1 y1 x2 and y2.