计算三维空间两点距离【马士兵面向对象课后习题1】

class Point{
double x;double y;double z;double s;
Point (double _x,double _y,double _z)
{
x=_x;y=_y;z=_z;
}
void setx(double _x){
x=_x;
}
void sety(double _y){
y=_y;
}
void setz(double _z){
z=_z;
}
void sets(){
s=Math.sqrt( Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2) );
}
void display(){
System.out.println("x="+x+"  "+"y="+y+"  "+"z="+z);
System.out.println("s="+s);
}

double getDistance(Point p)
{
return (x - p.x)*(x - p.x)+(y - p.y)*(y - p.y)+(z - p.z)*(z - p.z);
}
}
public class Test{
public static void main(String [] args)
{
Test test=new Test();
Point p1=new Point(2.0,2.0,2.0);
test.gets(p1);
p1.display();//自己写的,麻烦了

Point p2=new Point(1.0,1.0,1.0);
p2.sets();
p2.display();

Point p3=new Point(4.0,4.0,4.0);
p3.sets();
p3.display();

Point p4=new Point(5.0,5.0,5.0);
System.out.print("p1到p4之间距离的平方="+p1.getDistance(p4));//p1到p4的距离的平方,扩展方法,老师的
}

void gets(Point p){
p.sets();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值