分别编写两个类point2d,point3d来表示二维空间和三维空间的点,使之满足下列要求:
时间: 2023-04-27 19:02:49 浏览: 107
1. point2d类和point3d类都应该包含x和y坐标属性。
2. point3d类应该还包含z坐标属性。
3. point2d类和point3d类都应该包含计算两点之间距离的方法。
4. point3d类应该还包含计算两点之间距离的方法。
5. point2d类和point3d类都应该包含输出点坐标的方法。
相关问题
用Java分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点,使之满足以下要求
1.类的定义
Point2D类:
```
public class Point2D {
private double x;
private double y;
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
```
Point3D类:
```
public class Point3D {
private double x;
private double y;
private double z;
public Point3D(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setZ(double z) {
this.z = z;
}
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
}
}
```
2.测试代码
Point2D测试代码:
```
public class Point2DTest {
public static void main(String[] args) {
Point2D p = new Point2D(3.0, 4.0);
System.out.println(p); // 输出 (3.0, 4.0)
p.setX(5.0);
System.out.println(p); // 输出 (5.0, 4.0)
}
}
```
Point3D测试代码:
```
public class Point3DTest {
public static void main(String[] args) {
Point3D p = new Point3D(3.0, 4.0, 5.0);
System.out.println(p); // 输出 (3.0, 4.0, 5.0)
p.setX(6.0);
System.out.println(p); // 输出 (6.0, 4.0, 5.0)
}
}
```
以上代码分别实现了二维空间和三维空间的点的表示,并且满足了题目要求。
JAVA语言编写两个类point2d和point3d来表示二维点和三维空间点
好的,我可以帮你编写JAVA语言的point2d和point3d类来表示二维点和三维空间点。
point2d类的代码如下:
```
public class point2d {
private double x;
private double y;
public point2d(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
```
point3d类的代码如下:
```
public class point3d {
private double x;
private double y;
private double z;
public point3d(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
}
```
这两个类分别表示二维点和三维空间点,其中包含了坐标的信息和对应的getter和setter方法。你可以根据需要在这两个类的基础上进行扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)