In: Computer Science
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.
SAMPLE RUN #0: ./circle_Non-Interactive
Interactive Session Standard Error (empty) Standard Output Hide Invisibles
Highlight: NoneStandard Input OnlyPrompts OnlyStandard Output w/o PromptsFull Standard OutputAllShow Highlighted Only
Calling·default·constructor:·The·circle's·radius·in·the·default·Circle·Object·is:·0↵ ↵ Calling·setRadius(20)·to·change·radius·of·default:↵ The·circle's·radius·in·the·default·Circle·Object·is·Now:·20↵ ↵ Creating·Circle·circle2(10):·↵ The·circle2's·radius·in·the·circle2·Object·is:·10↵ ↵ The·circle2's·area·is·therefore:·314.159↵ The·circle2's·diameter·is·therefore:·20↵ The·circle2's·circumference·is·therefore:·62.8318↵
out put must be like sample run. HELP PLEASE
Java Program
import java.util.Scanner; //importing Scanner utility class
public class Main //Main class with main method for testing Circle class
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter radius: "); //getting raidus input
double radius = sc.nextDouble();
System.out.println("Calling·default·constructor:·The·circle's·radius·in·the·default·Circle·Object·is:·0\n");
Circle circle = new Circle(); //creating a circle object by default constructor
System.out.println("Calling·setRadius("+radius+")·to·change·radius·of·default:");
circle.setRadius(radius); //setting circle raidus to user inputted radius
System.out.println("The·circle's·radius·in·the·default·Circle·Object·is·Now:·"+circle.radius+"\n");
System.out.println("Creating·Circle·circle2(10):·");
Circle circle2 = new Circle(10); //creating circle2 by parametrized constructor
System.out.println("The·circle2's·radius·in·the·circle2·Object·is:·"+circle2.radius+"\n");
System.out.println("The·circle2's·area·is·therefore:·"+circle2.getArea());
System.out.println("The·circle2's·diameter·is·therefore:·"+circle2.getDiameter());
System.out.println("The·circle2's·circumference·is·therefore:·"+circle2.getCircumference());
}
}
class Circle //Circle class definition
{
public static double pi; //class variable
public double radius; //object variable
static //static block executed only once when the class is loaded for the first time
{
pi = 3.14159; //initializing static variables
}
Circle() //default constructor called everytime an object is created
{
radius = 0.0;
}
Circle(double r) //parametrized constructor
{
radius = r;
}
public void setRadius(double r) //setter method
{
radius = r;
}
public double getRadius() //getter method
{
return radius;
}
public double getArea() //computes and returns area of circle
{
return pi*radius*radius;
}
public double getDiameter() //computes and returns circle diameter
{
return 2*radius;
}
public double getCircumference() //computes and returns circle circumference
{
return 2*pi*radius;
}
}
Program Screenshot
Output
Each and everything is explained clearly in the comment section of the code.
Thank you! Hit like, if you like my work.