Define a class to model an abstract circle. A circle is defined by the value of
ID: 650907 • Letter: D
Question
Define a class to model an abstract circle. A circle is defined by the value of its radius. Your class should have one instance variable of type double. The name of the class is Circle. In addition to writing your own default, parameterless constructor, include a constructor with a single argument that is used to set the instance variable of an object of the Circle class. Note that the radius of a circle must be greater than zero and your class should uphold this restriction. Define accessor (getter) and mutator (setter) methods. In addition, define the following accessor methods:Explanation / Answer
commenr if you have any doubts.
//Circle.java
public class Circle
{
private double radius;
public Circle()
{
this.radius=0;
}
public Circle(double radius)
{
setRadius(radius);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
if(radius>0)this.radius = radius;
else this.radius=0;
}
public double gtDiameter()
{
return 2*this.radius;
}
public double getCircumference()
{
return Math.PI*2*this.radius;
}
public double getArea()
{
return Math.PI*this.radius*this.radius;
}
public String toString()
{
return "Radius= "+this.radius+" Circumference= "+getCircumference()+" Area= "+getArea();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.