diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/printtriangles/PrintTriangleExamples.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/printtriangles/PrintTriangleExamples.java index a67c54a922..156766f382 100644 --- a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/printtriangles/PrintTriangleExamples.java +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/printtriangles/PrintTriangleExamples.java @@ -4,7 +4,7 @@ import org.apache.commons.lang3.StringUtils; public class PrintTriangleExamples { - public static String printARightAngledTriangle(int N) { + public static String printARightTriangle(int N) { StringBuilder result = new StringBuilder(); for (int r = 1; r <= N; r++) { for (int j = 1; j <= r; j++) { @@ -29,6 +29,17 @@ public class PrintTriangleExamples { return result.toString(); } + public static String printAnIsoscelesTriangleUsingStringUtils(int N) { + StringBuilder result = new StringBuilder(); + + for (int r = 1; r <= N; r++) { + result.append(StringUtils.repeat(' ', N - r)); + result.append(StringUtils.repeat('*', 2 * r - 1)); + result.append(System.lineSeparator()); + } + return result.toString(); + } + public static String printAnIsoscelesTriangleUsingSubstring(int N) { StringBuilder result = new StringBuilder(); String helperString = StringUtils.repeat(' ', N - 1) + StringUtils.repeat('*', N * 2 - 1); @@ -41,8 +52,9 @@ public class PrintTriangleExamples { } public static void main(String[] args) { - System.out.println(printARightAngledTriangle(5)); + System.out.println(printARightTriangle(5)); System.out.println(printAnIsoscelesTriangle(5)); + System.out.println(printAnIsoscelesTriangleUsingStringUtils(5)); System.out.println(printAnIsoscelesTriangleUsingSubstring(5)); } diff --git a/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/printtriangles/PrintTriangleExamplesUnitTest.java b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/printtriangles/PrintTriangleExamplesUnitTest.java index 6fa6584b8e..97e99290c9 100644 --- a/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/printtriangles/PrintTriangleExamplesUnitTest.java +++ b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/printtriangles/PrintTriangleExamplesUnitTest.java @@ -9,9 +9,9 @@ import static org.junit.Assert.assertEquals; @RunWith(JUnitParamsRunner.class) public class PrintTriangleExamplesUnitTest { - - private static Object[][] rightAngledTriangles() { - String expected0 = ""; + + private static Object[][] rightTriangles() { + String expected0 = ""; String expected2 = "*" + System.lineSeparator() + "**" + System.lineSeparator(); @@ -39,9 +39,9 @@ public class PrintTriangleExamplesUnitTest { } @Test - @Parameters(method = "rightAngledTriangles") - public void whenPrintARightAngledTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) { - String actual = PrintTriangleExamples.printARightAngledTriangle(nrOfRows); + @Parameters(method = "rightTriangles") + public void whenPrintARightTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) { + String actual = PrintTriangleExamples.printARightTriangle(nrOfRows); assertEquals(expected, actual); } @@ -81,6 +81,14 @@ public class PrintTriangleExamplesUnitTest { assertEquals(expected, actual); } + + @Test + @Parameters(method = "isoscelesTriangles") + public void whenPrintAnIsoscelesTriangleUsingStringUtilsIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) { + String actual = PrintTriangleExamples.printAnIsoscelesTriangleUsingStringUtils(nrOfRows); + + assertEquals(expected, actual); + } @Test @Parameters(method = "isoscelesTriangles")