Home >Java >javaTutorial >Java Program to Find the Volume of Cylinder

Java Program to Find the Volume of Cylinder

DDD
DDDOriginal
2025-02-07 11:11:08603browse

Java Program to Find the Volume of Cylinder

The cylinder is a three-dimensional geometric shape with two parallel circular base surfaces connected by curved surfaces. The volume of a cylinder can be calculated using mathematical formulas that consider its radius and height.

Problem Description

In this tutorial, we will discuss how to calculate the volume of a given cylinder in Java using different methods. Cylinder volume formula

The formula for the volume of the cylinder is as follows:

Cylinder volume = π × r² × h

Of:

  • r: The radius of the circular base surface.
  • h: The body height of the cylinder.

Example 1

<code>**输入:**
半径 = 5 个单位
高度 = 10 个单位
**输出:**
体积 = 785.4 立方单位
**说明:**
使用公式计算体积:
体积 = π × 5² × 10
体积 = 785.4 立方单位</code>

Example 2

<code>**输入:**
半径 = 7 个单位
高度 = 15 个单位
**输出:**
体积 = 2309.4 立方单位
**说明:**
使用公式计算体积:
体积 = π × 7² × 15
体积 = 2309.4 立方单位</code>

How to calculate the volume of a cylinder in Java?

The following are different ways to calculate the volume of a cylinder in Java:

  • Direct formula method
  • Using function

Use direct formula method

We use direct formula method in Java to calculate the volume of a cylinder:

Volume = π × r² × h

Implementation steps

  • Prefer radius and height as input parameters.
  • Use formulas to calculate volume.
  • Print the result.

Implementation code

<code class="language-java">import java.text.DecimalFormat;

public class CylinderVolume {
    public static void main(String[] args) {
        double radius = 5;
        double height = 10;
        double volume = Math.PI * Math.pow(radius, 2) * height;
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println("半径为 " + radius + ",高度为 " + height + " 的圆柱体的体积是: " + df.format(volume) + " 立方单位");
    }
}</code>

Output:

<code>半径为 5.0,高度为 10.0 的圆柱体的体积是: 785.40 立方单位</code>

Time complexity:

O(1)

Space complexity:

O(1)

Using function

In this method, we encapsulate the logic of calculating the volume of the cylinder into a reusable function.

Implementation steps

  • Define a function that uses formula to calculate the volume of a cylinder.
  • Pass the input value (radius and height) to the function.
  • Return the result and print it.

Implementation code

<code class="language-java">import java.text.DecimalFormat;

public class CylinderVolumeFunction {
    static double calculateVolume(double radius, double height) {
        return Math.PI * Math.pow(radius, 2) * height;
    }

    public static void main(String[] args) {
        double radius = 5;
        double height = 10;
        double volume = calculateVolume(radius, height);
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println("半径为 " + radius + ",高度为 " + height + " 的圆柱体的体积是: " + df.format(volume) + " 立方单位");
    }
}</code>

Output:

<code>半径为 5.0,高度为 10.0 的圆柱体的体积是: 785.40 立方单位</code>

Time complexity:

O(1)

Space complexity:

O(1)

Using these methods, you can easily calculate the volume of cylinders in Java while keeping your code simple and modular. Choose the method that best suits your needs!

The above is the detailed content of Java Program to Find the Volume of Cylinder. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn