Bladeren bron

Merge ecb1913733e3fa876c45bc688a5bd42ea611c0cb into 24118cb7b3c6ce6a0b3be331c7987add797baa81

Madeline Bowe 6 jaren geleden
bovenliggende
commit
37190ae846
Geen account gekoppeld aan de committers e-mail

+ 42
- 5
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Bestand weergeven

@@ -3,17 +3,38 @@ package io.zipcoder.microlabs.mastering_loops;
3 3
 
4 4
 public class NumberUtilities {
5 5
     public static String getEvenNumbers(int start, int stop) {
6
-        return null;
6
+        StringBuilder sb  = new StringBuilder();
7
+        for (int i = start; i < stop; i++) {
8
+            if (i % 2 == 0) {
9
+                sb.append(i).toString();
10
+            }
11
+        }
12
+        return sb.toString();
7 13
     }
8 14
 
9 15
 
10 16
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
17
+        StringBuilder sb  = new StringBuilder();
18
+        for (int i = start; i < stop; i++) {
19
+            if (i % 2 != 0) {
20
+                sb.append(i).toString();
21
+            }
22
+        }
23
+        return sb.toString();
12 24
     }
13 25
 
14 26
 
15 27
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
28
+
29
+        StringBuilder sb  = new StringBuilder();
30
+
31
+        for (int i = start; i < stop; i += step) {
32
+            sb.append((int) Math.pow(i, 2));
33
+            }
34
+
35
+            return sb.toString();
36
+
37
+
17 38
     }
18 39
 
19 40
     public static String getRange(int start) {
@@ -26,11 +47,27 @@ public class NumberUtilities {
26 47
 
27 48
 
28 49
     public static String getRange(int start, int stop, int step) {
29
-        return null;
50
+
51
+        StringBuilder sb  = new StringBuilder();
52
+
53
+        for (int i = start; i < stop; i += step) {
54
+            sb.append(i).toString();
55
+        }
56
+
57
+        return sb.toString();
58
+
30 59
     }
31 60
 
32 61
 
62
+
33 63
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
64
+        StringBuilder sb  = new StringBuilder();
65
+
66
+        for (int i = start; i < stop; i += step) {
67
+            sb.append((int) Math.pow(i, exponent));
68
+        }
69
+
70
+        return sb.toString();
71
+
35 72
     }
36 73
 }

+ 45
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Bestand weergeven

@@ -2,14 +2,56 @@ package io.zipcoder.microlabs.mastering_loops;
2 2
 
3 3
 public class TableUtilities {
4 4
     public static String getSmallMultiplicationTable() {
5
-        return null;
5
+
6
+        StringBuilder builder = new StringBuilder();
7
+
8
+        for (int row = 1; row <= 5; row++) {
9
+            for(int col = 1; col <=5; col++) {
10
+                int table = row * col;
11
+                builder.append(String.format("%3d |", table));
12
+
13
+            }
14
+
15
+            builder.append("\n");
16
+
17
+        }
18
+
19
+        return builder.toString();
6 20
     }
7 21
 
8 22
     public static String getLargeMultiplicationTable() {
9
-        return null;
23
+
24
+        StringBuilder builder = new StringBuilder();
25
+
26
+        for (int row = 1; row <= 10; row++) {
27
+            for(int col = 1; col <=10; col++) {
28
+                int table = row * col;
29
+                builder.append(String.format("%3d |", table));
30
+
31
+            }
32
+
33
+            builder.append("\n");
34
+
35
+        }
36
+
37
+        return builder.toString();
10 38
     }
11 39
 
12 40
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
41
+
42
+        StringBuilder builder = new StringBuilder();
43
+
44
+        for (int row = 1; row <= tableSize; row++) {
45
+            for(int col = 1; col <= tableSize; col++) {
46
+                int table = row * col;
47
+                builder.append(String.format("%3d |", table));
48
+
49
+            }
50
+
51
+            builder.append("\n");
52
+
53
+        }
54
+
55
+        return builder.toString();
14 56
     }
15 57
 }

+ 14
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java Bestand weergeven

@@ -3,18 +3,28 @@ package io.zipcoder.microlabs.mastering_loops;
3 3
 public class TriangleUtilities {
4 4
 
5 5
     public static String getTriangle(int numberOfRows) {
6
-        return null;
6
+        StringBuilder builder = new StringBuilder();
7
+        for (int row = 0; row < numberOfRows; row++) {
8
+            builder.append(getRow(row));
9
+        }
10
+        return builder.toString();
7 11
     }
8 12
 
9 13
     public static String getRow(int numberOfStars) {
10
-        return null;
14
+        StringBuilder builder = new StringBuilder();
15
+
16
+        for (int i = 0; i <= numberOfStars; i++) {
17
+            builder.append("*");
18
+        }
19
+        builder.append("\n");
20
+        return builder.toString();
11 21
     }
12 22
 
13 23
     public static String getSmallTriangle() {
14
-        return null;
24
+        return getTriangle(4);
15 25
     }
16 26
 
17 27
     public static String getLargeTriangle() {
18
-        return null;
28
+        return getTriangle(9);
19 29
     }
20 30
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java Bestand weergeven

@@ -173,7 +173,7 @@ public class NumberUtilitiesTest {
173 173
     @Test
174 174
     public void testGetEvenNumbers() {
175 175
         // : Given
176
-        String expected = "5791113151719";
176
+        String expected = "681012141618";
177 177
         int start = 5;
178 178
         int stop = 20;
179 179
 
@@ -187,7 +187,7 @@ public class NumberUtilitiesTest {
187 187
     @Test
188 188
     public void testGetOddNumbers() {
189 189
         // : Given
190
-        String expected = "681012141618";
190
+        String expected = "5791113151719";
191 191
         int start = 5;
192 192
         int stop = 20;
193 193
         int step = 5;

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java Bestand weergeven

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