How To Find Volume of the Octahedron in Java?


An octahedron is a three-dimensional shape which has eight plane faces. Simply, it is a polyhedron with eight faces, twelve edges and 6 vertices. It is derived from a Greek word i.e. ‘Oktaedron’ which means Eight Faced.

Formula to find volume of octahedron −

$$\mathrm{Volume\: =\: \sqrt{2}/3\: × \:a^3}$$

Where, ‘a’ refers to the side of the octahedron.

In this article we will see how we can find the volume of the octahedron in Java.

To show you some instances

Instance-1

Suppose the side length is 3

Then according to the volume formula of octahedron −

Volume = 12.72

Instance-2

Suppose the side length is 6

Then according to the volume formula of octahedron −

Volume = 101.82

Instance-3

Suppose the edge length is 4.5

Then according to the volume formula of dodecahedron −

Volume = 42.95

Syntax

To get the square root of a number we have inbuilt sqrt() method in the Math class of java.lang package.

Following is the syntax to get square root of any number by using the method

double squareRoot = Math.sqrt(input_vale)

Similarly, 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 3 by using the method

double power = Math.pow(inputValue,3)

Algorithm

  • Step 1 − Get the side length of the octahedron either by initialization or by user input.

  • Step 2 − Find the volume of the octahedron by using the volume formula.

  • Step 3 − Print the result.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value with Inbuilt Methods

In this approach the side length value of the octahedron will be declared in the program. Then by using the algorithm find the volume. Here we will make use of the inbuilt sqrt() and pow() method in the program.

Example

import java.util.*; public class Main{ //main method public static void main(String args[]){ //declared the side length of octahedron double a=3; System.out.println("The side of octahedron: "+a); //Find volume by using formula double volume= (Math.pow(a,3)*Math.sqrt(2))/3; //Print the result System.out.println("Volume of octahedron: " +volume); } }

Output

The side of octahedron: 3.0
Volume of octahedron: 12.727922061357857

Approach-2: By Using User Defined Method

In this approach the side length value of the octahedron will be declared in the program. Then call a user defined method by passing this length as parameter and inside the method find the volume by using the volume formula of octahedron.

Example

import java.util.*; public class Main{ //main method public static void main(String args[]){ //Declared the side length double a=10; System.out.println("The side of octahedron: "+a); //calling the method findVolume(a); } //user defined method to find volume of octahedron public static void findVolume(double a){ //Find volume by using formula double volume= (Math.pow(a,3)*Math.sqrt(2))/3; //Print the result System.out.println("Volume of octahedron: " +volume); } }

Output

The side of octahedron: 10.0
Volume of octahedron: 471.4045207910317

In this article, we explored how to find the volume of the octahedron in Java by using different approaches.

Updated on: 28-Oct-2022

121 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements