Diameter, perimeter and area of a circle and length of an arc
In the article Right and distance between two points, we saw how to link two points with x and y coordinates to create a segment in Java. In this tutorial, we'll use the Point.java to draw a center circle p(x, y) and radius r. Let's start with the creation of the class Circle:public class Point {
double x,y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
}
public class Circle {
Center point;
double radius;
public Circle(Center Point, Double Radius){
this.center=center;
this.radius=radius;
}
}
Definitions
In a circle, we should know:- The diameter: composed of two rays, its length is 2r.
- The arc length: is a part of the circle subtended by an alpha angle expressed in radians, is equal to alpha*r.
- The Perimeter: is the length for an alpha angle of 2*PI, the length is 2*r*PI (PI=3.14).
- The surface or the air of the circle: PI*r².
The following Java code implements the four methods to obtain diameter, arc length, perimeter, and area: getDiameter(), getLength(), getLengthArc(), and getSurface().
public class Circle {Output
Center point;
static double radius;
public Circle(Center Point, Double Radius){
this.center=center;
this.radius=radius;
}
static double getDiametre(){
return radius*2;
}
static double getLength(){
return 2*3.14*radius;
}
static double getSurface(){
return 3.14*Math.pow(radius, 2);
}
static double getLengthArc(double alpha){
return 3.14*alpha;
}
public static void main(String[] args) {
Circle = new Circle(new Point(0, 0), 8);
System.out.println("Diameter: "+Circle.getDiameter());
System.out.println("Length: "+Circle.getLength());
//arc length with an angle of 55 radians
double alpha = 55;
System.out.println("Arc Length: "+Circle.getArcLength(55));
System.out.println("Surface: "+Circle.getSurface());
}
}
Diameter: 16.0
Perimeter: 50.24
Arc length: 172.700000000000002
Area: 200.96