Repository: TheAlgorithms/Java
Pull request #3655 (2022-10-25 10:55:51)

Added function and fixed bug in PerfectCube.java
Added another function to PerfectCube.java and fixed a testing mistake in line number 9

<!-- For completed items, change [ ] to [x] -->

- [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] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [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] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


siriak: @debasishbsws please review

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/PerfectCube.java b/src/main/java/com/thealgorithms/maths/PerfectCube.java
index 820b1720..0fae7889 100644
--- a/src/main/java/com/thealgorithms/maths/PerfectCube.java
+++ b/src/main/java/com/thealgorithms/maths/PerfectCube.java
@@ -5,15 +5,6 @@ package com.thealgorithms.maths;
  */
 public class PerfectCube {
 
-    public static void main(String[] args) {
-        assert !isPerfectCube(-1);
-        assert isPerfectCube(0);
-        assert isPerfectCube(1);
-        assert !isPerfectCube(4);
-        assert isPerfectCube(8);
-        assert isPerfectCube(27);
-    }
-
     /**
      * Check if a number is perfect cube or not
      *
@@ -22,7 +13,20 @@ public class PerfectCube {
      * {@code false}
      */
     public static boolean isPerfectCube(int number) {
+        number = Math.abs(number); // converting negative number to positive number
         int a = (int) Math.pow(number, 1.0 / 3);
         return a * a * a == number;
     }
+    
+    /**
+     * Check if a number is perfect cube or not by using Math.cbrt function
+     *
+     * @param number number to check
+     * @return {@code true} if {@code number} is perfect cube, otherwise
+     * {@code false}
+     */
+    public static boolean isPerfectCubeMathCbrt(int number) {
+        double cubeRoot = Math.cbrt(number);
+        return cubeRoot == (int) cubeRoot;
+    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java b/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java
new file mode 100644
index 00000000..806f5493
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java
@@ -0,0 +1,33 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+class PerfectCubeTest {
+
+    @Test
+    public void perfectCube() {
+
+        Assertions.assertTrue(PerfectCube.isPerfectCube(-27));
+        Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(-27));
+        Assertions.assertTrue(PerfectCube.isPerfectCube(-1));
+        Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(-1));
+        Assertions.assertTrue(PerfectCube.isPerfectCube(0));
+        Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(0));
+        Assertions.assertTrue(PerfectCube.isPerfectCube(1));
+        Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(1));
+        Assertions.assertTrue(PerfectCube.isPerfectCube(8));
+        Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(8));
+        Assertions.assertTrue(PerfectCube.isPerfectCube(27));
+        Assertions.assertTrue(PerfectCube.isPerfectCubeMathCbrt(27));
+
+        Assertions.assertFalse(PerfectCube.isPerfectCube(-9));
+        Assertions.assertFalse(PerfectCube.isPerfectCubeMathCbrt(-9));
+        Assertions.assertFalse(PerfectCube.isPerfectCube(2));
+        Assertions.assertFalse(PerfectCube.isPerfectCubeMathCbrt(2));
+        Assertions.assertFalse(PerfectCube.isPerfectCube(4));
+        Assertions.assertFalse(PerfectCube.isPerfectCubeMathCbrt(4));
+        Assertions.assertFalse(PerfectCube.isPerfectCube(30));
+        Assertions.assertFalse(PerfectCube.isPerfectCubeMathCbrt(30));
+    }
+}