JAVA编程定义一个点类。

定义一个二维空间的点类,有横、纵坐标信息,有计算两点之间距离的方法,有将当前点的横、纵坐标移动一定距离到下一个位置的方法。定义一个测试类测试点的这两个方法。

你的第二个方法要求描述不太明白,我就按照自己的理解写了一个。

我偷懒就直接在main方法里面写测试代码了,你需要的话就再自己定义一个Test类写个mian方法,内容其实没什么区别。


public class Point {

private double x;

private double y;

public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}

public double distance(Point point) {
return Math.sqrt((point.x - this.x) * (point.x - this.x) + (point.y - this.y) * (point.y - this.y));
}

public Point move(double x, double y) {
return new Point(this.x + x, this.y + y);
}

@Override
public String toString() {
// TODO Auto-generated method stub
return "(" + x + ", " + y + ")";
}

public static void main(String[] args) {
Point p1 = new Point(5, 6);
Point p2 = new Point(-2, -9);

System.out.println(p1.distance(p2));
System.out.println(p1.move(11, -2));
}

}

测试结果:

16.55294535724685

(16.0, 4.0)

温馨提示:内容为网友见解,仅供参考
第1个回答  2014-04-13
class Point{
      int x;
      int y;
      public Point(int x,int y){
          this.x = x;
          this.y = y;
      }
      
      public void move(){
      //要怎么移动。。。
      }
      public void test(){
      //要测试什么。。。
      }
}

第2个回答  2014-04-13
package cc.icoc.javaxu.threads;

public class Point
{
float x;
float y;

private void moveX(int x)
{
this.x += x;
}

private void moveY(int y)
{
this.y += y;
}

/***计算两点间的距离***/
public int distanceOfTwoPoint(int x1 , int y1 , int x2 , int y2)
{
return ((int)Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)));
}
}本回答被网友采纳
第3个回答  2014-04-13
相似回答