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

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.)

This is what I have but it is incorrect.

Circle.java

public class Circle { private double radius, diameter, area; public Circle() { radius = 1; calc(); }

public void setRadius(double radius)

{ this.radius = Math.abs(radius); calc(); }

public double getRadius()

{ return this.radius; } public double getDiameter()

{ return this.diameter; } public double getArea()

{ return this.area; } private void calc()

{ diameter = radius/2; area = (22/7)*radius*radius; } }

TestCircle.java

public class TestCircle { public static void main(String... args) { Circle c1 = new Circle(); Circle c2 = new Circle(); Circle c3 = new Circle();

c1.setRadius(2.0932); c2.setRadius(22.234);

display(c1); display(c2); display(c3); }

public static void display(Circle c) { System.out.println(" Radius " + c.getRadius()); System.out.println("Diameter" + c.getDiameter()); System.out.println("Area: " + c.getArea()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!