w3resource

Java: Create a Circle class with area and circumference calculation

Java OOP: Exercise-4 with Solution

Write a Java program to create a class called "Circle" with a radius attribute. You can access and modify this attribute. Calculate the area and circumference of the circle.

Sample Solution:

Java Code:

// Define the Circle class
public class Circle {
    // Declare a private variable to store the radius of the circle
    private double radius;

    // Constructor for the Circle class that initializes the radius variable
    public Circle(double radius) {
        // Set the radius variable to the provided radius parameter
        this.radius = radius;
    }

    // Method to retrieve the radius of the circle
    public double getRadius() {
        // Return the value of the radius variable
        return radius;
    }

    // Method to set the radius of the circle
    public void setRadius(double radius) {
        // Set the radius variable to the provided radius parameter
        this.radius = radius;
    }

    // Method to calculate and return the area of the circle
    public double getArea() {
        // Calculate the area using the formula π * radius^2 and return the result
        return Math.PI * radius * radius;
    }

    // Method to calculate and return the circumference of the circle
    public double getCircumference() {
        // Calculate the circumference using the formula 2 * π * radius and return the result
        return 2 * Math.PI * radius;
    }
} 

The above "Circle" class has a private attribute 'radius', a constructor that initializes this attribute with the value passed as an argument, and getter and setter methods to access and modify this attribute. It also calculates circle area and circumference using methods.

// Define the Main class
public class Main {
    // Define the main method which is the entry point of the program
    public static void main(String[] args) {
        // Declare an integer variable r and initialize it with the value 5
        int r = 5;
        // Create an instance of the Circle class with the radius r
        Circle circle = new Circle(r);
        // Print the radius of the circle to the console
        System.out.println("Radius of the circle is " + r);
        // Print the area of the circle to the console
        System.out.println("The area of the circle is " + circle.getArea());
        // Print the circumference of the circle to the console
        System.out.println("The circumference of the circle is " + circle.getCircumference());
        // Update the radius variable r to 8
        r = 8;
        // Set the radius of the circle to the new value of r
        circle.setRadius(r);
        // Print the updated radius of the circle to the console
        System.out.println("\nRadius of the circle is " + r);
        // Print the updated area of the circle to the console
        System.out.println("The area of the circle is now " + circle.getArea());
        // Print the updated circumference of the circle to the console
        System.out.println("The circumference of the circle is now " + circle.getCircumference());
    }
}

In the above main() function, we create an instance of the "Circle" class with a radius of 5, and call its methods to calculate the area and circumference. We then modify the radius using the setter method and print the updated area and circumference.

Sample Output:

Radius of the circle is 5
The area of the circle is 78.53981633974483
The circumference of the circle is 31.41592653589793

Radius of the circle is 8
The area of the circle is now 201.06192982974676
The circumference of the circle is now 50.26548245743669

Flowchart:

Flowchart: Java  OOP Exercises: Create a Circle class with area and circumference calculation.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Calculate area and perimeter of a rectangle.
Java OOP Next: Create a Circle class and calculate its area and circumference.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/oop/java-oop-exercise-4.php