定义Point类,表示二维坐标中的一个点,有属性横 * 坐标x和纵坐标y,还有用来获取和设置坐标值,以 * 及计算到原点距离平方值的方法。定义一个构造方 * 法初始化x和y。在主类中创建
public class point {
double x,y;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public point(double x, double y) {
super();
this.x = x;
this.y = y;
}
double distance(){
return Math.sqrt((x*x+y*y));
}
}
package gh;
public class text4 {
public static void main(String[] args) {
point mypoint1=new point(3.2,4.5);
point mypoint2=new point(5.2,4.1);
System.out.println("X:"+mypoint1.getX()+" Y:"+mypoint1.getY()+" distance:"+mypoint1.distance());
System.out.println("X:"+mypoint2.getX()+" Y:"+mypoint2.getY()+" distance:"+mypoint2.distance());
}
}
