设计平面直角坐标系下的Point类:

定义相应的字段和方法,具体字段和方法见下表。要求:在主方法中创建两个Point类的对象,调用各方法完成设置两点坐标、计算两点间距离、屏幕打印点坐标信息等功能。 

public class Point {
    private int x;
    private int y;
    
    public Point() {
    }
    
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public int getX() {
        return x;
    }
    
    public void setX(int x) {
        this.x = x;
    }
    
    public int getY() {
        return y;
    }
    
    public void setY(int y) {
        this.y = y;
    }
    
    public double getDistance(Point another) {
        int xDiff = this.x - another.x;
        int yDiff = this.y - another.y;
        return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
    }
    
    public void print() {
        System.out.println("(" + x + "," + y + ")");
    }
}

主程序

public static void main(String[] args) {
    Point p1 = new Point(3, 4);
    Point p2 = new Point(5, 6);
    
    System.out.println("点1的坐标为:");
    p1.print();
    System.out.println("点2的坐标为:");
    p2.print();
    
    double distance = p1.getDistance(p2);
    System.out.println("两点间距离为:" + distance);
}

  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/188725
  • 除此之外, 这篇博客: JAVA|定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)中的 定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 要求如下:
    (1)可以生成具有特定坐标的点对象。
    (2)提供可以设置三个坐标的方法。
    (3)提供可以计算该“点”距原点距离平方的方法。
    (4)编写主类程序验证。

    Point.java👇

    package work.seventeen;
    
    public class Point {
        private double x,y,z;
        public Point(double x, double y, double z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        public void setX(double x) {
            this.x = x;
        }
    
        public void setY(double y) {
            this.y = y;
        }
    
        public void setZ(double z) {
            this.z = z;
        }
        public double getX() {
            return x;
        }
    
        public double getY() {
            return y;
        }
    
        public double getZ() {
            return z;
        }
        public double distance(){
            return  Math.sqrt(x*x+y*y+z*z);
        }
    
        @Override
        public String toString() {
            return "当前坐标为 ("+x+","+y+","+z+")";
        }
    }
    
    

    PointTest.java👇

    package work.seventeen;
    
    public class PointTest {
        public static void main(String[] args) {
            Point point = new Point(1,2,3);
            System.out.println(point);
            point.setX(3);
            point.setY(4);
            point.setZ(5);
            System.out.println(point);
            System.out.println("到原点的距离为:"+point.distance());
        }
    }