Find the Volume of a Capsule in Java



A capsule is a three-dimensional geometric figure that consists of a cylindrical body with hemispherical ends on both sides. The volume of a capsule can be calculated by adding the volume of the cylindrical part and the volume of the two hemispherical ends present on both sides of the cylindrical part. In this tutorial, we are going to discuss how to find the volume of a given capsule in Java using different approaches.

Formula for Volume of Capsule

The formula for the volume of a capsule is as follows:

Volume of Capsule = Volume of cylinder + Volume of both & hemispheres

Where,
r: The radius of the hemispherical ends.
h: The height of the cylindrical body (excluding the hemispherical ends).

Example 1

Input

Radius = 5 units
Height = 10 units

Output

Volume = 1570.8 cubic units

Explanation

Using the formula to calculate the volume:

Volume = ? × r2 × h + (4/3) × ? × r3
Volume = 785.4 + 523.6 Volume = 1570.8 cubic units

Example 2

Input

Radius = 7 units
Height = 15 units

Output

Volume = 4311.97 cubic units

Explanation

Using the formula to calculate the volume:

Volume = ? × r2 × h + (3/4) × ? × r3
Volume = 2309.4 + 2002.57 cubic units
Volume = 4311.97cubic units

How to Find the Volume of Capsule in Java?

Below are different approaches to calculate the volume of a capsule in Java:

Find Volume of Capsule Using Direct Formula Approach

We use the direct formula approach to calculate the volume of the capsule in Java. The formula for the volume of a capsule is: Volume = ? × r2 × h + (3/4) × ? × r3.

Steps for Implementation

  • Take the radius and height as input parameters.
  • Calculate the volume using the formula.
  • Print the result.

Implementation Code

import java.lang.Math;

public class CapsuleVolume {
	public static void main(String[] args) {
		double radius = 5;
		double height = 10;

		double volume = Math.PI * Math.pow(radius, 2) * height + (4.0 / 3) * Math.PI * Math.pow(radius, 3);

		System.out.printf("The volume of the capsule with radius %.1f and height %.1f is: %.2f cubic units\n", radius, height, volume);
	}
}

Output

The volume of the capsule with radius 5.0 and height 10.0 is: 1309.00 cubic units.

Time Complexity: O(1)

Space Complexity: O(1)

Find Volume of Capsule Using Function

We will use a function to calculate the volume of the capsule. The logic and formula for calculating the volume remain the same, but we encapsulate the calculation in a reusable function.

Steps for Implementation

  • Define a function to calculate the volume of the capsule using the formula.
  • Pass the input values (radius and height) to the function.
  • Return the result and print it.

Implementation Code

import java.lang.Math;

public class CapsuleVolume {

	public static double calculateVolume(double radius, double height) {
		double cylindricalVolume = Math.PI * Math.pow(radius, 2) * height;
		double hemisphericalVolume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
		return cylindricalVolume + hemisphericalVolume;
	}

	public static void main(String[] args) { 
		double radius = 5;
		double height = 10;

		double volume = calculateVolume(radius, height);

		System.out.printf("The volume of the capsule with radius %.1f and height %.1f is: %.1f cubic units\n", radius, height, volume);
	}
}

Output

The volume of the capsule with radius 5.0 and height 10.0 is: 1309.0 cubic units.

Time Complexity: O(1)

Space Complexity: O(1)

Updated on: 2025-01-22T11:07:50+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements