create triangles with for loops - examples (#7573)
This commit is contained in:
parent
8205672b29
commit
2c7c520a44
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ import static org.junit.Assert.assertEquals;
|
|||
@RunWith(JUnitParamsRunner.class)
|
||||
public class PrintTriangleExamplesUnitTest {
|
||||
|
||||
private static Object[][] rightAngledTriangles() {
|
||||
private static Object[][] rightTriangles() {
|
||||
String expected0 = "";
|
||||
|
||||
String expected2 = "*" + 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);
|
||||
}
|
||||
|
@ -82,6 +82,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")
|
||||
public void whenPrintAnIsoscelesTriangleUsingSubstringIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
|
||||
|
|
Loading…
Reference in New Issue