user2708074
user2708074

Reputation:

Value not printing(java)?

I have the following code executed and when I try to print it out no value besides 0.0 prints:

public class RegularPolygon {
 private int numSides; //# sides
 private double sideLength; //side length 
 private double inscribedRadius; //radius of inscribed circle

 private void calcr(){
    inscribedRadius = .5 * sideLength * 1/Math.tan(Math.PI / numSides);
  }
}

public double getr(){
   return inscribedRadius;
}

Main:

RegularPolygon poly = new RegularPolygon(4, 10);
System.out.println(poly.getr());

Output:

0.0

Constructor:

    public RegularPolygon(int newNumSides, double newSideLength){
    numSides = newNumSides;
    sideLength = newSideLength;
}

What is preventing it from printing out?

Upvotes: 0

Views: 108

Answers (7)

code_fish
code_fish

Reputation: 3434

Have you added constructor to your class?

If not add

public RegularPolygon(int numSides, double sideLength){
this.numSides=numSides;
this.sideLength=sideLength;

}

and then call

poly.calcr();

before printing the value.

You are getting 0.0 because you havn't initalized varalble and also you havn't called method to calculate inscribedRadius.

If you havn't initialized any int or double variable, the default initilization will be 0. Thats why you getting 0.0.

Upvotes: 0

gjman2
gjman2

Reputation: 920

Try add this in your RegularPolygon class

public RegularPolygon(int numSides, double sideLength){
this.numSides=numSides;
this.sideLength=sideLength;

}

Then call the calcr();

Upvotes: 0

Sharif Chowdhury
Sharif Chowdhury

Reputation: 256

// Just edit your getr() function. You have to call calcr() function 

public double getr(){
    calcr();
    return inscribedRadius;

}

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279940

Since your constructor doesn't initialize inscribedRadius and you haven't called calcr(), the value of inscribedRadius remains 0, which is the default value assigned to primitive fields during instance initialization.

RegularPolygon poly = new RegularPolygon(4, 10);
poly.calcr();
System.out.println(poly.getr());

This is only possible if the above code is executed in your RegularPolygon class. Because your method is private it is not visible outside that class. In that case, you would need to setup your design so as to call calcr() internally.

Upvotes: 1

Jon Newmuis
Jon Newmuis

Reputation: 26492

You will need to add a constructor to set the values of the fields you've defined, then invoke your calcr() function in it, to calculate the value for inscribedRadius. Also, getr() should probably be moved inside the RegularPolygon class.

For example:

public class RegularPolygon {
  private int numSides; //# sides
  private double sideLength; //side length 
  private double inscribedRadius; //radius of inscribed circle

  public RegularPolygon(int numSides, double sideLength) {
    this.numSides = numSides;
    this.sideLength = sideLength;
    this.calcr();
  }

  private void calcr(){
    inscribedRadius = .5 * sideLength * 1/Math.tan(Math.PI / numSides);
  }

  public double getr(){
    return inscribedRadius;
  }
}

See http://ideone.com/lxcmlH for a running example of this code.

Upvotes: 0

Karl
Karl

Reputation: 16

When class RegularPolygon is been instanced, because you don't give a initialized value to field inscribedRadius, its field inscribedRadius will be initialized as 0.0. Before you call poly.getr(), you don't do anything, so printing out can only be 0.0.

Upvotes: 0

Java Devil
Java Devil

Reputation: 10959

Modify your constructor to calculate the radius

public RegularPolygon(int newNumSides, double newSideLength){
     numSides = newNumSides;
     sideLength = newSideLength;
     this.calcr();
}

Upvotes: 0

Related Questions