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

(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日

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?