Java Program to find Volume and Surface Area of Cuboid

Write Java Program to find Volume and Surface Area of Cuboid with example. Before we step into the Java Program to find Volume and Surface Area of Cuboid, Let see the definitions and formulas behind the Area of Top & Bottom Surfaces, Surface area of a Cuboid, Lateral Surface Area and Volume of a Cuboid

Java Cuboid

A Cuboid is a 3D object made up of 6 Rectangles. All the opposite faces (i.e., Top and Bottom) are equal.

Java Surface Area of a Cuboid

The Total Surface Area of a Cuboid is the sum of all the six rectangles areas present in the Cuboid. If we know the length, width, and height of the Cuboid then we can calculate the Total Surface Area using the formula:

  • Area of Top & Bottom Surfaces = lw + lw = 2lw
  • Area of Front & Back Surfaces = lh + lh = 2lh
  • The Area of both sides = wh + wh = 2wh

The Total Surface Area of a Cuboid is the sum of all the 6 faces. So, we have to add all these areas to calculate the final Surface Area

  • Total Surface Area of a Cuboid = 2lw + 2lh + 2wh
  • It is equal: Total Surface Area = 2 (lw + lh +wh)

The Java volume of a Cuboid

The amount of space inside the Cuboid is called Volume. If we know the length, width, and height of the Cuboid then we can calculate the volume using the formula:

  • Volume of a Cuboid = Length * Breadth * Height
  • The volume of a Cuboid = lbh
  • The Lateral Surface Area of a Cuboid = 2h (l + w).

Java Program to find Volume and Surface Area of Cuboid

This Java program allows users to enter the length, width, and height of a Cuboid. Next, the Java program will calculate the Volume, Surface Area, and Lateral Surface Area of Cuboid as per the formulas.

// Java Program to find Volume and Surface Area of Cuboid
package SurfaceAreaPrograms;

import java.util.Scanner;

public class VolumeOfCuboid {
	private static Scanner sc;

	public static void main(String[] args) {
		// LSA = Lateral Surface Area of a Cube, SA = Surface Area
		float length, width, height, SA, Volume, LSA;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter the Length of a Cuboid : ");
		length = sc.nextFloat();
		System.out.println("\n Please Enter the Width of a Cuboid : ");
		width = sc.nextFloat();
		System.out.println("\n Please Enter the Height of a Cuboid : ");
		height = sc.nextFloat();
		
		SA = 2 * (length * width + length * height + width * height);
		Volume = length * width * height;
		LSA = 2 * height * (length + width);
		
		System.out.format("\n The Surface area of a Cuboid = %.2f", SA);
		System.out.format("\n The Volume of a Cuboid = %.2f", Volume);
		System.out.format("\n The Lateral Surface area of a Cuboid = %.2f", LSA);
	}
}
Java Program to find Volume and Surface Area of Cuboid 1

The following statement will allow the user to enter the length, width, and height of a cuboid. Then we are assigning the user entered values to already declared a variable called length, width, and height.

System.out.println("\n Please Enter the Length of a Cuboid : ");
length = sc.nextFloat();
System.out.println("\n Please Enter the Width of a Cuboid : ");
width = sc.nextFloat();
System.out.println("\n Please Enter the Height of a Cuboid : ");
height = sc.nextFloat();

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

SA = 2 * (length * width + length * height + width * height);

In the next Java line, We are calculating the volume of a Cuboid

Volume = length * width * height;

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

LSA = 2 * height * (length + width);

The following System.out.format statements will help us to print the Volume of a Cuboid, Lateral Surface Area of a Cuboid, and Surface Area of a Cuboid

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

From the above Java Program to find Volume and Surface Area of Cuboid screenshot, you can observe that we have entered the Values Length = 10, Width = 6, and Height = 8.

The Volume of a Cuboid for the specified Measures are:

Volume of a Cuboid = lbh = l * w * h
The Volume of a Cuboid = length * width * height
Volume of a Cuboid = 10 * 6 * 8
Volume of a Cuboid = 480
The Volume of a Cuboid is 480

The Total Surface Area of a Cuboid for the specified Measures are:

Total Surface Area of a Cuboid = 2lw + 2lh + 2wh ==> 2 (lw + lh +wh)
Total Surface Area of a Cuboid = 2* (length * width + length * height + width * height)
The Total Surface Area of a Cuboid = 2 * ( (10 * 6) + (10 * 8) + (6 * 8) )
Total Surface Area of a Cuboid = 2 * (60 + 80 + 48) ==> 2 * (188) ==> 376
The Total Surface Area of a Cuboid is 376

The Lateral Surface Area of a Cuboid for the specified Measures are:

Lateral Surface Area of a Cuboid = 2lh + 2wh ==> 2h (l + w)
Lateral Surface Area of a Cuboid = 2 * height * (length + width)
The Lateral Surface Area of a Cuboid = 2 * 8 * (10 + 6)
Lateral Surface Area of a Cuboid = 2 * 8 * (16 ) ==> 256
The Lateral Surface Area of a Cuboid is 156

