moontemplar212
moontemplar212

Reputation: 53

Why are my constructor arguments not passed?

Here is my class. In the main method at the bottom I pass two arguments in the constructor (int numTriangles, double radius) as (8,1). The variables declared at the top radius and numTriangles should assume those values as my constructor assigns them then runs a calculation. However I get divide by zero error when twoTheta is calculated as numTriangles is zero at this point. Why is this and how can I fix it? Thanks.

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);
    }   
}

Upvotes: 0

Views: 78

Answers (6)

pavithraCS
pavithraCS

Reputation: 691

You can change your code like this to avoid the error.

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

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

Upvotes: 0

Przemysław Moskal
Przemysław Moskal

Reputation: 3609

You call runCalculation() from the constructor - at this moment twoTheta has assigned it's default value (it's 0 for primitives and that default value is assigned even before constructor is called). There are two simple ways of solving your problem:

  1. You should call runCalculation() from outside of the constructor to be sure that all fields are initialized to values other than default (to values that you specified)

    OR

  2. You could initialize twoTheta and thetain the constructor before calling runCalculation().


If you want to use first option - change your main method like this to see results that you expect:

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

You should also delete call of runCalculation() from the body of the constructor.


If you choose second way of solving your problem, just initialize theta and twoTheta before runCalculation() in the constructor body:

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

You might want to look here at official Java tutorial to see a list of default values assigned to primitive types and to read a bit more about it (by the way, objects are assigned to null by default).

Upvotes: 0

Arjun Kay
Arjun Kay

Reputation: 328

As multiple answers have pointed out, twoTheta (and theta as well) is initialized before constructor is called, hence the error.

I suggest you initialize twoTheta, and theta inside the constructor.

 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();
}

Upvotes: 2

Adrien H
Adrien H

Reputation: 793

Because these definitions are executed at the creation of the class and not at the instantiation of the object. Furthermore, when you declare but don't assign radius or numTriangles, the default value 0 is used. The default value is 0 for any primitive type (double, int, float, ...) and null for any other type (String, Triangle, ...).

You should only declare them in the class and assign them in the constructor.

double radius;
int numTriangles;
double twoTheta;

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

Upvotes: 0

Shanu Gupta
Shanu Gupta

Reputation: 3807

Call runCalculation();

after

Triangles triangles = new Triangles(8, 1);

in your main method.

Let the constructor complete.

Upvotes: 0

Robert Kock
Robert Kock

Reputation: 6008

Ths class attributes are initialized before the constructor is called.
So, the attribute numTriangles is first set to 0. After that double twoTheta = 360/numTriangles; is executed.
The constructor is called afterwards.
That's why you get your error.

Therefore, don't initialize the attributes twoTheta and theta directly but let the constructor handle it after having set the numTriangles and radius attributes.

Upvotes: 0

Related Questions