Wednesday, 5 February 2025

Write a java program to define an interface which has methods area () volume (). Define constant PI. Create a class cylinder which implements this interface and calculate area and volume

// Define the interface

interface Shape {

        double PI = 3.14159;

      double area();

    double volume();

}


// Cylinder class implementing Shape interface

class Cylinder implements Shape {

    private double radius;

    private double height;


    // Constructor to initialize radius and height

    public Cylinder(double radius, double height) {

        this.radius = radius;

        this.height = height;

    }


    // Implementing the area method

        public double area() {

         return 2 * PI * radius * height + 2 * PI * radius * radius;

    }


    // Implementing the volume method

      public double volume() {

                return PI * radius * radius * height;

    }

}


public class InterfaceDemo {

    public static void main(String[] args) {

        // Create a Cylinder object

        Cylinder cylinder = new Cylinder(5, 10); 

        // Calculate and display area and volume

        System.out.println("Cylinder Area: " + cylinder.area());

        System.out.println("Cylinder Volume: " + cylinder.volume());

    }

}