In this article we will show you, How to write Java Program to find Volume and Surface Area of Cube with example. Before we step into the program, Let see the definitions and formulas behind Surface area of a Cube and Volume of a Cube

## CUBE

All the edges in the Cube has same length. We can say, Cube is nothing but 6 equal squares.

### Surface Area of a Cube

If we know the length of any edge in Cube then, we can calculate the surface area of a Cube using the formula:

- Surface Area of a Cube = 6l² (Where l is the Length of any side of a Cube).
- Area of a square = l² Since the Cube is made of 6 equal squares, Surface Area of a Cube = 6l²

If we already know the Surface Area of Cube and then we can calculate the length of any side by altering the above formula as:

- l = √sa / 6 (sa = Surface Area of a Cube)

**Volume of a Cube**

The amount of space inside the Cube is called as Volume. If we know the length of any edge of a Cube then we can calculate the Volume of Cube using formula:

- Volume = l * l * l
- The Lateral Surface Area of a Cube = 4 * (l * l)

## Java Program to find Volume and Surface Area of Cube

This Java program allows user to enter any side of a Cube. Using this value, this program will calculate the Surface Area of a cube, Volume of a cube and Lateral Surface Area of a Cube as per the formulas.

**JAVA CODE**

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 |
// Java Program to find Volume and Surface Area of Cube package SurfaceAreaPrograms; import java.util.Scanner; public class VolumeOfCube { private static Scanner sc; public static void main(String[] args) { // LSA = Lateral Surface Area of a Cube, SA = Surface Area float length, SA,Volume, LSA; sc = new Scanner(System.in); System.out.println("\n Please Enter the Length of any side of a Cube : "); length = sc.nextFloat(); SA = 6 * (length * length); Volume = length * length * length; LSA = 4 * (length * length); System.out.format("\n The Surface area of a Cube = %.2f", SA); System.out.format("\n The Volume of a Cube = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cube = %.2f", LSA); } } |

**OUTPUT**

**ANALYSIS**

Following statement will allow the User to enter the length of any side of a cube and then we are assigning the user entered value to already declared variable called length.

1 2 |
System.out.println("\n Please Enter the Length of any side of a Cube : "); length = sc.nextFloat(); |

Next, we are using the Mathematical Formula to calculate the surface area of a Cube

1 |
SA = 6 * (length * length); |

In the next line, We are calculating the volume of a Cube

1 |
Volume = length * length * length; |

In the next line, We are calculating the Lateral Surface Area of a Cube

1 |
LSA = 4 * (length * length); |

Following System.out.format statements will help us to print the Volume of a Cube, Lateral Surface Area of a Cube and Surface Area of a Cube

1 2 3 |
System.out.format("\n The Surface area of a Cube = %.2f", SA); System.out.format("\n The Volume of a Cube = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cube = %.2f", LSA); |

From the above screenshot you can observe that, We have entered the Length of any side of a Cube = 6

The **Surface Area of a Cube** is

Surface Area of a cube = 6 * length * length

Surface Area of a cube = 6 * 6 * 6

Surface Area of a cube = 216

The **Volume of a Cube** is

Volume of a cube = length * length * length

Volume of a cube = 6 * 6 * 6;

Volume of a cube = 216

The **Lateral Surface Area of a Cube** is

Lateral Surface Area of a cube = 4 * length * length

Lateral Surface Area of a cube = 4 * 6 * 6

Lateral Surface Area of a cube = 144

In the above example we got Surface area = 216 when the length = 6. Let us do the reverse approach (Calculate the length of a Cube using Surface Area)

Length of a cube = √sa / 6

Length of a cube = √216 / 6

Length of a cube = √36

Length of a cube = 6

## Java Program to find Volume and Surface Area of Cube using Functions

This Java program allows the user to enter the value of a radius and height. Using these values, we will calculate the Volume of a Cube, Lateral Surface Area of a Cube and Surface Area of a Cube as per the formula. In this example, we are going to use the logic that we specified in first example but we will separated the logic and place it in a method.

