Tuesday, 4 February 2025

Write a Java program to calculate the volume of a cone and a cylinder using method overloading.


import java.io.*;


public class CalculateVolume {


    //  calculate the volume of a cylinder (V = πr²h)

    public double volume(double radius, double height) {

        return Math.PI * radius * radius * height;

    }


    //  calculate the volume of a cone (V = (1/3)πr²h)

    public double volume(double radius1, double height1) {

       return (1.0 / 3) * Math.PI * radius1 * radius1 * height1;

       return volume(radius1, height1); 

    }


    public static void main(String[] args) {

     CalculateVolume calc = new CalculateVolume(); 

        // Calculate and display volumes

        double cylinderVolume = calc.volume(5.0, 10.0); // Cylinder

        System.out.println("Volume of Cylinder: " + cylinderVolume);


        double coneVolume = calc.volume(5.0, 10.0); // Cone

        System.out.println("Volume of Cone: " + coneVolume);

    }

}