Skip to content

Commit 5ab1b6c

Browse files
TaranjeetSinghKalsiDebasish Biswas
and
Debasish Biswas
authored
Created VolumeTest.java & Fixed Bugs in Volume.java (TheAlgorithms#3682)
* Fixed functions and removed main() in Volume.java Fixed calculation errors in volumeSphere() and volumeHemisphere() functions and removed main() and changed functions access specifier from private to public * Created VolumeTest.java Created VolumeTest.java JUnit Tests for Volume.java Co-authored-by: Debasish Biswas <[email protected]>
1 parent bd267bb commit 5ab1b6c

File tree

2 files changed

+50
-45
lines changed

2 files changed

+50
-45
lines changed

src/main/java/com/thealgorithms/maths/Volume.java

+15-45
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,25 @@
33
/* Find volume of various shapes.*/
44
public class Volume {
55

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-
326
/**
337
* Calculate the volume of a cube.
348
*
359
* @param sideLength side length of cube
3610
* @return volume of given cube
3711
*/
38-
private static double volumeCube(double sidelength) {
12+
public static double volumeCube(double sidelength) {
3913
return sidelength * sidelength * sidelength;
4014
}
4115

4216
/**
4317
* Calculate the volume of a cuboid.
4418
*
45-
* @param width of cuboid
19+
* @param width of cuboid
4620
* @param height of cuboid
4721
* @param length of cuboid
4822
* @return volume of given cuboid
4923
*/
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) {
5525
return width * height * length;
5626
}
5727

@@ -61,8 +31,8 @@ private static double volumeCuboid(
6131
* @param radius radius of sphere
6232
* @return volume of given sphere
6333
*/
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;
6636
}
6737

6838
/**
@@ -72,7 +42,7 @@ private static double volumeSphere(double radius) {
7242
* @param height height of the cylinder.
7343
* @return volume of given cylinder
7444
*/
75-
private static double volumeCylinder(double radius, double height) {
45+
public static double volumeCylinder(double radius, double height) {
7646
return Math.PI * radius * radius * height;
7747
}
7848

@@ -82,8 +52,8 @@ private static double volumeCylinder(double radius, double height) {
8252
* @param radius radius of hemisphere
8353
* @return volume of given hemisphere
8454
*/
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;
8757
}
8858

8959
/**
@@ -93,29 +63,29 @@ private static double volumeHemisphere(double radius) {
9363
* @param height of cone.
9464
* @return volume of given cone.
9565
*/
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;
9868
}
9969

10070
/**
10171
* Calculate the volume of a prism.
10272
*
103-
* @param area of the base.
73+
* @param area of the base.
10474
* @param height of prism.
10575
* @return volume of given prism.
10676
*/
107-
private static double volumePrism(double basearea, double height) {
77+
public static double volumePrism(double basearea, double height) {
10878
return basearea * height;
10979
}
11080

11181
/**
11282
* Calculate the volume of a pyramid.
11383
*
114-
* @param area of the base.
84+
* @param area of the base.
11585
* @param height of pyramid.
11686
* @return volume of given pyramid.
11787
*/
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;
12090
}
12191
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class VolumeTest {
7+
8+
@Test
9+
public void volume() {
10+
11+
/* test cube */
12+
assertTrue(Volume.volumeCube(7) == 343.0);
13+
14+
/* test cuboid */
15+
assertTrue(Volume.volumeCuboid(2, 5, 7) == 70.0);
16+
17+
/* test sphere */
18+
assertTrue(Volume.volumeSphere(7) == 1436.7550402417319);
19+
20+
/* test cylinder */
21+
assertTrue(Volume.volumeCylinder(3, 7) == 197.92033717615698);
22+
23+
/* test hemisphere */
24+
assertTrue(Volume.volumeHemisphere(7) == 718.3775201208659);
25+
26+
/* test cone */
27+
assertTrue(Volume.volumeCone(3, 7) == 65.97344572538566);
28+
29+
/* test prism */
30+
assertTrue(Volume.volumePrism(10, 2) == 20.0);
31+
32+
/* test pyramid */
33+
assertTrue(Volume.volumePyramid(10, 3) == 10.0);
34+
}
35+
}

0 commit comments

Comments
 (0)