This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Wednesday, March 8, 2023

Write an object oriented program in java to find area of circle.

 Here's an example object-oriented Java program that calculates the area of a circle using the formula A = Ï€r²:

import java.util.Scanner;

public class Circle {
    private double radius;

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

    public double getRadius() {
        return radius;
    }

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

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the radius of the circle: ");
        double radius = sc.nextDouble();
        Circle circle = new Circle(radius);
        System.out.println("The area of the circle is " + circle.getArea());
    }
}

This program defines a Circle class with a constructor that takes a radius as a parameter, as well as getRadius() and getArea() methods to get the radius and area of the circle, respectively. The getArea() method uses the formula for the area of a circle, A = Ï€r².

In the main() method, the program prompts the user to enter the radius of the circle, creates a new Circle object with that radius, and then prints out the area of the circle using the getArea() method.

No comments:

Post a Comment

If you have any doubts, please let me know

Slider Widget