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);
}
}
(3)编写一个测试类PointDemo,在该类中创建两个Point类的实例对象,即坐标点(3,3)和(4,5),并计算这两个点间的距离。
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
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); } }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报