为什么我的构造函数参数没有传递?

时间:2018-04-20 06:42:04

标签: java variables constructor zero

这是我的班级。在底部的main方法中,我将构造函数中的两个参数(int numTriangles,double radius)传递为(8,1)。在顶部半径和numTriangles声明的变量应该假定这些值,因为我的构造函数指定它们然后运行计算。然而,当计算twoTheta时,我得到除以零误差,因为此时numTriangles为零。为什么这样,我该如何解决?谢谢。

package Triangles;

public class Triangles {

    double radius;
    int numTriangles;
    double twoTheta = 360/numTriangles;
    double theta = twoTheta / 2;
    double base;
    double height;
    double result;
    double halfTriangleArea;
    double triangleArea;
    double area;

    public Triangles(int numTriangles, double radius) {
        this.numTriangles = numTriangles;
        this.radius = radius;
        runCalculation();
    }

    // My methods

    public double calculateBase() { // SOH
        double thetaToRadians = Math.toRadians(theta);
        double base = Math.sin(thetaToRadians) / radius;
        return base;
    }

    public double calculateHeight() { // CAH
        double thetaToRadians = Math.toRadians(theta);
        double height = Math.cos(thetaToRadians) / radius;
        return height;
    }

    public double checkPythag(double base, double height) {
        double a = base;
        double b = height;
        double result = Math.sqrt(a*a + b*b);
        return result;
    }

    public double calculateArea(double base, double height) {
        double halfTriangleArea = (0.5) * base * height;
        return halfTriangleArea;
    }

    public double runCalculation() {
        base = calculateBase();
        height = calculateHeight();
        result = checkPythag(base, height);
        halfTriangleArea = calculateArea(base, height); 
        triangleArea = 2 * halfTriangleArea;
        // C = Pi * D = Pi * 2 * r
        // A = Pi * r.^2
        area = numTriangles * triangleArea;
        // Substitute Pi for X
        // area = X * r.^2
        // if r is 1
        // area = X
        return area;
    }

    // Runnable

    public static void main(String[] args) { // create an instance of class to run in main
        Triangles triangles = new Triangles(8, 1);
        System.out.println("radius: " + triangles.radius);
        System.out.println("numTriangles: " + triangles.numTriangles);
        System.out.println("twoTheta " + triangles.twoTheta);
        System.out.println("theta " + triangles.theta);
        System.out.println("base: " + triangles.base);
        System.out.println("height: " + triangles.height);
        System.out.println("checkPythag " + triangles.result + " | " + triangles.radius);
        System.out.println("halfTriangleArea: " + triangles.halfTriangleArea);
        System.out.println("triangleArea: " + triangles.triangleArea);
        System.out.println("Approximation of Pi by triangles: " + triangles.area);
    }   
}

6 个答案:

答案 0 :(得分:2)

正如多个答案所指出的,twoTheta(以及theta)在构造函数被调用之前被初始化,因此错误。

我建议您在构造函数中初始化twoThetatheta

 public Triangles(int numTriangles, double radius) {
    this.numTriangles = numTriangles;
    this.radius = radius;
    //Initialize twoTheta
    this.twoTheta = 360/this.numTriangles;
    this.theta = this.twoTheta/2;
    runCalculation();
}

答案 1 :(得分:0)

在调用构造函数之前初始化类属性 因此,属性numTriangles首先设置为0.执行double twoTheta = 360/numTriangles;之后 之后调用构造函数 这就是你得到错误的原因。

因此,请勿直接初始化属性twoThetatheta,但在设置numTrianglesradius属性后让构造函数处理它。

答案 2 :(得分:0)

致电runCalculation();

之后

Triangles triangles = new Triangles(8, 1);

main方法中。

让构造函数完成。

答案 3 :(得分:0)

您可以像这样更改代码以避免错误。

public Triangles(int numTriangles, double radius) {
        this.numTriangles = numTriangles;
        this.radius = radius;           
        initialize();
        runCalculation();
    }

    private void initialize()
    {
      twoTheta = 360/numTriangles;
      theta = twoTheta / 2;      
    }

答案 4 :(得分:0)

您从构造函数中调用runCalculation() - 此时twoTheta已为其分配了默认值(对于基元,它为0,并且即使在调用构造函数之前也会分配默认值)。有两种简单的方法可以解决您的问题:

  1. 您应该从构造函数外部调用runCalculation(),以确保所有字段都初始化为默认值以外的值(指定的值)

  2. 您可以在调用twoTheta之前在构造函数中初始化thetarunCalculation()

  3. 如果您想使用第一个选项 - 请更改此类main方法以查看您期望的结果:

    public static void main(String[] args) {
        Triangles triangles = new Triangles(8, 1);
        triangles.runCalculation();
        ...
    }
    

    您还应该从构造函数的主体中删除runCalculation()的调用。

    如果您选择解决问题的第二种方法,只需在构造函数体中theta之前初始化twoThetarunCalculation()

    public Triangles(int numTriangles, double radius) {
        this.numTriangles = numTriangles;
        this.radius = radius;
        this.twoTheta = 360/this.numTriangles;
        this.theta = this.twoTheta/2;
        runCalculation();
    } 
    

    您可能希望在官方Java教程中查看here以查看分配给基本类型的默认值列表,并阅读更多相关信息(顺便说一下,将对象分配给null默认情况下)。

答案 5 :(得分:0)

因为这些定义是在创建类而不是实例化对象时执行的。此外,当您声明但不指定radius或numTriangles时,将使用默认值0 。对于任何基本类型(double,int,float,...),默认值为0;对于任何其他类型(String,Triangle,...),默认值为null。

您应该只在类中声明它们并在构造函数中指定它们。

double radius;
int numTriangles;
double twoTheta;

public Triangle(double radius, int numTriangles) {
    this.radius = radius;
    this.numTriangles = numTriangles;
    this.twoTheta = 360 / numTriangles;
}