Javatpoint Logo
Javatpoint Logo

Circle Program in Java

Java is a versatile and widely used programming language, and it's an excellent choice for implementing various algorithms and mathematical concepts. One such concept is working with circles. In this section, we will explore how to write a Circle program in Java, covering different approaches to achieve this and provide complete code examples.

Circles

A circle is a fundamental geometric shape defined by a set of points in a plane that are equidistant from a fixed point called the center. The distance from the center to any point on the circumference is called the radius.

Approach 1: Using Classes

The Object-Oriented Programming (OOP) paradigm in Java allows us to represent circles as objects. We will create a Circle class with attributes such as the center coordinates and radius, and then implement methods to perform operations like calculating the area and circumference.

CircleProgram.java

Output:

Area: 78.53981633974483
Circumference: 31.41592653589793

In this approach, we encapsulate the circle's properties within a class and provide methods to calculate the area and circumference. The main function demonstrates how to use this Circle class.

Approach 2: Using Functions

If we prefer a more procedural approach, you can use functions to perform circle-related calculations. In this approach, we would not define a Circle class but rather use separate functions to calculate area and circumference.

CircleProgram.java

Output:

Area: 78.53981633974483
Circumference: 31.41592653589793

In this approach, we pass the radius as a parameter to the functions calculateArea() and calculateCircumference() to get the desired results.

Finding The Area and Circumference of the Circle

CircleProgram2.java

Conclusion

In this section, we explored two different approaches to implement a Circle program in Java: one using classes (Object-Oriented) and the other using functions (procedural). The choice of approach depends on your specific requirements and coding style preferences.

Using a class-based approach provides a more structured and encapsulated way to work with circles, while the procedural approach is simpler and may be more suitable for small-scale programs. Whichever approach we choose, Java provides the flexibility and tools to work with geometric shapes like circles effectively.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA