编写一个坐标系中的“点”类point

img


/** 纯手工代码可能有些许问题,注意!**/
public class Point{
    priate int x;
    priate int y;
    public Point(int x, int y){
        this.x = x;
        this.y = y;
    }
    public Point(int x){
        this.x = x;
        this.y = 0;
    }
    /** 计算和原点的距离**/
    public double distance(){
        if(x = 0){
            return Math.abs(((Integer) y).doubleValue());
        }
        if(y = 0){
            return Math.abs(((Integer) x).doubleValue());
        }
        
        return Math.sqrt(x*x + y*y);
    }
    /** 计算和指定点的距离**/
    public double distance(Point p){
        double x2 = (p.x-x)*(p.x-x);
        double y2 = (p.y-y)*(p.y-y);
        
        return Math.sqrt(x2 + y2);
    }
    
    public static void main(String[] args){
        Point p1 = new Point(4,3);
        system.out.println("p1距离原点的距离:"+ p1.distance());
        
        Point p2 = new Point(6,9);
        system.out.println("p1距离p2的距离:"+ p1.distance(p2));
    }
}