m0_56149972 2021-10-13 10:24 采纳率: 90%
浏览 57
已结题

(3)编写一个测试类PointDemo,在该类中创建两个Point类的实例对象,即坐标点(3,3)和(4,5),并计算这两个点间的距离。

public class Point
{
public int x;
public int y;
public Point(int x,int y)
{
this.x=x;
this.y=y;
}
public Point(int x)
{
this(x,x);
}
public double distance()
{
return this.distance(0,0);
}
public double distance(int x,int y)
{
return Math.sqrt((this.x-x)(this.x-x)+(this.y-y)(this.y-y));
}
public double distance(Point p)
{
return this.distance(p.x,p.y);
}
}

  • 写回答

1条回答 默认 最新

  • qfl_sdu 2021-10-13 10:53
    关注

    Point类:

    
    public class Point {
        private double xx;
        private double yy;
        
        public Point(){xx = 0;yy=0;}
        public Point(double x,double y){xx =x;yy = y;}
        public double getXx() {
            return xx;
        }
        public void setXx(double xx) {
            this.xx = xx;
        }
        public double getYy() {
            return yy;
        }
        public void setYy(double yy) {
            this.yy = yy;
        }
        
    }
    
    
    

    PointDemo类:

    
    public class PointDemo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Point p1 = new Point(3,3);
            Point p2 = new Point(4,5);
            double dis = Math.sqrt((p1.getXx() - p2.getXx())*(p1.getXx() - p2.getXx()) + (p1.getYy()-p2.getYy())*(p1.getYy()-p2.getYy()));
            System.out.println("两点间的距离=" + dis);
        }
    
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月7日
  • 已采纳回答 10月30日
  • 创建了问题 10月13日