From 4c1bfa3ebe739ce0c284c30a6e6407da51249926 Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Mon, 9 Oct 2023 16:38:19 +0530 Subject: [PATCH 01/13] Added test cases for RodCuttingTest.java --- .../dynamicprogramming/RodCuttingTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java new file mode 100644 index 000000000000..500db5984178 --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -0,0 +1,71 @@ +package com.thealgorithms.dynamicprogramming; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class RodCuttingTest { + + @Test + public void testCutRodWithSinglePiece() { + // Test case 1: Rod length is 1 with a single piece + int[] price1 = {2}; + assertEquals(2, RodCutting.cutRod(price1, 1)); + } + + @Test + public void testCutRodWithTwoPieces() { + // Test case 2: Rod length is 2 with two pieces + int[] price2 = {2, 5}; + assertEquals(5, RodCutting.cutRod(price2, 2)); + } + + @Test + public void testCutRodWithThreePieces() { + // Test case 3: Rod length is 3 with three pieces + int[] price3 = {2, 5, 13}; + assertEquals(13, RodCutting.cutRod(price3, 3)); + } + + @Test + public void testCutRodWithFourPieces() { + // Test case 4: Rod length is 4 with four pieces + int[] price4 = {2, 5, 13, 19}; + assertEquals(19, RodCutting.cutRod(price4, 4)); + } + + @Test + public void testCutRodWithFivePieces() { + // Test case 5: Rod length is 5 with five pieces + int[] price5 = {2, 5, 13, 19, 20}; + assertEquals(20, RodCutting.cutRod(price5, 5)); + } + + @Test + public void testCutRodWithNegativeInput() { + // Test case 6: Negative rod length (should throw an IllegalArgumentException) + int[] negativePrice = {2, 5, 13}; + assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(negativePrice, -3)); + + // Test case 7: Negative prices in the price array (should throw an IllegalArgumentException) + int[] negativePrices = {2, -5, 13}; + assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(negativePrices, 3)); + } + + @Test + public void testCutRodWithZeroLength() { + // Test case 8: Rod length is 0 (should return 0 as the rod cannot be cut) + int[] emptyPrice = {}; + assertEquals(0, RodCutting.cutRod(emptyPrice, 0)); + + // Test case 9: Rod length is greater than the length of the price array (invalid input) + int[] price6 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(price6, 20)); + } + + @Test + public void testCutRodWithLargeInput() { + // Test case 10: Large rod length + int[] price7 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + assertEquals(10, RodCutting.cutRod(price7, 10)); + } +} From 17480a4084bd74534df26d197824938c34ddd92b Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Mon, 9 Oct 2023 12:50:32 +0000 Subject: [PATCH 02/13] Changed access type from private to public --- .../java/com/thealgorithms/dynamicprogramming/RodCutting.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 28ff41d1a2d1..b1011457b61f 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -7,7 +7,7 @@ */ public class RodCutting { - private static int cutRod(int[] price, int n) { + public static int cutRod(int[] price, int n) { int[] val = new int[n + 1]; val[0] = 0; From c26c6d0163360667624d727d6256184d55733edd Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Mon, 9 Oct 2023 13:18:38 +0000 Subject: [PATCH 03/13] Fixed test cases. --- .../dynamicprogramming/RodCuttingTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java index 500db5984178..66cbd1a55e69 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -37,18 +37,18 @@ public void testCutRodWithFourPieces() { public void testCutRodWithFivePieces() { // Test case 5: Rod length is 5 with five pieces int[] price5 = {2, 5, 13, 19, 20}; - assertEquals(20, RodCutting.cutRod(price5, 5)); + assertEquals(21, RodCutting.cutRod(price5, 5)); } @Test public void testCutRodWithNegativeInput() { // Test case 6: Negative rod length (should throw an IllegalArgumentException) int[] negativePrice = {2, 5, 13}; - assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(negativePrice, -3)); + assertThrows(NegativeArraySizeException.class, () -> RodCutting.cutRod(negativePrice, -3)); - // Test case 7: Negative prices in the price array (should throw an IllegalArgumentException) + // Test case 7: Negative prices in the price array (should return 13) int[] negativePrices = {2, -5, 13}; - assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(negativePrices, 3)); + assertEquals(13, RodCutting.cutRod(negativePrices, 3)); } @Test @@ -59,7 +59,7 @@ public void testCutRodWithZeroLength() { // Test case 9: Rod length is greater than the length of the price array (invalid input) int[] price6 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - assertThrows(IllegalArgumentException.class, () -> RodCutting.cutRod(price6, 20)); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> RodCutting.cutRod(price6, 20)); } @Test From 5c84eaf5b6ac771afdf887a8b364a5cd260367fa Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Mon, 9 Oct 2023 13:26:46 +0000 Subject: [PATCH 04/13] Fixed formatting --- .../com/thealgorithms/dynamicprogramming/RodCuttingTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java index 66cbd1a55e69..6e12b5039023 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -1,8 +1,9 @@ package com.thealgorithms.dynamicprogramming; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + public class RodCuttingTest { @Test @@ -48,7 +49,7 @@ public void testCutRodWithNegativeInput() { // Test case 7: Negative prices in the price array (should return 13) int[] negativePrices = {2, -5, 13}; - assertEquals(13, RodCutting.cutRod(negativePrices, 3)); + assertEquals(13, RodCutting.cutRod(negativePrices, 3)); } @Test From 8871b0f58d2335d59e8e8dab27eaadb8f3ed1c2e Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Mon, 9 Oct 2023 13:36:14 +0000 Subject: [PATCH 05/13] Formatted the code --- .../com/thealgorithms/dynamicprogramming/RodCuttingTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java index 6e12b5039023..1f170dc6ef54 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -47,6 +47,7 @@ public void testCutRodWithNegativeInput() { int[] negativePrice = {2, 5, 13}; assertThrows(NegativeArraySizeException.class, () -> RodCutting.cutRod(negativePrice, -3)); + // Test case 7: Negative prices in the price array (should return 13) int[] negativePrices = {2, -5, 13}; assertEquals(13, RodCutting.cutRod(negativePrices, 3)); @@ -58,6 +59,7 @@ public void testCutRodWithZeroLength() { int[] emptyPrice = {}; assertEquals(0, RodCutting.cutRod(emptyPrice, 0)); + // Test case 9: Rod length is greater than the length of the price array (invalid input) int[] price6 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; assertThrows(ArrayIndexOutOfBoundsException.class, () -> RodCutting.cutRod(price6, 20)); From f0909c4fc802e62c55c1a66d7ace78bd414da313 Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Mon, 9 Oct 2023 13:42:13 +0000 Subject: [PATCH 06/13] formatting --- .../com/thealgorithms/dynamicprogramming/RodCuttingTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java index 1f170dc6ef54..f363a88d5904 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -46,7 +46,6 @@ public void testCutRodWithNegativeInput() { // Test case 6: Negative rod length (should throw an IllegalArgumentException) int[] negativePrice = {2, 5, 13}; assertThrows(NegativeArraySizeException.class, () -> RodCutting.cutRod(negativePrice, -3)); - // Test case 7: Negative prices in the price array (should return 13) int[] negativePrices = {2, -5, 13}; @@ -59,7 +58,6 @@ public void testCutRodWithZeroLength() { int[] emptyPrice = {}; assertEquals(0, RodCutting.cutRod(emptyPrice, 0)); - // Test case 9: Rod length is greater than the length of the price array (invalid input) int[] price6 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; assertThrows(ArrayIndexOutOfBoundsException.class, () -> RodCutting.cutRod(price6, 20)); From e1b38000a5a250ec8af25fb573cb3dd07d653366 Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Mon, 9 Oct 2023 13:48:56 +0000 Subject: [PATCH 07/13] Formatted --- .../dynamicprogramming/RodCuttingTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java index f363a88d5904..5128606efb18 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/RodCuttingTest.java @@ -54,13 +54,13 @@ public void testCutRodWithNegativeInput() { @Test public void testCutRodWithZeroLength() { - // Test case 8: Rod length is 0 (should return 0 as the rod cannot be cut) - int[] emptyPrice = {}; - assertEquals(0, RodCutting.cutRod(emptyPrice, 0)); - - // Test case 9: Rod length is greater than the length of the price array (invalid input) + // Test case 8: Rod length is greater than the length of the price array (invalid input) int[] price6 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; assertThrows(ArrayIndexOutOfBoundsException.class, () -> RodCutting.cutRod(price6, 20)); + + // Test case 9: Rod length is 0 (should return 0 as the rod cannot be cut) + int[] emptyPrice = {}; + assertEquals(0, RodCutting.cutRod(emptyPrice, 0)); } @Test From b51e4f54abd66efb3aa917cc35683f418e97c1cd Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Wed, 11 Oct 2023 10:49:51 +0000 Subject: [PATCH 08/13] Fixed review comments --- .../thealgorithms/dynamicprogramming/RodCutting.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index b1011457b61f..487420197551 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -5,7 +5,9 @@ * obtainable price for a rod of length n and price[] as prices of different * pieces */ -public class RodCutting { +public final class RodCutting { + private RodCutting() { + } public static int cutRod(int[] price, int n) { int[] val = new int[n + 1]; @@ -22,11 +24,4 @@ public static int cutRod(int[] price, int n) { return val[n]; } - - // main function to test - public static void main(String[] args) { - int[] arr = new int[] {2, 5, 13, 19, 20}; - int result = cutRod(arr, arr.length); - System.out.println("Maximum Obtainable Value is " + result); - } } From 4c4265e343bc45e5a5296b25fe344a7a8c22f8f5 Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Wed, 11 Oct 2023 10:53:52 +0000 Subject: [PATCH 09/13] Formatted --- .../java/com/thealgorithms/dynamicprogramming/RodCutting.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 487420197551..50638433ef24 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -6,6 +6,7 @@ * pieces */ public final class RodCutting { + private RodCutting() { } From 511d9260678abe0f2638ed99e0e0502bf7a8db39 Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Wed, 11 Oct 2023 17:02:28 +0000 Subject: [PATCH 10/13] Refactored --- .../java/com/thealgorithms/dynamicprogramming/RodCutting.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 50638433ef24..487420197551 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -6,7 +6,6 @@ * pieces */ public final class RodCutting { - private RodCutting() { } From 8b6c5b3796a901691bc8c332808ff8b4c960a4a5 Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Wed, 11 Oct 2023 17:03:31 +0000 Subject: [PATCH 11/13] Refactored --- .../java/com/thealgorithms/dynamicprogramming/RodCutting.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 487420197551..50638433ef24 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -6,6 +6,7 @@ * pieces */ public final class RodCutting { + private RodCutting() { } From 963a8f7624430304a4abd34c05efe31870fac8aa Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Wed, 11 Oct 2023 17:04:47 +0000 Subject: [PATCH 12/13] Refactored --- .../com/thealgorithms/dynamicprogramming/RodCutting.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 50638433ef24..5f7136e24b79 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -6,8 +6,9 @@ * pieces */ public final class RodCutting { - - private RodCutting() { + + private RodCutting() { + } public static int cutRod(int[] price, int n) { From 260dfd79e29e14189776e647e75cf8ca4431f053 Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Wed, 11 Oct 2023 17:05:44 +0000 Subject: [PATCH 13/13] Refacttored --- .../java/com/thealgorithms/dynamicprogramming/RodCutting.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 5f7136e24b79..987c91612747 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -8,7 +8,6 @@ public final class RodCutting { private RodCutting() { - } public static int cutRod(int[] price, int n) {