There are ofthen times when we're writing a code that the overall logic behind it is correct but some trivial mistakes bring error. I'll now fix them along with you.
So below is a code I wrote to create Cylinder class and use setter and getter in it.
the first common and very obvious mistake I did was writing public int getRadius(), the variables i provided were float. So the return type of function should be float.
Secondly r and h were the parameters I provided to the setter class and not the variables, so in getter class I should return the value of variables and not the parameter, so it should be return rad and return height instead of return r and return h.
To resolve this I could've also used this keyword instead i.e. for setter function:
public void setRadius(float rad){
this.rad = rad;
}
and same for height setter.
Debugged code for Cylinder class:
Main class:
You could've already guessed the error,so these again are very trivial beginner level error that, my the variable rad and height are instances of class Cylinder and not Main, so that could not be accessed directly into main without its method so it the code should be as follows:
Top comments (0)