求高手用JAVA帮我设计一个完整的Point类,可以计算两点之间距离和中间点坐标的,万分感谢!

如题所述

你好,程序如下:

public class Point {

double x1,x2,y1,y2;
double d,x3,y3;
Point(double x1,double y1,double x2,double y2){//构造方法
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
void TestPoint(){//求距离和中点坐标
x3 = (x1 + x2)/2;
y3 = (y1 + y2)/2;
d = Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));

System.out.println("(" + x3 + ","+ y3 + ")");
System.out.println(d);
}

public static void main(String[] args) {
Point p = new Point(1,1,2,2);
p.TestPoint();

}

}
如果还有其他要求的话,我再改进一下。追问

Point这个函数改成Point(x,y),里面只有两个参数的,谢谢!

追答

public class Point {

double x,y;
double d;
Point(double a,double b){
x = a;
y = b;
}

void TestPoint(Point p1,Point p2){
x = (p1.x + p2.x)/2;
y = (p1.y + p2.y)/2;
System.out.println("(" + x + ","+ y + ")");
}

void juli(Point p1,Point p2){
d = Math.sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
System.out.println(d);
}
public static void main(String[] args) {

Point p = new Point(0,0);
Point p1 = new Point(1,1);
Point p2 = new Point(2,2);
p.TestPoint(p1, p2);
p.juli(p1, p2);
}

}
修改下。

追问

万分感谢啊兄弟!

温馨提示:内容为网友见解,仅供参考
第1个回答  2012-04-06
public class point{
private double x;
private double y;
point(double x,double y) {this.x = x;this.y = y;}
void center(point a,point b){
double resx,resy;
resx = (a.x+b.x)/2;
resy = (a.y+b.y)/2;
System.out.println(输出结果);
}
void distance(point a ,point b){
double res;
res = Math.sqrt(Math.pow(a.x-b.x,2)+Math.pow(a.y-b.y,2));
System.out.println(结果);
}
}

主函数不写了,就创建两个point对象调用两个方法就行 像这么创建对象:point a = new point(1,2)
直接写的 没编译,意思都有了 看着改改就好
相似回答