create triangles with for loops - examples (#7573)

This commit is contained in:
Catalin Burcea 2019-08-19 05:35:36 +03:00 committed by KevinGilmore
parent 8205672b29
commit 2c7c520a44
2 changed files with 28 additions and 8 deletions

View File

@ -4,7 +4,7 @@ import org.apache.commons.lang3.StringUtils;
public class PrintTriangleExamples { public class PrintTriangleExamples {
public static String printARightAngledTriangle(int N) { public static String printARightTriangle(int N) {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
for (int r = 1; r <= N; r++) { for (int r = 1; r <= N; r++) {
for (int j = 1; j <= r; j++) { for (int j = 1; j <= r; j++) {
@ -29,6 +29,17 @@ public class PrintTriangleExamples {
return result.toString(); 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) { public static String printAnIsoscelesTriangleUsingSubstring(int N) {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
String helperString = StringUtils.repeat(' ', N - 1) + StringUtils.repeat('*', N * 2 - 1); String helperString = StringUtils.repeat(' ', N - 1) + StringUtils.repeat('*', N * 2 - 1);
@ -41,8 +52,9 @@ public class PrintTriangleExamples {
} }
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(printARightAngledTriangle(5)); System.out.println(printARightTriangle(5));
System.out.println(printAnIsoscelesTriangle(5)); System.out.println(printAnIsoscelesTriangle(5));
System.out.println(printAnIsoscelesTriangleUsingStringUtils(5));
System.out.println(printAnIsoscelesTriangleUsingSubstring(5)); System.out.println(printAnIsoscelesTriangleUsingSubstring(5));
} }

View File

@ -10,7 +10,7 @@ import static org.junit.Assert.assertEquals;
@RunWith(JUnitParamsRunner.class) @RunWith(JUnitParamsRunner.class)
public class PrintTriangleExamplesUnitTest { public class PrintTriangleExamplesUnitTest {
private static Object[][] rightAngledTriangles() { private static Object[][] rightTriangles() {
String expected0 = ""; String expected0 = "";
String expected2 = "*" + System.lineSeparator() String expected2 = "*" + System.lineSeparator()
@ -39,9 +39,9 @@ public class PrintTriangleExamplesUnitTest {
} }
@Test @Test
@Parameters(method = "rightAngledTriangles") @Parameters(method = "rightTriangles")
public void whenPrintARightAngledTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) { public void whenPrintARightTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
String actual = PrintTriangleExamples.printARightAngledTriangle(nrOfRows); String actual = PrintTriangleExamples.printARightTriangle(nrOfRows);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@ -82,6 +82,14 @@ public class PrintTriangleExamplesUnitTest {
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test
@Parameters(method = "isoscelesTriangles")
public void whenPrintAnIsoscelesTriangleUsingStringUtilsIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
String actual = PrintTriangleExamples.printAnIsoscelesTriangleUsingStringUtils(nrOfRows);
assertEquals(expected, actual);
}
@Test @Test
@Parameters(method = "isoscelesTriangles") @Parameters(method = "isoscelesTriangles")
public void whenPrintAnIsoscelesTriangleUsingSubstringIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) { public void whenPrintAnIsoscelesTriangleUsingSubstringIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {