分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点,用java写

 我来答
匿名用户
2013-06-30
展开全部
你看看吧。我也是今天写jaca作业的时候写的,不知道行不行。searchpcc
自己修改一下:

/*
* Project: 空间点
* Author: searchpcc(xixi,11级,20112866)
* File: TuXing.java
* Instruction: 主程序
* Time: 2012-11-07
*/
class Point2D{
int x;
int y;
public Point2D(){

}
public Point2D(int x, int y){
this.x = x;
this.y = y;
}
public void offset(int a, int b){
x = a;
y = b;
this.x = this.x + 1;
this.y = this.y + 1;
System.out.print(x);
System.out.print(y);
}
double distan(Point2D p1, Point2D p2){
return(Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
}
}
class Point3D extends Point2D{
int z;
public Point3D(int x, int y, int z){
super(x, y);
this.z = z;
}
public Point3D(){

}
public Point3D(Point2D p, int z){
super(p.x, p.y);
this.z = z;
}
public void offset(int a, int b,int c){
x = a;
y = b;
z = c;
this.x = this.x + 1;
this.y = this.y + 1;
this.z = this.z + 1;
}
double distan(Point3D p1, Point3D p2){
return(Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z)));
}

}
public class KongJian1{
public static void main(String[] args){
Point2D p1 = new Point2D(3,3);
p1.offset(3, 3);
Point2D p2 = new Point2D(2,2);
Point3D p3 = new Point3D(4,4,4);
p3.offset(4,2,3);//可以进行移动
Point3D p4 = new Point3D(5,5,5);
System.out.println("p1和p2之间的距离为:");
System.out.println(new Point2D().distan(p1,p2));
System.out.println("p3和p4间的距离为:");
System.out.println(new Point3D().distan(p3,p4));

}
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式