Create a class named Circle with fields named radius, diameter, and area. Includ
ID: 3544185 • Letter: C
Question
Create a class named Circle with fields named radius, diameter, and area. Include a constructor 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 but also calculates the other two values. (The diameter of a circle is twice the radius, and the area of a circle is PI multiplied by the square of the radius. Use Math class PI consant for this calculation.
It need to compile and run!!
Explanation / Answer
import java.util.*;
public class Circle
{
private double radius;
private double diameter;
private double area;
public Circle()
{
radius = 1;
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;
}
public double getDiameter()
{
return diameter;
}
public double getArea()
{
return area;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter circle Radius ");
double radius = in.nextDouble();
Circle C1= new Circle();
C1.setRadius(radius);
System.out.println("Circle Diameter given by "+C1.getDiameter());
System.out.println("Circle Area given by "+C1.getArea());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.