Distance between 2 points

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 18601 Views - Last Post: 29 May 2014 - 12:51 PM Rate Topic: -----

#1 needHELP!  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 needHELP!  Icon User is offline

  • New D.I.C Head

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

Re: Distance between 2 points

Posted 14 March 2009 - 07:38 PM

View Postmostyfriedman, 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

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2582
  • View blog
  • Posts: 15,605
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Distance between 2 points

Posted 14 March 2009 - 08:37 PM

View PostNeoTifa, 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
Was This Post Helpful? 0
  • +
  • -

#7 casingh  Icon User is offline

  • New D.I.C Head

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

Re: Distance between 2 points

Posted 14 March 2009 - 09:25 PM

View Postmostyfriedman, 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
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2582
  • View blog
  • Posts: 15,605
  • 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. :P
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Distance between 2 points

Posted 15 March 2009 - 10:52 AM

getDistance() should evaluate the actual distance
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Distance between 2 points

Posted 15 March 2009 - 10:58 AM

View PostNeoTifa, 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
Was This Post Helpful? 1
  • +
  • -

#11 needHELP!  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#12 shreeharil  Icon User is offline

  • New D.I.C Head

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

Re: Distance between 2 points

Posted 16 March 2009 - 05:30 AM

View PostneedHELP!, 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.
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5774
  • View blog
  • Posts: 12,587
  • 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".

Let's look at your assignment:
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.
Was This Post Helpful? 1
  • +
  • -

#14 needHELP!  Icon User is offline

  • New D.I.C Head

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

Re: Distance between 2 points

Posted 16 March 2009 - 05:32 PM

View Postbaavgai, 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".

Let's look at your assignment:
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

Was This Post Helpful? 0
  • +
  • -

#15 Sfrius  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2