Browse Source

Cleaned up code

mbowe4 6 years ago
parent
commit
ecb1913733

+ 10
- 29
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java View File

4
 
4
 
5
     public static String getTriangle(int numberOfRows) {
5
     public static String getTriangle(int numberOfRows) {
6
         StringBuilder builder = new StringBuilder();
6
         StringBuilder builder = new StringBuilder();
7
-        for (int row = 1; row < numberOfRows; row++) {
8
-            for (int col = 0; col < row; col++) {
9
-                builder.append("*");
10
-            }
11
-            builder.append("\n");
7
+        for (int row = 0; row < numberOfRows; row++) {
8
+            builder.append(getRow(row));
12
         }
9
         }
13
-
14
         return builder.toString();
10
         return builder.toString();
15
-
16
     }
11
     }
17
 
12
 
18
     public static String getRow(int numberOfStars) {
13
     public static String getRow(int numberOfStars) {
19
-        String asterisk = "";
20
-        for (int i = 0; i < numberOfStars; i++) {
21
-            asterisk += "*";
14
+        StringBuilder builder = new StringBuilder();
15
+
16
+        for (int i = 0; i <= numberOfStars; i++) {
17
+            builder.append("*");
22
         }
18
         }
23
-        return asterisk;
19
+        builder.append("\n");
20
+        return builder.toString();
24
     }
21
     }
25
 
22
 
26
     public static String getSmallTriangle() {
23
     public static String getSmallTriangle() {
27
-        StringBuilder builder = new StringBuilder();
28
-        for (int row = 1; row <= 4; row++) {
29
-            for (int col = 0; col < row; col++) {
30
-                builder.append("*");
31
-            }
32
-            builder.append("\n");
33
-        }
34
-
35
-        return builder.toString();
24
+        return getTriangle(4);
36
     }
25
     }
37
 
26
 
38
     public static String getLargeTriangle() {
27
     public static String getLargeTriangle() {
39
-        StringBuilder builder = new StringBuilder();
40
-        for (int row = 1; row <= 9; row++) {
41
-            for (int col = 0; col < row; col++) {
42
-                builder.append("*");
43
-            }
44
-            builder.append("\n");
45
-        }
46
-
47
-        return builder.toString();
28
+        return getTriangle(9);
48
     }
29
     }
49
 }
30
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java View File

18
     @Test
18
     @Test
19
     public void getTriangleTest1() {
19
     public void getTriangleTest1() {
20
         String expected =
20
         String expected =
21
-                "*\n" +
21
+                        "*\n" +
22
                         "**\n" +
22
                         "**\n" +
23
                         "***\n" +
23
                         "***\n" +
24
                         "****\n" +
24
                         "****\n" +
27
                         "*******\n" +
27
                         "*******\n" +
28
                         "********\n" +
28
                         "********\n" +
29
                         "*********\n";
29
                         "*********\n";
30
-        String actual = TriangleUtilities.getTriangle(10);
30
+        String actual = TriangleUtilities.getTriangle(9);
31
         Assert.assertEquals(expected, actual);
31
         Assert.assertEquals(expected, actual);
32
     }
32
     }
33
 
33