Java Program to find Volume and Surface Area of Cuboid using Functions

This Java program allows users to enter the length, width, and height of a Cuboid. Next, the Java program will calculate the Volume, Surface Area, and Lateral Surface Area of Cuboid as per the formulas. 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 VolumeOfCuboidUsingMethods {
	private static Scanner sc;

	public static void main(String[] args) {
	private static Scanner sc;

	public static void main(String[] args) {
		// LSA = Lateral Surface Area of a Cube, SA = Surface Area
		float length, width, height;
		sc = new Scanner(System.in);
		
		System.out.println(" Please Enter the Length, Width & Height of a Cuboid : ");
		length = sc.nextFloat();
		width = sc.nextFloat();
		height = sc.nextFloat();
		VolumeOfCuboid (length, width, height); 	

	}
	public static void VolumeOfCuboid (float length, float width, float height) {
		float SA,Volume, LSA;
		SA = 2 * (length * width + length * height + width * height);
		Volume = length * width * height;
		LSA = 2 * height * (length + width);
		
		System.out.format("\n The Surface area of a Cuboid = %.2f", SA);
		System.out.format("\n The Volume of a Cuboid = %.2f", Volume);
		System.out.format("\n The Lateral Surface area of a Cuboid = %.2f", LSA);
	}
}

Java Volume and Surface Area of Cuboid output

 Please Enter the Length, Width & Height of a Cuboid : 
8 5 6

 The Surface area of a Cuboid = 236.00
 The Volume of a Cuboid = 240.00

Java Program to find Volume and Surface Area of Cuboid 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 VolumeOfaCuboid {
	float SA, Volume, LSA;
	
	public float VolumeOfCuboid (float length, float width, float height) {
		Volume = length * width * height;
		return Volume;
	}
	
	public float SurfaceAreaOfCuboid (float length, float width, float height) {
		SA = 2 * (length * width + length * height + width * height);
		return SA;
	}
	
	public float LateralSurfaceAreaOfCuboid(float length, float width, float height) {
		LSA = 2 * height * (length + width);
		return LSA;
	}
}

Within the Main Java Program to find Volume and Surface Area of Cuboid program, we will create an instance of the above-specified class and call the methods.

package SurfaceAreaPrograms;

import java.util.Scanner;

public class VolumeOfCuboidUsingClass {
	private static Scanner sc;

	public static void main(String[] args) {
		// LSA = Lateral Surface Area of a Cube, SA = Surface Area
		float length, width, height, SA, Volume, LSA;
		sc = new Scanner(System.in);
		
		System.out.println(" Please Enter the Length, Width & Height of a Cuboid : ");
		length = sc.nextFloat();
		width = sc.nextFloat();
		height = sc.nextFloat();
		
		VolumeOfaCuboid vcd = new VolumeOfaCuboid();
		SA = vcd.SurfaceAreaOfCuboid(length, width, height);
		Volume = vcd.VolumeOfCuboid(length, width, height);
		LSA = vcd.LateralSurfaceAreaOfCuboid(length, width, height);
		
		System.out.format(" The Surface area of a Cuboid = %.2f", SA);
		System.out.format("\n The Volume of a Cuboid = %.2f", Volume);
		System.out.format("\n The Lateral Surface area of a Cuboid = %.2f", LSA);
	}
}
 Please Enter the Length, Width & Height of a Cuboid : 
7 8 11

 The Surface area of a Cuboid = 442.00
 The Volume of a Cuboid = 616.00
 The Lateral Surface area of a Cuboid = 330.00

VolumeofACuboid Class Analysis:

  1. First, we declared a function VolumeofCuboid with three arguments. Within the Java Program to find Volume and Surface Area of Cuboid function, we are calculating the Volume of a Cuboid and returning the value.
  2. Next, we declared a function SurfaceAreaofCuboid with three arguments. Within the function, we are calculating the Surface Area of a Cuboid and returning the value.
  3. Next, we declared a function LateralSurfaceAreaofCuboid with three arguments. Within the function, we are calculating the Lateral Surface Area of a Cuboid and returning the value.

Main Class Analysis:

In this Java Program to find Volume and Surface Area of Cuboid, we created an instance / created an Object of the VolumeofACuboid Class

VolumeOfaCuboid vcd = new VolumeOfaCuboid();

Next, we are calling the VolumeofCuboid method by passing three arguments. It is the first method that we created with the double data type. This method will calculate the Volume of a Cuboid and return a value. So, we are assigning the return value to the volume variable.

Volume = vcd.VolumeOfCuboid(length, width, height);

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

SA = vcd.SurfaceAreaOfCuboid(length, width, height);

Here, we are calling the LateralSurfaceAreaofCuboid method. It is the third method that we created with a double data type, and this method will calculate the Lateral Surface Area of a Cuboid and return a value. So, we are assigning the return value to the LSA variable.

LSA = vcd.LateralSurfaceAreaOfCuboid(length, width, height);

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

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