19.5k views
5 votes
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 it 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 the Math class PI constant for this calculation.) Save the class as Circle.java.

2 Answers

2 votes

Final answer:

To create a class named Circle with the specified fields and methods in Java, you can follow these steps.

Step-by-step explanation:

To create a class named Circle with the specified fields and methods in Java, you can follow these steps:

  1. Define a class named Circle.
  2. Add three private instance variables: radius, diameter, and area.
  3. Create a constructor that sets the radius to 1 and calculates the diameter and area using the given formulas.
  4. Implement the setRadius() method that not only sets the radius but also calculates the diameter and area.
  5. Implement the getRadius() method that returns the radius.

Here's an example of the Circle class:

public class Circle {
private double radius;
private double diameter;
private double area;

public Circle() {
radius = 1;
setDiameterAndArea();
}

public void setRadius(double radius) {
this.radius = radius;
setDiameterAndArea();
}

public double getRadius() {
return radius;
}

private void setDiameterAndArea() {
diameter = 2 * radius;
area = Math.PI * radius * radius;
}
}

User Seva Titov
by
4.6k points
4 votes

Answer:

The code for this class is shown in the explanation section

Step-by-step explanation:

public class Circle {

private double radius;

private double diameter;

private double area;

public Circle(double radius, double diameter, double area) {

this.radius = 1;

this.diameter = diameter;

this.area = area;

}

public void setRadius(double radius) {

this.radius = radius;

diameter = radius*2;

area= Math.PI*(radius*radius);

}

public double getRadius() {

return radius;

}

}

User Hector Magana
by
4.5k points