Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbc6510e

Browse files
shitkodersiriak
andauthored
Add volume formulas (TheAlgorithms#2805)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parentb0ccec9 commitbc6510e

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+
packageMaths;
2+
3+
4+
/* Find volume of various shapes.*/
5+
publicclassVolume {
6+
publicstaticvoidmain(String[]args) {
7+
8+
/* test cube */
9+
assertDouble.compare(volumeCube(7),343.0) ==0;
10+
11+
/* test cuboid */
12+
assertDouble.compare(volumeCuboid(2,5,7),70.0) ==0;
13+
14+
/* test sphere */
15+
assertDouble.compare(volumeSphere(5),523.5987755982989) ==0;
16+
17+
/* test cylinder */
18+
assertDouble.compare(volumeCylinder(1,2),12.566370614359172) ==0;
19+
20+
/* test hemisphere */
21+
assertDouble.compare(volumeHemisphere(5),261.79938779914943) ==0;
22+
23+
/* test cone */
24+
assertDouble.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+
privatestaticdoublevolumeCube(doublesidelength) {
36+
returnsidelength *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+
privatestaticdoublevolumeCuboid(doublewidth,doubleheight,doublelength) {
48+
returnwidth *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+
privatestaticdoublevolumeSphere(doubleradius) {
58+
return4 /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+
privatestaticdoublevolumeCylinder(doubleradius,doubleheight) {
69+
returnMath.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+
privatestaticdoublevolumeHemisphere(doubleradius) {
79+
return2 /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+
privatestaticdoublevolumeCone(doubleradius,doubleheight) {
90+
returnMath.PI *radius *radius *height /3;
91+
}
92+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp