分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点,使之满足下列要求

分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点,使之满足下列要求

原题

分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点,使之满足下列要求:

  1. Point2D有两个整型成员变量x, y (分别为二维空间的X,Y方向坐标),Point2D的构造方法要实现对其成员变量x, y的初始化。
  2. Point2D有一个void型成员方法offset(int a, int b),它可以实现Point2D的平移。
  3. Point3D是Point2D的直接子类,它有有三个整型成员变量x,y,z (分别为三维空间的X,Y,Z方向坐标),Point3D有两个构造方法:Point3D(int x, int y, int z)和Point3D(Point2D p, int z),两者均可实现对Point3D的成员变量x, y, z的初始化。
  4. Point3D有一个void型成员方法offset(int a, int b, int c),该方法可以实现Point3D的平移。
  5. 在Point3D中的主函数main()中实例化两个Point2D的对象p2d1,p2d2,打印出它们之间的距离,再实例化两个Point2D的对象p3d1,p3d2,打印出他们之间的距离。

分析

这个题是对java中继承相关知识的应用,将其完整的写下来有助于增强我们对代码的熟练度。
具体要求题目计较明确,就是写两个类,然后再在主函数中运行,题目要求是在Point3D中的主函数main()中实例化相关对象,这里我想单独写一个主函数,感觉整体看起来会舒服一点,当然,你完全可以将单独出来的合并到一起。

代码

package Point;

public class Point2D {
    protected int x;
    protected int y;

    public Point2D() {
    }

    public Point2D(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void offset(int a, int b) {
        x += a;
        y += b;
    }

}

package Point;

public class Point3D extends Point2D {
    private int z;

    Point3D() {
    }

    Point3D(int x, int y, int z) {
        super(x, y);
        this.z = z;
    }

    public void setZ(int z) {
        this.z = z;
    }

    public int getZ() {
        return z;
    }

    Point3D(Point2D p, int z) {
        super(p.getX(), p.getY());
        this.z = z;
    }

    public void offset(int a, int b, int c) {
        super.offset(a, b);
        z = z + c;
    }


}

package Point;

import java.math.*;

public class PointTest {
    public static void main(String[] args) {
        Point2D p2d1 = new Point2D(0, 0);
        Point2D p2d2 = new Point2D(1, 1);
        double length2D = distance2D(p2d1, p2d2);
        System.out.println("两点之间的距离是:" + length2D);
        Point3D p3d1 = new Point3D(0, 0, 0);
        Point3D p3d2 = new Point3D(1, 1, 1);
        double length3D = distance3D(p3d1, p3d2);
        System.out.println("三点之间的距离是:" + length3D);
    }

    public static double distance2D(Point2D p2d1, Point2D p2d2) {
        double distance;
        double x;
        double y;
        x = Math.pow((p2d1.getX() - p2d2.getX()), 2);
        y = Math.pow((p2d1.getY() - p2d2.getY()), 2);
        distance = Math.sqrt(y + x);
        return distance;
    }

    public static double distance3D(Point3D p3d1, Point3D p3d2) {
        double distance;
        double x;
        double y;
        double z;
        x = Math.pow((p3d1.getX() - p3d2.getX()), 2);
        y = Math.pow((p3d1.getY() - p3d2.getY()), 2);
        z = Math.pow((p3d1.getZ() - p3d2.getZ()), 2);
        distance = Math.sqrt(y + x + z);
        return distance;


    }

}

在这里插入图片描述

  • 17
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值