Circle Class
/**
*A circle has an area and perimeter.
*/
public class Circle
{
private double radius;
/**
*Creates a circle with a given radius.
*@param radius Specifies the radius.
*/
public Circle(double newRadius)
{
radius=newRadius;
}
/**
*Calculates and returns the area of the circle.
*@return Returns the area.
*/
public double getArea()
{
return (22/7.0)*radius*radius;
}
/**
*Calculates and returns the perimeter of the circle.
*@return Returns the perimeter.
*/
public double getPerimeter()
{
return 2*(22/7.0)*radius;
}
/**
*Returns all the stored information on the circle.
*@return Returns all information.
*/
public String toString()
{
return "Radius: "+radius+"\n"+
"Area: "+getArea()+"\n"+
"Perimeter: "+getPerimeter();
}
}
/** OUTPUT FROM DEMO
Area= 78.57142857142857
Perimeter= 31.428571428571427
Radius: 5.0
Area: 78.57142857142857
Perimeter: 31.428571428571427
Press any key to continue...
*/
Home