Write a Circle class that has the following fields: • radius: a double • PI: a f
ID: 3635264 • Letter: W
Question
Write a Circle class that has the following fields:• radius: a double
• PI: a final double initialized with the value 3.14159
The class should have the following methods:
• 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
• getDiamete: 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 circle’s radius, creating a circle object, and then reporting the circle’s area, diameter and circumference.
Explanation / Answer
Please Rate:Thanks
import javax.swing.JOptionPane;
public class Circle1 {
private double radius;
public static final double PI = 3.14159;
public Circle1(double radius) {
this.radius = radius;
}
public Circle1() {
this(0.0);
}
public void setRadius(double newRadius) {
radius = newRadius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return PI * radius * radius;
}
public double getDiameter() {
return 2 * radius;
}
public double getCircumference() {
return 2 * PI * radius;
}
public static void main(String[] args) {
String ans = JOptionPane.showInputDialog(null, "Circle radius");
double radius = Double.parseDouble(ans);
Circle1 aCircle = new Circle1(radius);
System.out.println("Area: " + aCircle.getArea());
System.out.println("Diameter: " + aCircle.getDiameter());
System.out.println("Circumference: " + aCircle.getCircumference());
}
}
----------------------------------------------
Output:
Area: 314.159
Diameter: 20.0
Circumference: 62.8318
BUILD SUCCESSFUL (total time: 31 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.