Hello guys, welcome to Lingarajtechhub. Today we are going to discuss how to find the volume of cube using Scanner class
Code:
package com.Arithmatic_operator.java; import java.util.Scanner; public class Volume_Cube { public static void main(String[] args) { double volume,edge; Scanner sc=new Scanner(System.in); System.out.println("Entr the edge of a cube "); edge=sc.nextDouble(); volume=edge*edge*edge; System.out.println("The volume of a cube "+volume ); } }
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 Volume_Cube
–> Here Volume_Cube is defined as a class which is a template used to create objects and to define object data types and methods .
–> And is defined as public because it accesses anywhere.
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 volume,edge;
–> Here we Declare 2 variable volume,edge 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(“Entr the edge of a cube “);
–> It means System is a class name and out is an instance of the System println is print a new line
Line-10:
edge=sc.nextDouble();
–> Here edge 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-11:
volume=edge;