/* Program Circle.java
 * This program takes the radius as input, and calculates the area
 * of the circle.
 */

// Import statement
import java.io.*;

// Class declaration
public class Circle{

    // Private data variables
    private double radius;
    private double area;
	 
	 // Constant
    final double PI = 3.14159;

    // Default Constructor
    // The Constructor instantiates all of the data members and
    // allocates memory space for those variables.
    public Circle (){
	 
        // Initializing the radius and area to zero.
        radius=0;
        area=0;
    }

    // Class methods can be public or private. Only the methods of the
    // class can access the private data members of the class.
    // The methods can be used to retrieve the current value of the data
    // members or to change the value of the private data variables.


    // This function gets the radius of the circle from the
    // keyboard and store it in the private data member radius.
    public  void setRadius(){
       
        String radiusStr;
        double temp;
		  
		  // Prompt the user to input the radius
        BufferedReader stdin =
               new BufferedReader(new InputStreamReader(System.in), 1);
        System.out.println("Enter a radius: ");

        // Read the radius from the console
        try {
		  
            // get input from console
            radiusStr = stdin.readLine();

            // The readLine() function returns the input from the console
            // in string format. The string is then parsed and converted
            // into a double using the parseDouble() function. The radius
            // is then stored in the private data variable radius.
            temp = Double.parseDouble(radiusStr);
            if( temp <0){
                System.out.println("Enter a non negative number");
                // call the same method again
                setRadius();
            }
            else
                radius = temp;
        }catch(IOException ex){
            // try to catch any input and output exceptions if any.
            System.out.println(ex);
        }
    } // end of method setRadius

    // This public method computes the area of the circle.
    public void computeArea (){
        // The area is computed using the member variable radius and
        // is stored in the member variable area.
        area  = PI * radius * radius;
        System.out.println ("Given a radius of " + radius + ", the area is " + area);
    }// end of method computeArea
   
} // end of Circle class


