Create a class name Circle with fields named radius, diameter, and area. Include
ID: 3543378 • Letter: C
Question
Create a class name Circle with fields named radius, diameter, and area. Include a constuctor that sets the radius to 1 and calculates
the other two values. Also include methods named setRadius() and getRadius(). The setRadius() method not only sets the radius vut also calculates the other two values. (The diameter of the circle is twice the radius, and the area of the circle is pi multiplied by the square of the radius. Use the Math class PI constant for this calculation.) Save the class as Circle.java
Explanation / Answer
public class Circle {
double radius;
double diameter;
double area;
public Circle(){
radius = 1.0;
diameter = 2*radius;
area = Math.PI * radius * radius;
}
public void setRadius(double r){
radius = r;
diameter = 2*radius;
area = Math.PI * radius * radius;
}
public double getRadius(){
return radius;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.