Hello guys, welcome to Lingarajtechhub. Today we are going to discuss finding the area of cylinder by using Scanner class in java.
Code:
package com.Arithmatic_operator.java; import java.util.Scanner; public class Area_of_Cyllinder { public static void main(String[] args) { double area,pi=3.141,rad,hgt; Scanner sc=new Scanner(System.in); System.out.println("Enter value of cylinder radius"); rad=sc.nextDouble(); System.out.println("Enter va;ue of cylinder height"); hgt=sc.nextDouble(); area=2*(pi*rad*hgt)+(pi*rad*rad); System.out.println("The area of cylinder is "+area); } }
Line-1:
package com.Arithmatic_operator.java;
–> It is a java package( com.Arithmatic_operator.java) which is collection of classes and this package is created in java project.
Line-2:
import java.util.Scanner;
–> Scanner is a class in java. util package used for obtaining the input of the primitive types like int, double, etc. and strings.
Line-3:
public class Area_of_Cyllinder
–>Here Area_of_Cyllinder is defined as a class which is a template used to create objects and to define object data types and methods.
–> And its define as public because it access any where.
Line-5:
public static void main(String[] args)
–> It is a main method in which we can execute a program
–>public :it is a access modifier of the main function which is must be public so that the JRE which is (Java Run Time Environment)can access and execute this method.
–> If a method is not public, then access is restricted and its showing error
–> static is a keyword in which main method has must to be static in which JVM can load the class into memory and call the main method without creating an instance
–> void it means no return type
–> main() is function
–> string[]args : string means collection of charecters ,String[] args represents a collection of Strings that are separated by a space and can be typed into the program on the terminal directly
Line-7:
double area,pi=3.141,rad,hgt;
–>Here we Declare 4 variable area ,pi (which value is 3.141 is assih=gn to pi),rad,hgt which is double type
Line-8:
Scanner sc=new Scanner(System.in);
–>here scanner is a class and Sc is the instance of the object which is new Scanner.System.in is an argument
Line-9:
System.out.println(“Enter value of cylinder radius”);
–>It means System is a class name and out is an instance of the System println is print a new line
Line-10:
rad=sc.nextDouble();
–> Here rad is a variable that we take and sc is our instance name which is create for a object and nextDouble() is a method method of a Scanner object reads in a string of digits or char and converts them into a double type
Line-13:
area=2(pi*rad*hgt)+(pirad*rad);
–>here we apply the logic to find the area of cylinder,the formula is 2(piradhgt)+(pi*(rad*rad)),after find this value it assign to area value