Thursday, February 29, 2024

Java program to calculate area and perimeter of circle using interfaces.

interface Shape {
    double calculateArea();
}

interface CircleProperties {
    double calculatePerimeter();
}

class Circle implements Shape, CircleProperties {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double calculateArea() {
        return Math.PI * radius * radius;
    }

    public double calculatePerimeter() {
        return 2 * Math.PI * radius;
    }
}

public class CircleCalculator {
    public static void main(String[] args) {
        Circle myCircle = new Circle(5.0);

        System.out.println("Area of the circle: " + myCircle.calculateArea());
        System.out.println("Perimeter of the circle: " + myCircle.calculatePerimeter());
    }
}

No comments:

Post a Comment

Java program to implement file input stream class to read binary data from image file.

import java.io.FileInputStream; import java.io.IOException; public class ReadImageFile {     public static void main(String[] args) {       ...