Circle Class Write a Circle class that has the following fields: · radius: a dou
ID: 3534929 • Letter: C
Question
Circle Class
Write a Circle class that has the following fields:
· radius: a double
· PI: a final double initialized with the value 3.14159
The class should have the following methods:
· Constructor: Accepts the radius of the circle as an argument
· Constructor: A no-arg constructor that sets the radius field to 0.0
· setRadius: A mutator method for the radius field
· getRadius: An accessor method for the radius field
· getArea: Returns the area of the circle, which is calculated as area = PI * radius * radius
· getDiamete: 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.
Explanation / Answer
public class circle { final double PI = 3.14159; double radius; double area; double circumference; double diameter; public circle(double r, double a, double c, double d) //constructor of the circle class. { radius = r; area = a; circumference = c; diameter = d; } public circle() //default constructor of the circle class. { radius = 0; area = 0; circumference = 0; diameter = 0; } public void setRadius(double r) { radius = r; } public double getRadius() { return radius; } public double getArea() { return area = PI * radius * radius; } public double getCircumference() { return circumference = 2 * PI * radius; } public double getDiameter() { return diameter = radius * 2; } public double getPI() { return PI; } } program: import java.util.Scanner; public class circleCreate { public static void main(String[]Args) { circle myCircle = new circle(); double circlePI; double circleRadius; double circleArea; double circleCircumference; double circleDiameter; Scanner keyboard = new Scanner(System.in); //Creates a Scanner object //get the user to enter the radius of the circle. System.out.print("What is the circle's radius? "); circleRadius = keyboard.nextDouble(); myCircle.radius(); System.out.println("The area of the circle is " + myCircle.getArea()); System.out.println("The circumference of the circle is " + myCircle.getCircumference()); System.out.println("The diameter of the circle is " + myCircle.getDiameter()); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.