你好,程序如下:
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();
}
}
如果还有其他要求的话,我再改进一下。
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)
直接写的 没编译,意思都有了 看着改改就好