㈠ java定义一个点类Point, 具备坐标系中的横坐标x, 和纵坐标y, 并实现如下功能:
Point类
publicclassPoint{
privatefloatx;
privatefloaty;
publicPoint(floatx,floaty){
this.x=x;
this.y=y;
}
publicfloatgetX(){
returnx;
}
publicvoidsetX(floatx){
this.x=x;
}
publicfloatgetY(){
returny;
}
publicvoidsetY(floaty){
this.y=y;
}
publicfloatdistanceToOrigin(){
return(float)Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2));
}
publicfloatdistanceToOther(intx,inty){
return(float)Math.sqrt(Math.pow(this.x-x,2)+Math.pow(this.y-y,2));
}
publicfloatdistanceToOther(Pointpoint){
return(float)Math.sqrt(Math.pow(this.x-point.x,2)+Math.pow(this.y-point.y,2));
}
}
测试类
publicclassTestPoint{
publicstaticvoidmain(String[]args){
Pointp1=newPoint(3,5);
Pointp2=newPoint(7,8);
System.out.println(p1.distanceToOrigin());
System.out.println(p2.distanceToOrigin());
System.out.println(p1.distanceToOther(20,30));
System.out.println(p2.distanceToOther(20,30));
System.out.println(p1.distanceToOther(p2));
}
}