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

a. Create a class named Circle with fields named radius, diameter, and area. Inc

ID: 673890 • Letter: A

Question

a. 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 names setRadius() and getRadius(). The setRadius() mehtod not only sets the radius, it also calculates the other two values. (The diameter of a circle is twice the radius and the area of a circle is pi multiplies by the square of the radius. Use the math class PI constant for this calculation.) Save the class as Circle.java.

b. Create a class named TestCircle whose main() method declares several Circle objects. Using the setRadius() method, assign one Circle a small radius value, and assign another a larger radius value. Do not assign a value to the radius of the third circle; instead, retain the value assigned at contruction. Display all the values for all the Circle objects. Save the application as TestCircle.java.

Explanation / Answer

Circle.java

package com.chegg.nancy.solutions;

public class Circle {

   private double radius;
   private double diameter;
   private double area;

   Circle() {
       this.radius = 1;
       this.area = 3.14 * this.radius;
       this.diameter = 2 * this.radius;
   }

   public double getRadius() {
       return radius;
   }

   public void setRadius(double radius) {
       this.radius = radius;
       this.area = 3.14 * radius;
       this.diameter = 2 * radius;

   }

   public void display() {
       System.out.println(" Radius = " + this.radius);
       System.out.println(" Diameter = " + this.diameter);
       System.out.println(" Area = " + this.area);
   }
}

________________________________________________________________________________________

TestCircle.java

package com.chegg.nancy.solutions;

public class TestCircle {

   public static void main(String[] args) {

       Circle c1 = new Circle();
       c1.setRadius(2);
       Circle c2 = new Circle();
       c2.setRadius(4);
       Circle c3 = new Circle();

       System.out.println("First circle");
       c1.display();
       System.out.println("Second circle");
       c2.display();
       System.out.println("Third circle");
       c3.display();
   }

}

____________________________________________________________________________________

Output:

First circle
Radius = 2.0
Diameter = 4.0
Area = 6.28
Second circle
Radius = 4.0
Diameter = 8.0
Area = 12.56
Third circle
Radius = 1.0
Diameter = 2.0
Area = 3.14

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