2(vii)
John was asked to write a Java code to calculate the surface area of a cone. The following code was written by him: Surface area of cone is A = πrl
l = $\sqrt{r^2 + h^2}$
class area{
double area(double r, double h){
double l, a;
a = 22.0 / 7 * r * l;
l = Math.sqrt(r * r + h * h);
return a;
}
}
Specify the type of the error in the above program, correct and write the program to be error free.
Show SolutionHide Solution↓
The compile-time error occurs because the variable l is used before it is initialized.
The corrected code is as follows:
class area{
double area(double r, double h){
double l, a;
l = Math.sqrt(r * r + h * h);
a = 22.0 / 7 * r * l;
return a;
}
}