**JAVA CODE**

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 |
package SurfaceAreaPrograms; import java.util.Scanner; public class VolumeOfCubeUsingMethods { private static Scanner sc; public static void main(String[] args) { // LSA = Lateral Surface Area of a Cube, SA = Surface Area float length; sc = new Scanner(System.in); System.out.println("\n Please Enter the Length of any side of a Cube : "); length = sc.nextFloat(); VolumeOfCube (length); } public static void VolumeOfCube (float length) { float SA,Volume, LSA; SA = 6 * (length * length); Volume = length * length * length; LSA = 4 * (length * length); System.out.format("\n The Surface area of a Cube = %.2f", SA); System.out.format("\n The Volume of a Cube = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cube = %.2f", LSA); } } |

**OUTPUT**

## Java Program to find Volume and Surface Area of Cube using Oops

In this Java program, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a methods.

**JAVA CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package SurfaceAreaPrograms; public class VolumeOfaCube { float sa, Volume, LSA; public float VolumeOfCube (float length) { Volume = length * length * length; return Volume; } public float SurfaceAreaOfCube (float length) { sa = 6 * (length * length); return sa; } public float LateralSurfaceAreaOfCube(float length) { LSA = 4 * (length * length); return LSA; } } |

Within the Main program, we will create an instance of the above specified class and call the methods.

**JAVA CODE**

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 |
package SurfaceAreaPrograms; import java.util.Scanner; public class VolumeOfCubeUsingClass { private static Scanner sc; public static void main(String[] args) { // LSA = Lateral Surface Area of a Cube, SA = Surface Area float length, SA,Volume, LSA; sc = new Scanner(System.in); System.out.println("\n Please Enter the Length of any side of a Cube : "); length = sc.nextFloat(); VolumeOfaCube vs = new VolumeOfaCube(); SA = vs.SurfaceAreaOfCube(length); Volume = vs.VolumeOfCube(length); LSA = vs.LateralSurfaceAreaOfCube(length); System.out.format("\n The Surface area of a Cube = %.2f", SA); System.out.format("\n The Volume of a Cube = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cube = %.2f", LSA); } } |

**OUTPUT**

**ANALYSIS**

**VolumeofACube Class Analysis:**

- First we declared a function
**VolumeofCube**with one argument. Within the function, we are calculating the Volume of a Cube and returning the value - Next, we declared a function
**Surface****AreaofCube**with one argument. Within the function, we are calculating the Surface Area of a Cube and returning the value - Next we declared a function
**Lateral****Surface****AreaofCube**with one argument. Within the function, we are calculating the Lateral Surface Area of a Cube and returning the value

**TIP:** If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

**Main Class Analysis:**

First we created an instance / created an Object of the **VolumeofACube** Class

1 |
VolumeOfaCube vs = new VolumeOfaCube(); |

Next, we are calling the **VolumeofCube** method. This is the first method that we created with double data type and this method will calculate Volume of a Cube and return a value so, we are assigning the return value to volume variable.

1 |
Volume = vs.VolumeOfCube(length); |

Next, we are calling the **Surface****AreaofCube** method. This is the second method that we created with double data type and this method will calculate Surface Area of a Cube and return a value so, we are assigning the return value to SA variable.

1 |
SA = vs.SurfaceAreaOfCube(length); |

Next, we are calling the **Lateral****Surface****AreaofCube** method. This is the third method that we created with double data type and this method will calculate Lateral Surface Area of a Cube and return a value so, we are assigning the return value to L variable.

1 |
L = vac.LateralSurfaceAreaOfCylinder(radius, height); |

Lastly we used following System.out.format statement to print the Volume of a Cube, Lateral Surface Area of a Cube and Surface Area of a Cube

1 2 3 |
System.out.format("\n The Surface area of a Cube = %.2f", SA); System.out.format("\n The Volume of a Cube = %.2f", Volume); System.out.format("\n The Lateral Surface area of a Cube = %.2f", LSA); |

Thank You for Visiting Our Blog

## Leave a Reply