Home Java Programming Language / Programs / Design a class to overload a function volume() as follows:
🚀 Programming Example

Design a class to overload a function volume() as follows:

👁 109 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.
No previous program
No next program

📌 Information & Algorithm

Design a class to overload a function volume() as follows:

  1. double volume (double R) – with radius (R) as an argument, returns the volume of sphere using the formula.
    V = 4/3 x 22/7 x R3

  2. double volume (double H, double R) – with height(H) and radius(R) as the arguments, returns the volume of a cylinder using the formula.
    V = 22/7 x R2 x H

  3. double volume (double L, double B, double H) – with length(L), breadth(B) and Height(H) as the arguments, returns the volume of a cuboid using the formula.
    V = L x B x H

💻 Program Code

public class RansariVolume
{
    double volume(double r) {
        return (4 / 3.0) * (22 / 7.0) * r * r * r;
    }

    double volume(double h, double r) {
        return (22 / 7.0) * r * r * h;
    }

    double volume(double l, double b, double h) {
        return l * b * h;
    }

    public static void main(String args[]) {
        RansariVolume obj = new RansariVolume();
        System.out.println("Sphere Volume = " +
            obj.volume(6));
        System.out.println("Cylinder Volume = " +
            obj.volume(5, 3.5));
        System.out.println("Cuboid Volume = " +
            obj.volume(7.5, 3.5, 2));
    }
}
                        

🖥 Program Output

Sphere Volume = 905.142857142857
Cylinder Volume = 192.5
Cuboid Volume = 52.5
Press any key to continue . . .

                            
No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.