作业帮 > 综合 > 作业

java编程:编Point类,有两属性x,y,一个方法distance(Point p1,Point p2),算两点距离

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/17 06:19:27
java编程:编Point类,有两属性x,y,一个方法distance(Point p1,Point p2),算两点距离,我用内部类做不
最好是用手动输入坐标点的
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