Computer Applications

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 = 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.

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.

Input in Java

13 Likes

Answer

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

Answered By

7 Likes


Related Questions