Write a Circle class that has the followingmember variables: • radius : a double
ID: 3610255 • Letter: W
Question
Write a Circle class that has the followingmember variables:
• radius: a double
• pi: a double initialized with the value3.14159
The class should have the following member functions:
• Default Constructor. A defaultconstructor that sets radius to 0.0.
• Constructor. Accepts the radius of thecircle as an argument.
• setRadius. A mutator function for theradius variable.
• getRadius. An accessor function for theradius variable.
• getArea. Returns the area of thecircle, which is calculated as
area = pi * radius * radius
• getDiameter. Returns the diameter ofthe circle, which is calculated as
diameter = radius * 2
• getCircumference. Returns thecircumference of the circle, which is calculated as
circumference = 2 * pi * radius
Write a program that demonstrates the Circleclass by asking the user for the circle’s radius, creating aCircle object, and then reporting thecircle’s area, diameter, and circumference.
Explanation / Answer
import java.io.*; import java.util.*; import java.lang.*; public class Circle{ private double radius; public double pi = 3.14159; public Circle() { this.radius = 0.0; } public Circle( double r) { this.radius =r; } public void setRadius(double r) { this.radius=r; } public double getRadius() { return this.radius; } public double getArea() { return pi*radius*radius; } public double getDiameter() { return 2*radius; } public double getCircumference() { return 2*pi* radius; } public static void main(String args[]){ System.out.print("Enter the Radius for the Circle: "); double r=0.0; try{ r= Double.parseDouble (new BufferedReader(newInputStreamReader(System.in)).readLine()); } catch (Exception e){ e.printStackTrace(); } Circle c = new Circle(r); System.out.println(" The area of the Circle with radius " + r + " is " + c.getArea()); System.out.println(" The diameter of the Circle withradius " + r + " is " +c.getDiameter()); System.out.println(" The circumference of the Circle withradius " + r + " is " +c.getCircumference()); }//end of main } //end of Circle class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.