Circle Class Write a Circle class that has the following fields: radius: a doubl
ID: 3792488 • 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. 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 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. use blue j to code and give to source code. one is circle and the one is circledemo showing the relationship between the two. one will provide the code and the other will output the code.
Explanation / Answer
Circle . Java:
package org.chegg;
public class Circle {
double radius;
final double PI = 3.14159;
public Circle(double radius){
this.radius = radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public double getArea(){
return PI * radius * radius;
}
public double getDiameter(){
return radius * 2;
}
public double getCircumference(){
return 2 * PI * radius;
}
}
CircleDemo.java :
package org.chegg;
import java.util.Scanner;
public class CircleDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Radius of Circle:");
double radius = sc.nextDouble();
Circle circle = new Circle(radius);
System.out.println("Area of a Circle:"+circle.getArea());
System.out.println("Diameter of a Circle:"+circle.getDiameter());
System.out.println("Circumference of a Circle:"+circle.getCircumference());
}
}
Input & Output :
Enter Radius of Circle:
4.5
Area of a Circle:63.6171975
Diameter of a Circle:9.0
Circumference of a Circle:28.27431
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.