Java Program to find Volume and Surface Area of Cube

Write Java Program to find Volume and Surface Area of Cube with example. Before we step into the Java Program to find Volume and Surface Area of Cube, Let see the definitions and formulas behind the Surface area of a Cube and Volume of a Cube

Java Cube Program

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

Surface Area of a Cube

If we know the length of an edge in a 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 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)

The volume of a Java Cube

The amount space inside the Cube is called Volume. If we know the length of any edge of a Cube, then we can calculate the Volume of Cube using the 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 users to enter any side of a Cube. Next, 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 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);
	}
}
Java Program to find Volume and Surface Area of Cube 1

In this Java Program to find the Volume and Surface Area of Cube example, the following statement will allow the user to enter the length of any side of a cube. Then we are assigning the user entered value to an already declared variable called length.

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

SA = 6 * (length * length);

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

Volume = length * length * length;

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

LSA = 4 * (length * length);

The 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

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 Java Program to find Volume and Surface Area of Cube screenshot, 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 = 6 * 6 * 6
Surface Area of a cube = 216

The Volume of a Cube is
Volume of a cube = length * length * length = 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 = 4 * 6 * 6
Lateral Surface Area of a cube = 144

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

Length of a cube = √sa / 6 = √216 / 6
Length of a cube = √36 = 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 the first example. However, we will separate the logic and place it in a method.

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);
	}
}

Java Volume and Surface Area of Cube Output

 Please Enter the Length of any side of a Cube : 
5

 The Surface area of a Cube = 150.00
 The Volume of a Cube = 125.00
 The Lateral Surface area of a Cube = 100.00

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 that holds methods.

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 Java Program to find Volume and Surface Area of Cube program, we will create an instance of the above specified class and call the methods.

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);
	}
}
 Please Enter the Length of any side of a Cube : 
7

 The Surface area of a Cube = 294.00
 The Volume of a Cube = 343.00
 The Lateral Surface area of a Cube = 196.00

VolumeofACube Class Analysis:

  1. First, we declared a function VolumeofCube with one argument. Within the function, we are calculating the Volume of a Cube and returning the value.
  2. Next, we declared a function SurfaceAreaofCube, with one argument. Within the function, we are calculating the Surface Area of a Cube and returning the value.
  3. Next, we declared a function LateralSurfaceAreaofCube with one argument. Within the function, we are calculating the Lateral Surface Area of a Cube and returning the value.

Main Class Analysis:

Within this Java Program to find Volume and Surface Area of Cube, we created an instance / created an Object of the VolumeofACube Class

VolumeOfaCube vs = new VolumeOfaCube();

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

Volume = vs.VolumeOfCube(length);

Next, we are calling the SurfaceAreaofCube method. It is the second method that we created with the double data type, and it will calculate the Surface Area of a Cube and return a value. So, we are assigning the return value to the SA variable.

SA = vs.SurfaceAreaOfCube(length);

Next, we are calling the LateralSurfaceAreaofCube method. It is the third method that we created with a double data type, and it calculates the Lateral Surface Area of a Cube and returns a value. So, we are assigning the return value to the L variable.

L = vac.LateralSurfaceAreaOfCylinder(radius, height);

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

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);