
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Volume of Hemisphere in Java
A hemisphere refers to the exact half of a sphere. Means if we divide a sphere in two equal parts then we will get two hemispheres. Hemisphere is a three-dimensional geometrical shape which has one flat face.
The amount of space occupied by the hemisphere is called the volume of the hemisphere.
Formula to calculate volume of hemisphere ?
Mathematically it can be represented as
Volume=(2πr2)/3
Mathematically it can be represented as
Volume = (2 * pi * r * r) / 3
Where, ?r' refers to the radius of the hemisphere.
In this article we will see how we can find the volume of the hemisphere by using Java programming language.
To show you some instances
Instance-1
Suppose radius(r) of hemisphere is 4.5
Then by using volume formula of hemisphere
Volume = 190.851
Hence, the volume of hemisphere is 190.851
Instance-2
Suppose radius(r) of hemisphere is 3
Then by using volume formula of hemisphere
Volume = 56.548
Hence, the volume of hemisphere is 56.548
Instance-3
Suppose radius(r) of hemisphere is 6
Then by using volume formula of hemisphere
Volume = 452.389
Hence, the volume of hemisphere is 452.389
Syntax
In Java we have a predefined constant in Math class of java.lang package i.e. Math.PI which gives us the pie value which is approximately equal to 3.14159265359.
Following is the syntax for that
Math.PI
To get the power of any number raised to the power of another number in Java we have inbuilt java.lang.Math.pow() method.
Following is the syntax to get power of 2 by using the method ?
double power = math.pow (inputValue,2)
Algorithm
Step 1 ? Get the radius of the hemisphere either by initialization or by user input.
Step 2 ? Find the volume of the hemisphere by using the volume formula.
Step 3 ? Print the result.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Input
By Using User Input
By Using User Defined Method
Let's see the program along with its output one by one.
Approach-1: By Using Static Input
In this approach, the radius value of the hemisphere will be initialized in the program. Then by using the algorithm we will find the volume.
Example
public class Main { //main method public static void main(String[] args) { //declared a double variable 'radius' //and initialized with radius value double radius = 6; //printing the given radius value of hemisphere System.out.println("Given radius of hemisphere : "+radius); //calculate volume by using formula double volume = (2 * Math.PI * radius * radius * radius) / 3; //print the result System.out.println("Volume of Hemisphere is : " + volume); } }
Output
Given radius of hemisphere : 6.0 Volume of Hemisphere is : 452.3893421169302
Approach-2: By Using User Input Value
In this approach, the user will be asked to take the input of the radius value of the cone. Then by using the volume formula of the cone, find the volume. Here we will make use of the Java inbuilt pow() method.
Example
public class Main { //main method public static void main(String[] args) { //declared a double variable 'radius' //and initialized with radius value double radius = 5.5; //printing the given radius value of hemisphere System.out.println("Given radius of hemisphere : "+radius); //calculate volume by using formula double volume = (2 * Math.PI * Math.pow(radius,2) * radius) / 3; //print the result System.out.println("Volume of Hemisphere is : " + volume); } }
Output
Given radius of hemisphere : 5.5 Volume of Hemisphere is : 348.45498516066795
Approach-3: By Using User Defined Method
In this approach, the radius value of the hemisphere will be initialized in the program. Then we call a user defined method to find the volume by passing the radius value of the hemisphere as a parameter. Then inside the method by using the volume formula of the, find the volume of the hemisphere.
Example
public class Main { //main method public static void main(String[] args) { //declared a double variable 'radius' //and initialized with radius value double radius = 5.5; //printing the given radius value of hemisphere System.out.println("Given radius of hemisphere : "+radius); //calling the method findVolume(radius); } //user defined method to find volume of hemisphere public static void findVolume(double radius) { //calculate volume by using formula double volume = (2 * Math.PI * Math.pow(radius,2) * radius) / 3; //print the result System.out.println("Volume of Hemisphere is : " + volume); } }
Output
Given radius of hemisphere : 5.5 Volume of Hemisphere is : 348.45498516066795
In this article, we explored how to find the volume of a hemisphere in Java by using different approaches.