java练习题

设计一个坐标类,能提供以下方法如求当前坐标和其他坐标之间的距离等方

public class XYdistance{
 private int x;
 private int y;
 XYdistance()
   {setX(0);setY(0);}
 public void setX(int x)
   {this.x=x;}
 public int getX()
   {return x;}
 public void setY(int y)
   {this.y=y;}
 public int getY()
   {return y;}
 public static void main(String[] args){
  XYdistance m_1=new XYdistance();
  m_1.setX(10);
  m_1.setY(10);
  XYdistance m_2=new XYdistance();
  double distance=(m_1.getX()-m_2.getX())*(m_1.getX()-m_2.getX())+(m_1.getY()-m_2.getY())*(m_1.getY()-m_2.getY()); 
  double result=Math.sqrt(distance); 
  System.out.println(result);
}
}

法,要求所有变量为私有变量,并提供两个构造函数。

原文地址:https://www.cnblogs.com/ljs-666/p/7732944.html