This example is used to calculate the area and circumference of a circle with given radius. The class Circle contains the field radius and methods setData (), area ()and circumference ().The statement.
Circle obj = new Circle();
creates a Circle object in memory and assign it to the reference variable obj,
The statement,
obj.setData(3.5) ;
assigns the radius field of the Circle object to 3.5 using the reference variable obj.
The statement,
obj.area ();
obj.circumference() ;
calculates the area and circumference of the corresponding Circle object, which is finally displayed.
class circle
{
double radius; //radius of circle
void setData(double r)
{
radius = r;
}
void area ()
{
double circleArea = Math.PI * radius *radius;
System.out.println("Area of circle is = " + circleArea);
}
void circumference()
{
double cir = 2 * Math.PI * radius ;
System.out.println("circumference of circle is = " + cir);
}
}
public class AreaAndCircumference
{
public static void main(String[] args)
{
circle obj = new circle();
obj.setData(3.5 ); //call setData method
obj.area( ); //call area method
obj.circumference( ); //call circumference method
}
}
Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.
Related Articles
Basic Courses
Advance Courses