9 Circle Class Write a circle class that has the following m
Solution
Circle.java
public class Circle {
//Declaring instance variable
private double radius;
//Declaring variable
double pi = 3.14159;
//Zero argumented constructor
public Circle() {
super();
this.radius = 0.0;
}
//Parameterized constructor
public Circle(double radius) {
super();
this.radius = radius;
}
//Getters and setters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//This method will return the area of the circle
public double getArea() {
return pi * radius * radius;
}
//This method will return the diameter of the circle
public double getDiameter() {
return 2 * radius;
}
//This method will return the circumference of the circle
public double getCircumference() {
return 2 * pi * radius;
}
}
_______________________________
Test.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
double radius;
DecimalFormat df=new DecimalFormat(\"#.##\");
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the radius of the circle
System.out.print(\"Enter the radius of the circle :\");
radius=sc.nextDouble();
//Creating the circle class object by passing the radius as argument
Circle c=new Circle(radius);
/* Displaying the diameter of the circle by
* calling the method on the Circle class object
*/
System.out.println(\"Diameter of the circle :\"+df.format(c.getDiameter()));
/* Displaying the area of the circle by
* calling the method on the Circle class object
*/
System.out.println(\"Area of the circle :\"+df.format(c.getArea()));
/* Displaying the circumference of the circle by
* calling the method on the Circle class object
*/
System.out.println(\"Circumference of the circle :\"+df.format(c.getCircumference()));
}
}
_____________________________________
output:
Enter the radius of the circle :5.5
Diameter of the circle :11
Area of the circle :95.03
Circumference of the circle :34.56
___________Thank You

