9 Circle Class Write a circle class that has the following m

9. Circle Class Write a circle class that has the following member variables: .radius: a double pi: a double initialized with the value 3.14159 The class should have the following member functions: . Default Constructor. A default constructor that sets radius to 0.0. Constructor. Accepts the radius of the circle as an argument. . setRadius. A mutator function for the radius variable. . getRadius. An accessor function for the radius variable. getArea. Returns the area of the circle, which is calculated as area pi radius radius getDiameter. Returns the diameter of the circle, which is calculated as diameter radius 2 getcircumference. Returns the circumference of the circle, which is calculated as circumference = 2 * pi * radius . Write a program that demonstrates the circle class by asking the user for the circle\'s radius, creating a circle object, and then reporting the circle\'s area, diameter, and circumference.

Solution

Circle.java

public class Circle {
   //Declaring instance variable
   private double radius;
  
   //Declaring variable
   double pi = 3.14159;

   //Zero argumented constructor
   public Circle() {
       super();
       this.radius = 0.0;
   }

   //Parameterized constructor
   public Circle(double radius) {
       super();
       this.radius = radius;
   }

   //Getters and setters
   public double getRadius() {
       return radius;
   }

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

   //This method will return the area of the circle
   public double getArea() {
       return pi * radius * radius;
   }

   //This method will return the diameter of the circle
   public double getDiameter() {
       return 2 * radius;
   }

   //This method will return the circumference of the circle
   public double getCircumference() {
       return 2 * pi * radius;
   }

}

_______________________________

Test.java

import java.text.DecimalFormat;
import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
   double radius;
   DecimalFormat df=new DecimalFormat(\"#.##\");
   //Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
  
//Getting the radius of the circle
System.out.print(\"Enter the radius of the circle :\");
radius=sc.nextDouble();
  
//Creating the circle class object by passing the radius as argument
Circle c=new Circle(radius);
  
/* Displaying the diameter of the circle by
* calling the method on the Circle class object   
*/
System.out.println(\"Diameter of the circle :\"+df.format(c.getDiameter()));
  
/* Displaying the area of the circle by
* calling the method on the Circle class object   
*/
System.out.println(\"Area of the circle :\"+df.format(c.getArea()));
  
/* Displaying the circumference of the circle by
* calling the method on the Circle class object   
*/
System.out.println(\"Circumference of the circle :\"+df.format(c.getCircumference()));
  

   }

}

_____________________________________

output:

Enter the radius of the circle :5.5
Diameter of the circle :11
Area of the circle :95.03
Circumference of the circle :34.56

___________Thank You

 9. Circle Class Write a circle class that has the following member variables: .radius: a double pi: a double initialized with the value 3.14159 The class shoul
 9. Circle Class Write a circle class that has the following member variables: .radius: a double pi: a double initialized with the value 3.14159 The class shoul

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site