Browse Source

Merge 38ad53ef6ab553c7d9bcb30c0c15a30018fc7e9b into 24118cb7b3c6ce6a0b3be331c7987add797baa81

Brian 6 years ago
parent
commit
2eda4c7597
No account linked to committer's email

+ 26
- 5
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java View File

@@ -3,17 +3,30 @@ 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
+git
7
+        StringBuilder evensOnly = new StringBuilder();
8
+        for(int i = start; i <= stop; i++) {
9
+            if(i % 2 == 0) {
10
+                evensOnly.append(i);
11
+            }
12
+        }
13
+        return evensOnly.toString();
7 14
     }
8 15
 
9 16
 
10 17
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
18
+        StringBuilder oddsOnly = new StringBuilder();
19
+        for(int i = start; i <= stop; i++) {
20
+            if(i % 2 != 0) {
21
+                oddsOnly.append(i);
22
+            }
23
+        }
24
+        return oddsOnly.toString();
12 25
     }
13 26
 
14 27
 
15 28
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
29
+        return getExponentiations(start, stop, step, 2);
17 30
     }
18 31
 
19 32
     public static String getRange(int start) {
@@ -26,11 +39,19 @@ public class NumberUtilities {
26 39
 
27 40
 
28 41
     public static String getRange(int start, int stop, int step) {
29
-        return null;
42
+        StringBuilder range = new StringBuilder();
43
+        for(int i = start; i < stop; i += step) {
44
+            range.append(i);
45
+        }
46
+        return range.toString();
30 47
     }
31 48
 
32 49
 
33 50
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
51
+        StringBuilder exponentials = new StringBuilder();
52
+        for(int i = start; i < stop; i += step) {
53
+            exponentials.append((int)Math.pow(i, exponent));
54
+        }
55
+        return exponentials.toString();
35 56
     }
36 57
 }

+ 11
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java View File

@@ -2,14 +2,22 @@ package io.zipcoder.microlabs.mastering_loops;
2 2
 
3 3
 public class TableUtilities {
4 4
     public static String getSmallMultiplicationTable() {
5
-        return null;
5
+        return getMultiplicationTable(5);
6 6
     }
7 7
 
8 8
     public static String getLargeMultiplicationTable() {
9
-        return null;
9
+        return getMultiplicationTable(10);
10 10
     }
11 11
 
12 12
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
13
+        StringBuilder table = new StringBuilder();
14
+        for(int i = 1; i < tableSize + 1; ++i) {
15
+            for(int j = 1; j < tableSize + 1; ++j) {
16
+                String row = String.format("%3d |", i * j);
17
+                table.append(row);
18
+            }
19
+            table.append("\n");
20
+        }
21
+        return table.toString();
14 22
     }
15 23
 }

+ 13
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java View File

@@ -3,18 +3,27 @@ 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 triangle = new StringBuilder();
7
+        for (int i = 1; i < numberOfRows; i++) {
8
+            triangle.append(getRow(i));
9
+            triangle.append("\n");
10
+        }
11
+        return triangle.toString();
7 12
     }
8 13
 
9 14
     public static String getRow(int numberOfStars) {
10
-        return null;
15
+        StringBuilder rowOfStars = new StringBuilder();
16
+        for (int i = 0; i < numberOfStars; i++) {
17
+            rowOfStars.append("*");
18
+        }
19
+        return rowOfStars.toString();
11 20
     }
12 21
 
13 22
     public static String getSmallTriangle() {
14
-        return null;
23
+        return getTriangle(5);
15 24
     }
16 25
 
17 26
     public static String getLargeTriangle() {
18
-        return null;
27
+        return getTriangle(10);
19 28
     }
20 29
 }

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

@@ -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 = "68101214161820";
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;

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

@@ -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" +