Reputation: 39
Hi I have to write a program that creates the two points and calculates the distance between them ... I have written the program but not I have to do it with user input .... can someone please tell me where I am going wrong ?
class MyPoint {
private double x;
private double y;
public double getx()
{
return x;
}
public double gety()
{
return y;
}
public MyPoint()
{
}
public MyPoint(double x, double y)
{
this.x = x;
this.y = y;
}
public double distance(MyPoint secondPoint) {
return distance(this, secondPoint);
}
public static double distance(MyPoint p1, MyPoint p2) {
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y)
* (p1.y - p2.y));
}
}
public class MyPointTest
{
public static void main(String[] args)
{
MyPoint p1 = new MyPoint(0,0);
MyPoint p2 = new MyPoint(10, 30.5);
p1.distance(p2);
System.out.println("Distance between two points (0,0) and (10,30.5)= "+MyPoint.distance(p1,p2));
}
}
This is what I have tried with user input
import java.util.Scanner;
public class TestMyPoint {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MyPoint p1 = new MyPoint();
MyPoint p2 = new MyPoint();
System.out.print("Enter a first point " + p1 );
System.out.print("Enter a second point " + p2 );
System.out.println(p1.distance(p2));
System.out.println(MyPoint.distance(p1, p2));
}
}
Upvotes: 0
Views: 976
Reputation: 1382
Scanner is imho a good start. Try something like:
System.out.println("Please enter x of the first point:");
double x1 = input.nextDouble();
System.out.println("Please enter y of the first point:");
double y1 = input.nextDouble();
MyPoint p1 = new MyPoint(x1, y1);
...
Upvotes: 2