Sunday, November 3, 2024

Circle Using a Class in Java

 


Program Listing

import java.text.DecimalFormat;


class Circle {

    private double radius;


    public Circle(double radius) {

        this.radius = radius;

    }


    public double getRadius() {

        return radius;

    }


    public double calculateArea() {

        return Math.PI * Math.pow(radius, 2);

    }


    public double calculateCircumference() {

        return 2 * Math.PI * radius;

    }

}


public class Main {

    public static void main(String[] args) {

        Circle circle = new Circle(8.15);


        DecimalFormat df = new DecimalFormat("#.00");


        System.out.println("\n\tCircle Using a Class in Java\n");

        

        System.out.println("\tRadius: " + df.format(circle.getRadius()));

        System.out.println("\tArea: " + df.format(circle.calculateArea()));

        System.out.println("\tCircumference: " + df.format(circle.calculateCircumference()));

         System.out.println("\n\tEnd of Program. Thank you for Using This Program\n");

    }

}


No comments:

Post a Comment