Compute Area of a Circle using Java Programming

Compute Area of a Circle using Java Programming

In this example we will dissect a simple program in Java about Computing Area of a Circle – a very common intro activity in learning how to program.

The source code:

/*  From Techie.Click
    By JP  */
import java.util.Scanner;
public class AreaCircleInput
{
	public static void main(String[] args) {
	    Scanner input = new Scanner(System.in);
	    double area=0, radius=0;
	    final double PI = 3.1416;
	    System.out.print("Input value for radius: ");
	    radius = input.nextDouble();
	    area = PI * radius * radius;
	    System.out.println("The area of the circle is: "
			+ area);
	}
}

Discussion – Compute Area of a Circle using Java Programming

Line 1: Import the Scanner class from java.util
Line 2: Declaration of the Class named AreaCircleInput – note, filename should be AreaCircleInput.java also
Line 3: The opening curly brace, signifies the beginning of the content of the class – start of the scope of Class AreaCircleInput
Line 4: Declaration of the main method.
* “public” is the access modifier – making it accessible with other class,
* “static” means that the main method can be accessed without creating an object of the class AreaCircleInput;
* “void” – the data type of the method (its return value) –void means that the method will not return anything
* “String[]” the data type of the array args — [] means its an array
* “args” the array that will hold the values you may pass upon the execution of your class
Line 5: Instantiation of the object input of the class Scanner
* Scanner input – means input is a type of Scanner
* = assignment operator
* new – keyword to create a new object / new instance of the class
* Scanner(System.in) – invocation of the constructor of class Scanner with System.in as its argument
Line 6: Declaration and initialization of variables area and radius
* the variables was declared as double (its data type)
* the variables was initialized with a value of 0
Line 7: Declaration and initialization of a constant PI with its value 3.1416
Line 8: Display on screen “Input value for radius: ”
Line 9: Get user input from console, convert it into appropriate value using the input object and assign it to variable radius
Line 10: multiply the value of constant PI to radius and multiple its result to radius – this is actually PI * r^2 and assign the result to variable area
Line 11: Display the result
* println was used – this will move the cursor in the console to a new line after printing
* the “+” operator here is a string concatenation operator – an operator that merge two strings together. Note: the “+” operator changes its purpose depending on the value on its operand
Line 13: The closing brace of the method main
Line 14: The closing brace of the class AreaCircleInput
Note: each statement should end with a semi-colon “;”

Compute Area of a Circle using Java Programming

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.