I'm having trouble getting the value of an operation. All I get as a result is 0. I've tried many things but nothing seems to work. I'm a beginner so I'm sorry if this is something really simple. I looked around for similar problems but I couldn't seem to find one that matched my issue
public class Circle {
double rad, C, D, A, A2, V;
public double getRad(){
return rad;
}
void setRad(double valueRad){
this.rad = valueRad;
}
public double getCircumference(){
return C;
}
void setCircumference(double Circ){
Circ = (rad*2)*(Math.PI);
this.C = Circ;
}
}
And this is my main:
public class Main {
public static void main(String[] args) {
Circle res = new Circle();
res.setRad(5.80);
System.out.println("The circumference is: "+res.getCircumference()+" cm");
}
}
Your calculation code is in the setCircumference()
method which is never called. When you call the getCircumference()
method the default C
field value of 0.0
is returned.
One way to fix it would be to calculate the value in the getCircumference()
method:
public class Circle {
private double rad;
public void setRad(double rad) {
this.rad = rad;
}
public double getCircumference() {
return rad * 2 * Math.PI;
}
}