Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called Circle. This class will have a double variable radius as i

ID: 3710741 • Letter: C

Question

Create a class called Circle. This class will have a double variable radius as its private member.

The class should have a constructor that defaults its parameter to 1 if a parameter value is not provided. Otherwise the constructor will set the object radius to the parameter value.

The class should have the appropriate getter and setter and should have functions that calculates the diameter, the circumference, and the area of the circle based on the member radius.

Demonstrate this circle object and its diameter, circumference, and area functions by dynamically allocating a Circle pointer in a main function.

Explanation / Answer


public class Circle {
private int radius;
public Circle() {
this(1);
}
public Circle(int radius) {
this.radius = radius;
}
  
public double area() {
return 3.14*this.radius*this.radius;
}
public double circumference() {
return 2*3.14*this.radius;
}
public int diameter() {
return 2*this.radius;
}
  
public int getRadius() {
return this.radius;
}
  
public void setRadius(int radius) {
this.radius = radius;
}
  
}

public class Main{
public static void main(String[] args) {
  
Circle c = new Circle();
System.out.println("Radius: " + c.getRadius());
c.setRadius(5);
System.out.println("Radius: " + c.getRadius());
System.out.println("Diameter: " + c.diameter() + ", Area: " + c.area() + ", Circumference: " + c.circumference());
  
Circle c2 = new Circle(2);
System.out.println("Radius: " + c2.getRadius());
c2.setRadius(5);
System.out.println("Radius: " + c2.getRadius());
System.out.println("Diameter: " + c2.diameter() + ", Area: " + c2.area() + ", Circumference: " + c2.circumference());
}
}

Sample run

Radius: 1
Radius: 5
Diameter: 10, Area: 78.5, Circumference: 31.400000000000002
Radius: 2
Radius: 5
Diameter: 10, Area: 78.5, Circumference: 31.400000000000002

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote