OOP:实现表示三维几何形式的类

时间:2017-01-04 14:21:21

标签: java oop

我需要实现一个能够表示三维几何形式的类 Geom 。该类拥有两个数据字段:

//colour of the form
public String colour;
//coordinates of the form
public double x, y, z;

有两个构造函数:第一个构建器传递参数列表中的所有数据字段,第二个构造函数是“默认构造函数”,它将值 black 分配给属性颜色并将表单放在原点。

需要实施以下方法:

  1. public void move(double dX, double dY, double dZ),更改坐标

  2. public double getVolume(),发出 Geom 的大小。对于任意形式,这只是0.0

  3. public double getArea(),提供表面内容。

  4. public double getDistanteTo(Geom other),给出了 Geom 的位置与欧几里得指标中 Geom other 之间的距离。

  5. public String toString(),在字符串中显示 Geom 的所有数据字段。

  6. 我必须编写一个用作接口的类,它看起来像这样:

    public interface GeomInterface {
    
      public void move(double dX, double dY, double dZ);
    
      public double getVolume();
    
      public double getArea();
    
      public double getDistanceTo(Geom other);
    
      public String toString();
    
    }
    

    下一步是编写Geom类:

    public class Geom implements GeomInterface {
    
    public String colour;
    public double x, y, z;
    

    就我的知识而言。我现在该如何开始实施其中一种方法?有人有例子吗?

2 个答案:

答案 0 :(得分:1)

这是另一种你应该自己做的方法。

public class Geom implements GeomInterface {

   public String colour;
   public double x, y, z;

   //define constructors here

   public double getDistanceTo(Geom other){

       // access local x,y,z as it is
       // access passed x,y,z as other.x,other.y,other.z
       // write some function to get distance between two points and 
       //save it in distance.

       return distance;
   }
}

答案 1 :(得分:1)

这里有一些代码可以从中开始。你必须自己实现真正的逻辑。

<强> GEOM的类

public class Geom implements GeomInterface {
    private double dx;
    private double dy;
    private double dz;
    private String color;

    public Geom() {
        this.dx = 0;
        this.dy = 0;
        this.dz = 0;
        this.color = "Black";
    }

    public Geom(double dx, double dy, double dz, String color) {
        this.dx = dx;
        this.dy = dy;
        this.dz = dz;
        this.color = color;
    }

    @Override
    public void move(double dx, double dy, double dz) {
        // Implement logic here

        // Sample
        this.dx = dx;
    }

    @Override
    public double getVolume() {
        double volume = 0;

        // Implement logic here

        // Sample
        volume = this.dx * this.dy * this.dz;

        return volume;
    }

    @Override
    public double getArea() {
        double area = 0;

        // Implement logic here 

        // Sample
        area = this.dx * this.dy;

        return area;
    }

    @Override
    public double getDistanceTo(Geom other) {
        double distance = 0;

        // Implement logic here

        // Sample
        distance = this.dx - other.dx; 

        return distance;
    }

    public double getDx() {
        return dx;
    }

    public double getDy() {
        return dy;
    }

    public double getDz() {
        return dz;
    }

    public void setDx(double dx) {
        this.dx = dx;
    }

    public void setDy(double dy) {
        this.dy = dy;
    }

    public void setDz(double dz) {
        this.dz = dz;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

<强>测试级

public class TestGeom {
    public TestGeom() {
        Geom geom1 = new Geom();
        Geom geom2 = new Geom(1.0, 2.5, 3.1, "White");

        geom1.setDx(5.3);
        geom1.setDy(1.2);
        geom1.setDz(2.0);
        geom1.setColor("Red");

        System.out.println("Geom1 area: " + geom1.getArea());
        System.out.println("Geom2 volume: " + geom2.getVolume());
        System.out.println("Distance from geom1 to geom2: " + geom1.getDistanceTo(geom2));
    }

    public static void main(String[] args) {
        new TestGeom();
    }
}