In: Computer Science
Write a Circle Class that has the following fields:
• radius: a double
• PI: a final double initialized with the value 3.14159
• Constructor. Accepts the radius of the circle as an argument
• Constructor. A no-arg constructor that sets the radius field to 0.0.
• setRadius. A mutator method for the radius field.
• getRadius. An accessor method for the radius field.
• getArea. Returns the area of the circle, which is calculated as area = PI * radius * radius.
• getDiameter. Returns the diameter of the circle, which is calculated as diameter = radius * 2.
• getCircumference. Returns the circumference of the circle, which is calculated as circumference = 2 * PI * radius.
Write a program that demonstrates the Circle class by asking the user for the radius of a circle, creating a circle object, and then reporting the circle’s area, diameter, and circumference. See Rectangle example in book.
Submit:
1. Algorithm
2. UML diagram
3. A complete Java program with comments/documentation
4. Output
Test Data: Radius =0 5
class Circle {
private double radius;
private final double PI = 3.14159;
// constructors
public Circle(double aRadius) {
super();
radius = aRadius;
}
// getters and setters
public double getRadius() {
return radius;
}
public void setRadius(double aRadius) {
radius = aRadius;
}
// returns area of circle
public double getArea() {
return PI * radius * radius;
}
// returns circumference of circle
public double getCircumference() {
return 2 * PI * radius;
}
// returns diameter
public double getDiameter() {
return 2 * radius;
}
// returns string representation of object
public String toString() {
return "Radius : " + getRadius() +
" Diameter : " + getDiameter() + " Area : " + getArea() + "
Circumference : "
+ getCircumference();
}
}
public class Main {
public static void main(String[] args) {
Circle c = new Circle(5);
System.out.println(c);
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me