Skip to content

Commit bc6510e

Browse files
shitkodersiriak
andauthored
Add volume formulas (#2805)
Co-authored-by: Andrii Siriak <[email protected]>
1 parent b0ccec9 commit bc6510e

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Maths/Volume.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package Maths;
2+
3+
4+
/* Find volume of various shapes.*/
5+
public class Volume {
6+
public static void main(String[] args) {
7+
8+
/* test cube */
9+
assert Double.compare(volumeCube(7), 343.0) == 0;
10+
11+
/* test cuboid */
12+
assert Double.compare(volumeCuboid(2, 5, 7), 70.0) == 0;
13+
14+
/* test sphere */
15+
assert Double.compare(volumeSphere(5), 523.5987755982989) == 0;
16+
17+
/* test cylinder */
18+
assert Double.compare(volumeCylinder(1,2), 12.566370614359172) == 0;
19+
20+
/* test hemisphere */
21+
assert Double.compare(volumeHemisphere(5), 261.79938779914943) == 0;
22+
23+
/* test cone */
24+
assert Double.compare(volumeCone(5, 7), 916.297857297023) == 0;
25+
26+
}
27+
28+
29+
/**
30+
* Calculate the volume of a cube.
31+
*
32+
* @param sideLength side length of cube
33+
* @return volume of given cube
34+
*/
35+
private static double volumeCube(double sidelength) {
36+
return sidelength * sidelength * sidelength;
37+
}
38+
39+
/**
40+
* Calculate the volume of a cuboid.
41+
*
42+
* @param width of cuboid
43+
* @param height of cuboid
44+
* @param length of cuboid
45+
* @return volume of given cuboid
46+
*/
47+
private static double volumeCuboid(double width, double height, double length) {
48+
return width * height * length;
49+
}
50+
51+
/**
52+
* Calculate the volume of a sphere.
53+
*
54+
* @param radius radius of sphere
55+
* @return volume of given sphere
56+
*/
57+
private static double volumeSphere(double radius) {
58+
return 4 / 3 * Math.PI * radius * radius * radius;
59+
}
60+
61+
/**
62+
* Calculate volume of a cylinder
63+
*
64+
* @param radius radius of the floor
65+
* @param height height of the cylinder.
66+
* @return volume of given cylinder
67+
*/
68+
private static double volumeCylinder(double radius, double height) {
69+
return Math.PI * radius * radius * height;
70+
}
71+
72+
/**
73+
* Calculate the volume of a hemisphere.
74+
*
75+
* @param radius radius of hemisphere
76+
* @return volume of given hemisphere
77+
*/
78+
private static double volumeHemisphere(double radius) {
79+
return 2 / 3 * Math.PI * radius * radius * radius;
80+
}
81+
82+
/**
83+
* Calculate the volume of a cone.
84+
*
85+
* @param radius radius of cone.
86+
* @param height of cone.
87+
* @return volume of given cone.
88+
*/
89+
private static double volumeCone(double radius, double height) {
90+
return Math.PI * radius * radius * height / 3;
91+
}
92+
}

0 commit comments

Comments
 (0)