Java Reference
In-Depth Information
A Sample Program
The following Java program demonstrates some of the programming ele-
ments and constructs discussed so far.
On the Web
The source file for the program Area.java can be found in the Chapter
5 directory at www.crcpress.com.
//
File name: Area.java
//
Reference: Chapter 5
//
//
Java program to calculate the area of a circle
//
Topics:
//
1. Using numeric variables and constants
//
2. Obtaining keyboard input
//
3. Displaying program data
//
4. Performing simple numeric calculations
//
//
Requires:
//
1. Keyin class in the current directory
public class Area
{
// Constant PI is defined at the class level
static final double PI = 3.141592653589793;
public static void main(String[] args)
{
// Local variables
double radius, area;
// Input radius from keyboard
radius = Keyin.inDouble("Enter radius: ");
// Perform calculations and display result
area = PI * (radius * radius);
System.out.println("The area is: " + area);
}
}
Search WWH ::




Custom Search