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

Java Program to Find the Volume of Cylinder

DDD
Release: 2025-02-07 11:11:08
Original
608 people have browsed it

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>
Copy after login

Example 2

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

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

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) + " 立方单位");
    }
}
Copy after login

Output:

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

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

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) + " 立方单位");
    }
}
Copy after login

Output:

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

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!

Related labels:
Statement of this Website
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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template