I need help with this. I know how to set the radius and pi andcreate a construct
ID: 3609307 • Letter: I
Question
I need help with this. I know how to set the radius and pi andcreate a constructor but i have forgotten how to do the set radiusand other things. A start would be greatly appreciatedWrite a Circle class that has the following fields:
· Radius : a double
· PI: a final double initialized with the value 3.14159
This Circle class should have the following methods:
· Constructor. Accepts the radius of the circle as anargument.
· setRadius. A mutator (setter) method for the radiusfield.
· getRadius. An accessor (getter) method for the radiusfield.
· getArea. Returns the area of the circle, which iscalculated as
area = PI * radius * radius
· getDiameter. Returns the diameter of the circle, which iscalculated as
diameter = radius * 2
· getCircumference. Returns the circumference of the circle,which is calculated as
circumference = 2 * PI * radius
Write a program (CircleDemo) that demonstrates the Circle class byasking the user for the
circle's radius, creating a Circle object, and then reporting thecircle's area, diameter, and
circumference.
Explanation / Answer
////////////////////////////////////////////////////// import java.util.Scanner; //import Scanner to read from commandline public class CircleDemo { //mainmethod public static void main(String[]args) { //create the scanner object to read from the commandline Scanner scan=newScanner(System.in); System.out.println("What isthe circle's radius?"); //readthe double and store as radius double radius =scan.nextDouble(); //create the circle object with radius Circle circle = newCircle(radius); //Printout the stuff System.out.println("Area ="+circle.getArea()); System.out.println("Diameter= "+circle.getDiameter()); System.out.println("Circumference ="+circle.getCircumference()); } } ////////////////////////////////////////////////public class Circle { //variable forradius private double radius; //final variable forPI public final double PI = 3.14159; //constructor public Circle(double rad) {radius = rad;}
//setsradius public void setRadius(double rad) {radius = rad;} //getradius public double getRadius() {return radius;} //calculate andreturn area public double getArea() {return PI*radius*radius;} //calculate andreturn diameter public double getDiameter() {return 2*radius;} //calculate andreturn circumference public double getCircumference() {return 2*PI*radius;} }