java定义一个点类Point, 具备坐标系中的横坐标x, ...
发布网友
发布时间:2022-04-22 06:36
我来回答
共1个回答
热心网友
时间:2024-09-03 22:21
Point类
public class Point{
private float x;
private float y;
public Point(float x,float y){
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float distanceToOrigin(){
return (float) Math.sqrt(Math.pow(this.x, 2)+Math.pow(this.y, 2));
}
public float distanceToOther(int x, int y){
return (float) Math.sqrt(Math.pow(this.x-x, 2)+Math.pow(this.y-y, 2));
}
public float distanceToOther(Point point){
return (float) Math.sqrt(Math.pow(this.x-point.x, 2)+Math.pow(this.y-point.y, 2));
}
}
测试类
public class TestPoint{
public static void main(String[] args) {
Point p1 = new Point(3, 5);
Point p2 = new Point(7, 8);
System.out.println(p1.distanceToOrigin());
System.out.println(p2.distanceToOrigin());
System.out.println(p1.distanceToOther(20,30));
System.out.println(p2.distanceToOther(20,30));
System.out.println(p1.distanceToOther(p2));
}
}