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