-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVolume.java
120 lines (100 loc) · 3.17 KB
/
Volume.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.thealgorithms.maths;
/* Find volume of various shapes.*/
public class Volume {
public static void main(String[] args) {
/* test cube */
assert Double.compare(volumeCube(7), 343.0) == 0;
/* test cuboid */
assert Double.compare(volumeCuboid(2, 5, 7), 70.0) == 0;
/* test sphere */
assert Double.compare(volumeSphere(5), 523.5987755982989) == 0;
/* test cylinder */
assert Double.compare(volumeCylinder(1, 2), 12.566370614359172) == 0;
/* test hemisphere */
assert Double.compare(volumeHemisphere(5), 261.79938779914943) == 0;
/* test cone */
assert Double.compare(volumeCone(5, 7), 916.297857297023) == 0;
/*test prism*/
assert Double.compare(volumePrism(10, 2), 20.0) == 0;
/*test pyramid*/
assert Double.compare(volumePyramid(10, 3), 10.0) == 0;
}
/**
* Calculate the volume of a cube.
*
* @param sideLength side length of cube
* @return volume of given cube
*/
private static double volumeCube(double sidelength) {
return sidelength * sidelength * sidelength;
}
/**
* Calculate the volume of a cuboid.
*
* @param width of cuboid
* @param height of cuboid
* @param length of cuboid
* @return volume of given cuboid
*/
private static double volumeCuboid(double width, double height, double length) {
return width * height * length;
}
/**
* Calculate the volume of a sphere.
*
* @param radius radius of sphere
* @return volume of given sphere
*/
private static double volumeSphere(double radius) {
return 4 / 3 * Math.PI * radius * radius * radius;
}
/**
* Calculate volume of a cylinder
*
* @param radius radius of the floor
* @param height height of the cylinder.
* @return volume of given cylinder
*/
private static double volumeCylinder(double radius, double height) {
return Math.PI * radius * radius * height;
}
/**
* Calculate the volume of a hemisphere.
*
* @param radius radius of hemisphere
* @return volume of given hemisphere
*/
private static double volumeHemisphere(double radius) {
return 2 / 3 * Math.PI * radius * radius * radius;
}
/**
* Calculate the volume of a cone.
*
* @param radius radius of cone.
* @param height of cone.
* @return volume of given cone.
*/
private static double volumeCone(double radius, double height) {
return Math.PI * radius * radius * height / 3;
}
/**
* Calculate the volume of a prism.
*
* @param area of the base.
* @param height of prism.
* @return volume of given prism.
*/
private static double volumePrism(double basearea, double height) {
return basearea * height;
}
/**
* Calculate the volume of a pyramid.
*
* @param area of the base.
* @param height of pyramid.
* @return volume of given pyramid.
*/
private static double volumePyramid(double basearea, double height) {
return basearea * height / 3;
}
}