In this section, you will learn to calculate the area and perimeter of a circle.
Write a program to calculate area and perimeter of a circle
The given example will teach you the method for preparing a program to calculate the area and perimeter of a circle. First of all name a class as "CircleArea" under Java I/O package and define and integer r=o, which is the radius of the circle. Now use try exception to handle errors and other exceptional events. As we have to input the value of radius here create a buffered class with an object as 'br1'. This create a buffering character input stream that uses a default sized input buffer. The InputStreamReader here works as a translator that converts byte stream to character stream. Now type message that "Enter radius of circle" in the System.out.println method.
Now use the parseInt() method of the Integer class in order to convert from external numeric format to internal format. Now create the Math class in which all the mathematical functions are defined. This Math class can be imported from the java.lang.* package. Write the program for both the cases: radius and perimeter.
Before ending the program use the Catch mechanism that detects and catch user input errors. In the end compile and run the program and enter your desired value as radius for calculating the radius and perimeter of the circle.
Here is the code of the program:
import java.io.*;
|