area of a circle in java

area of a circle in java

Java program  to calculate  area of  a circle


Write a program to calculate the area of a circle by entering the radius. Formula: area=PI*r*r.

Logic:
To calculate the area of a circle we use formula area= 22/7 * r*r. For this, we input the radius value and PI value from Math.PI.

/*
        Calculate Circle Area using Java Example
        This Calculate Circle Area using Java Example shows how to calculate
        area of a circle using its radius. 
        This program will help for ICSE, ISC and BCA students
*/
 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 public class CalculateCircleAreaExample
{
         public static void main(String[] args)
        {
              
                int radius = 0;                            //Local variable
                System.out.println("Please enter radius of a circle");
              
                try
                {
                        //get the radius from a console
                     InputStreamReader in=new InputStreamReader(System.in);
                      BufferedReader br = new BufferedReader(in);
                        radius = Integer.parseInt(br.readLine());
                }
                //if an invalid value was entered
                catch(NumberFormatException ne)
                {
                        System.out.println("Invalid radius value" + ne);
                        System.exit(0);
                }
                catch(IOException ioe)
                {
                        System.out.println("IO Error :" + ioe);
                        System.exit(0);
                }
              
                /*
                 * Area of a circle is
                 * Formula area=pi * r * r
                 * where r is a radius of a circle.
                 */
              
                // NOTE: use Math.PI constant to get the value of Pi
                double area = Math.PI * radius * radius;
               // Print the value of the area of a circle
                System.out.println("Area of a circle is " + area);
        }
}


Output:
Please enter radius of a circle
12

Area of a circle is 452.3893421169302

Sl. No.
Variable Name
Data Type
Purpose
1
radius
double
Store the radius value
2
area
double
Store the area of a circle
3
main()
static void
Main function
4
in
InputStreamReader
To instantiate the InputStreamReader class which exists in java.io package
5
br
BufferedReader
To instantiate the BufferedReader class which exists in java.io package using the above variable as the passing parameter


ICSE Computer Question Answer

ICSE Computer Question Answer

ICSE Computer Question Answer

Q. a) State the difference between token and identifier, variable.
Ans. A token is the smallest individual unit in a program for e.g.: Keyword, Identifiers, Literals etc. where as an identifier is the name given to different parts of a program e.g. variable, functions, classes etc.
Variable: A memory location that stores a value is known as variable. E.g. int a;
    b) What is variable. Give an example.
Ans. Place holder of data in computer memory. E.g. Int a,b,c;
Q. Define Byte code and JVM.
Ans. Byte code is the compiled format for Java programs. Once a Java program has been converted to byte code, it can be transferred across a network and executed by Java Virtual Machine (JVM). Byte code files generally have a .class extension
Ans: JVM is a platform-independent execution environment that converts Java byte code into machine language and executes it. Most programming languages compile source code directly into machine code that is designed to run on a specific microprocessor architecture or operating system, such as Windows or UNIX. A JVM -- a machine within a machine -- mimics a real Java processor, enabling Java byte code to be executed as actions or operating system calls on any processor regardless of the operating system.
Q. Example the term Object and Class using an example. 
Ans. Object: An instance of a class called Object. Table is an instance of class Furniture.
Class: Blue print of an object is called Class. Example, mango, apple and orange are members of the class fruit.
Q. Name four OOP’S principles.
Ans. Abstraction, Encapsulation, Polymorphism, Inheritance
Abstraction: Abstraction refers to the act of representating essential features without including the background details or explanations.
Encapsulation: The wrapping up of data and methods into a single unit is called Encapsulation.
Polymorphism: polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results.
Inheritance: Inheritance is the process by which objects of one class acquire the properties of objects of anther class. Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.
Q. Mention any two attributes required for class declaration.
Ans: Variables and function or state and behaviour.
Q. Differentiate between base and derived class.
The base class is a class whose properties will be inherited and the derived class is a class which inherits the base class. E.g. Of base class is Super and derived class is sub.
Q. What is data types?
A data type in a programming language is a set of data with values having predefined characteristics. Examples of data types are: int, float,char etc.
The following chart summarizes the default values for the above data types.
Data Type
Default Value (for fields)
byte
0
short
0
int
0
long
0L
float
0.0f
double
0.0d
char
'\u0000'
String (or any object)  
null
boolean
false