Java program to calculate the volume of a cube – In this distinct piece, we will talk more about the different ways to calculate the volume of a cube in Java Programming.
Fitting examples and sample programs have been added for the sake of greater comprehension for interested people. The compiler has also been added with which you can execute yourself.
The means with which the volume of a cube is calculated in this piece in Java programming are as follows:
- Using Scanner Class
- Using Command Line Arguments
- Using Static Function
As we all know, a cube is a three-dimensional figure which is used every day in our daily life. A cube is just an extrapolation of a square by giving it a height of the same length.
All the sides of a cube are equal and the opposite sides are parallel. All the angles in a cube are equal to 90 degrees.
This is what a cube looks like:
As you can see, this is a cube with a side of 4 units. The volume of a cube can be calculated with this formula:
Volume = Side*Side*Side
Hence, the volume of this cube is as follows:
Volume = 4*4*4 = 64 cubic units.
Thus, the multitude of ways to calculate the volume of a cube in Java Programming is as follows:
Using Scanner Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; class VolumeOfCube { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the side of cube:"); double side=s.nextDouble(); double volume=side*side*side; System.out.println("volume of Cube is: " +volume); } } |
1 2 3 4 |
Enter the side of cube: 5 volume of Cube is: 125.00 |
Using Command Line Arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Scanner; class VolumeOfCube1 { public static void main(String args[]) { double side=Double.parseDouble(args[0]); double volume=side*side*side; System.out.println("volume of Cube is: " +volume); } } |
1 |
parse(): |
Using Static Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.Scanner; class VolumeOfCube { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the side of cube:"); double side=s.nextDouble(); double a=VolumeOfCube.Volume(side); System.out.println("Volume Of Cube is:" +a); } public static double Volume(double side) { double a=side*side*side; return a; } } |
1 2 3 4 |
Enter the side of cube: 6 volume of Cube is: 216.00 |