编写Point类,有两个属性x,y,一个方法distance(Poinr p1,Point p2),计算两者

编写Point类,有两个属性x,y,一个方法distance(Poinr p1,Point p2),计算两者之间的距离

第1个回答  2017-03-18
import java.util.Scanner;

public class Point {

private double x;
private double y;

public Point() {
this(0,0);
}

public Point(double x, double y){
this.x = x;
this.y = y;
}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
System.out.print("Please input x for point 1:");
double x1 = scanner.nextDouble();
System.out.print("Please input y for point 1:");
double y1 = scanner.nextDouble();

System.out.print("Please input x for point 1:");
double x2 = scanner.nextDouble();
System.out.print("Please input y for point 1:");
double y2 = scanner.nextDouble();

double distance = Point.distance(new Point(x1, y1), new Point(x2, y2));
System.out.println("The distance is: " + distance);
}

public static double distance(Point p1, Point p2){
return Math.sqrt(Math.pow(p1.getX() - p2.getX(), 2) + Math.pow(p1.getY()-p2.getY(), 2));
}

public double getX() {
return x;
}

public double getY() {
return y;
}

}
---------------------
Please input x for point 1:0
Please input y for point 1:3
Please input x for point 1:4
Please input y for point 1:0
The distance is: 5.0本回答被网友采纳
相似回答
大家正在搜