# HELP!! Cant get point program to work

Printable View

• July 27th, 2013, 08:23 PM
Lashickk
HELP!! Cant get point program to work
So I am trying to create a program that gives the distances between two points that can be inputted by the user. And here has to be a default constructor to set x and y values to 0. Any help to set me in the right direction would be greatly appricated!
Code Java:

```import java.util.*; import java.math.*;   public class point { point P1=new point(0,0); point P2=new point(0,0); double x; double y;   double getX() { return x; }   double getY() { return y; }     public void distance(double x, double y) { double p=Math.sqrt((P1.getX() - P2.getX()) * (P1.getX() - P2.getX()) + (P1.getY() - P2.getY()) * (P1.getY() - P2.getY()) ); }   public double distance( double p) {   distance(x,y); return p; } }```

anddd the main...

Code Java:

```import java.util.*; import java.math.*; public class Lab07 {   public static void main(String[] args) { point P1=new point(0,0); point P2=new point(0,0); Scanner in = new Scanner(System.in); System.out.println("Enter first x coord"); double x = in.nextDouble(); System.out.println("Enter first y coord"); double y = in.nextDouble(); System.out.println("The distance between the two points is "+ point.distance(p)); }   }```
• July 28th, 2013, 12:51 AM
GregBrannon
Re: HELP!! Cant get point program to work
Are you getting errors? If so, post them. Is the program compiling and running but not working as you'd hoped? Then post sample runs and describe how the run does not meet the requirements.

Some 'points' to consider:

Follow Java naming conventions and capitalize class names. I recommend that you also don't name your classes the same as existing core Java classes.

A constructor is a method without a return type, not even void, that has the same name as the class. The "default" constructor refers to the zero-argument constructor.

A 'point' in a cartesian coordinate system is defined by two parameters, usually referred to as 'x' and 'y'.

Two points require 4 parameters, (x1, y1) and (x2, y2).

The distance between 2 points ( x1, y1 ) and ( x2, y2 ) = Sqrt ( ( x2 - x1 )^2 + ( y2 - y1 ) ^ 2 ).

The method distance() does not belong in a point class. A point has no dimensions.

Let us know what you need help with.
• July 28th, 2013, 03:16 AM
syedbhai
Re: HELP!! Cant get point program to work
Hello.
This shall be your point class.

**code removed**
• July 28th, 2013, 05:27 AM
GregBrannon
Re: HELP!! Cant get point program to work
If the point class must have a 'distance' method, then ifs signature should be:
Code :

```// this method returns the distance from point1 to point2 by calling point1.distanceTo( point2 ) public double distanceTo( Point point2 ) { }```
• July 28th, 2013, 08:23 AM
jps
Re: HELP!! Cant get point program to work
@syedbhai
Please see: The problem with spoonfeeding
• July 29th, 2013, 08:21 AM
Lashickk
Re: HELP!! Cant get point program to work
Ok so I fixed most of the errors , the only problem now is that it cant read "p1 or p2" form my main.

---"p1 cannot be resolved, p2 cannot be resolved. in distance(double, double)
Code Java:

```import java.util.*; import java.math.*;   public class point { double x; double y;   public point() { double x=0; double y=0; } double getX() { return x; }   double getY() { return y; }     public double distance(double x, double y) { double p=Math.sqrt((P1.getX() - P2.getX()) * (P1.getX() - P2.getX()) + (P1.getY() - P2.getY()) * (P1.getY() - P2.getY()) );   }   public double distance(double p) {   distance(p); return p; }       }```
and..
Code Java:

```import java.util.*; import java.math.*; public class Lab07 {   public static void main(String[] args) {   point P1=new point(); point P2=new point(); Scanner in = new Scanner(System.in); System.out.println("Enter first x coord"); double x = in.nextDouble(); System.out.println("Enter first y coord"); double y = in.nextDouble(); System.out.println("The distance between the two points is "+ point.distance()); }   }```

--- Update ---

Also how can i get this to input two cords without creating two new x and y variables?
• July 29th, 2013, 02:25 PM
syedbhai
Re: HELP!! Cant get point program to work
Hello.
This shall be your code.
...

Thanks,
Syed.
• July 29th, 2013, 03:43 PM
copeg
Re: HELP!! Cant get point program to work
@syedbhai, this is the second time I've had to edit your post in a single thread - consider this a warning, and read post #5. Continue to disregard moderator edits and warnings and we will have no choice but to take further action.
• July 29th, 2013, 08:50 PM
Lashickk
Re: HELP!! Cant get point program to work
Ok guys finally got it down to this last bit, all i need is for my Point class to read the p1 and p2, it comes up with "cant be resolved. Any fixes?

Code Java:

```import java.util.*; import java.math.*;   public class Point { double x0; double y0;   public Point() { double x=0; double y=0; } public Point(double x, double y) { x0=x; y0=y; } double getX() { return x0; } double getY() { return y0; } public static double distance() { double p=Math.sqrt((P1.getX() - P2.getX()) * (P1.getX() - P2.getX()) + (P1.getY() - P2.getY()) * (P1.getY() - P2.getY()) ); return p; }   }```
andddd.....
Code Java:

```import java.util.*; import java.math.*;   public class Lab07 {   public static void main(String[] args) {   Scanner in = new Scanner(System.in); System.out.println("Enter first x coord"); double x = in.nextDouble(); System.out.println("Enter first y coord"); double y = in.nextDouble(); Point P1 = new Point(x,y); System.out.println("Enter second x coord"); x = in.nextDouble(); System.out.println("Enter second y coord"); y = in.nextDouble(); Point P2=new Point(x,y); System.out.println("The distance between the two points is " + Point.distance()); }   }```
• July 30th, 2013, 12:35 AM
syedbhai
Re: HELP!! Cant get point program to work
Sir,
I am sorry. I apoligize. Please forgive me.
I will do my best to avoid spoonfeeding in the future.

Thanks,
Syed.
• July 30th, 2013, 01:26 AM
GregBrannon
Re: HELP!! Cant get point program to work
Quote:

. . . all i need is for my Point class to read the p1 and p2, it comes up with "cant be resolved.
I explained how this should be done in post #4. You can name the method what you like; I don't have to like it. Reread Post #4 and let me know what you don't understand or can't code to make that work.