Repository: TheAlgorithms/Java
Pull request #5479 (2024-09-29 18:12:47)

Add Volume "Algorithm Frustum Of Cone" Then Test It.
Hey I've implemented FrustumOfCone Algorithm in **src/main/java/com.thealgorithms/maths/Volume.java**
with the test class in the test package

for enhancement https://github.com/TheAlgorithms/Java/issues/5478

**Algorithm Overview:**

1. Input Parameters: The method accepts three parameters:
     - r1: radius of the top circular base 
     - r2: radius of the bottom circular base 
     - height: height of the frustum

2. Volume Calculation: The method calculates the volume using the defined formula and returns the computed value as a double.

3. Unit Testing: Comprehensive unit tests have been implemented to validate the correctness of the calculation. The tests cover various scenarios to ensure that the function behaves as expected.

**Performance:**

- The time complexity of the volume calculation is O(1), as it involves a fixed number of arithmetic operations.
- This method provides an efficient way to compute the volume for practical applications, making it suitable for real-time calculations in engineering and design software.


- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5479?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 52.28%. Comparing base [(`1e8abf1`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/1e8abf1ddf257e9b1396a1156333ef948917416c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`f1fd8c1`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/f1fd8c1f66fd52cda1cef01162e4f1711010ba35?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5479      +/-   ##
============================================
- Coverage     52.29%   52.28%   -0.02%     
  Complexity     3256     3256              
============================================
  Files           525      525              
  Lines         15184    15185       +1     
  Branches       2887     2887              
============================================
- Hits           7940     7939       -1     
- Misses         6920     6921       +1     
- Partials        324      325       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5479?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/maths/Volume.java b/src/main/java/com/thealgorithms/maths/Volume.java
index 4b73f849..0f282b2a 100644
--- a/src/main/java/com/thealgorithms/maths/Volume.java
+++ b/src/main/java/com/thealgorithms/maths/Volume.java
@@ -90,4 +90,16 @@ public final class Volume {
     public static double volumePyramid(double baseArea, double height) {
         return (baseArea * height) / 3;
     }
+
+    /**
+     * Calculate the volume of a frustum of a cone.
+     *
+     * @param r1 radius of the top of the frustum
+     * @param r2 radius of the bottom of the frustum
+     * @param height height of the frustum
+     * @return volume of the frustum
+     */
+    public static double volumeFrustumOfCone(double r1, double r2, double height) {
+        return (Math.PI * height / 3) * (r1 * r1 + r2 * r2 + r1 * r2);
+    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/VolumeTest.java b/src/test/java/com/thealgorithms/maths/VolumeTest.java
index 1bdb3ae8..7cd0c671 100644
--- a/src/test/java/com/thealgorithms/maths/VolumeTest.java
+++ b/src/test/java/com/thealgorithms/maths/VolumeTest.java
@@ -32,5 +32,8 @@ public class VolumeTest {
 
         /* test pyramid */
         assertTrue(Volume.volumePyramid(10, 3) == 10.0);
+
+        /* test frustum */
+        assertTrue(Volume.volumeFrustumOfCone(3, 5, 7) == 359.188760060433);
     }
 }