Write a Circle Class that has the followingfields: · Radius: adouble · PI: afina
ID: 3616341 • Letter: W
Question
Write a Circle Class that has the followingfields:
· Radius: adouble
· PI: afinal double initialized with the value 3.14159
The class should have the following methods:
· Constructor. Accepts the radius of the circle asan argument.
· setRadius. A mutator method for the radiusfield.
· getRadius. An accessor method for the radiusfield.
· getArea. Returns the area of the circle, which iscalculate as
area = PI * radius* radius
· getDiameter. Returns the diameter of the circle,which is calculated as
diameter = radius* 2
· getCircumference. Returns the circumference of theof the circle, which is calculated as
circumference = 2 *PI * radius
Write a program that demonstrates the Circle class by asking theuser for the circle’s radius, creating aCircle object, and then reporting thecircle’s area, diameter, and circumference.
Explanation / Answer
Dear.. import java.util.*;import java.lang.*;
import java.io.*;
class circle
{
public static void main(String args[])
{
Scanner s;
Scanner l;
String input_rec;
int cx;
int cy;
int px;
int py;
double radius;
double area;
double circ; double diameter;
s = new Scanner( System.in );
System.out.printf( "enter point ofcentre: " );
input_rec = s.nextLine( );
l = new Scanner( input_rec );
cx = l.nextInt( );
cy = l.nextInt( );
System.out.printf( "enter point oncircle: " );
input_rec = s.nextLine( );
l = new Scanner( input_rec );
px = l.nextInt( );
py = l.nextInt( );
radius = Math.sqrt( (double)(((cx-px)*(cx-px)) + (cy-py)*(cy-py)));
area = Math.PI * radius * radius;
circ = Math.PI * 2 * radius;
diameter =Math PI* radius * 2;
System.out.printf( "radius = %.2funits ", radius );
System.out.printf( "diameter = %.2f units ",radius * 2 );
System.out.printf( "area = %.2f squnits ", area );
System.out.printf( "circumference =%.2f ", circ ); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.