Kaynağa Gözat

Merge d0b61371c922e9a150d06b55eb29e58ee6385621 into 24118cb7b3c6ce6a0b3be331c7987add797baa81

Lawrence Wu 6 yıl önce
ebeveyn
işleme
4903bd4606
No account linked to committer's email

+ 45
- 6
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Dosyayı Görüntüle

@@ -3,17 +3,40 @@ 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;
7
-    }
6
+        StringBuilder evenNum = new StringBuilder();
8 7
 
8
+        for (int i = start; i < stop; i++){
9
+            if (i % 2 == 0){
10
+                evenNum.append(i);
11
+            }
12
+
13
+        }
14
+        return evenNum.toString();
15
+    }
9 16
 
10 17
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
18
+        StringBuilder oddNum = new StringBuilder();
19
+
20
+        for (int i = start; i < stop; i++){
21
+            if (i % 2 != 0){
22
+                oddNum.append(i);
23
+            }
24
+        }
25
+        return oddNum.toString();
12 26
     }
13 27
 
14 28
 
15 29
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
30
+        StringBuilder squareNum = new StringBuilder();
31
+
32
+        for (int i = start; i < stop; i = i + step){
33
+            if (i < stop){
34
+              int square = (int) Math.pow(i,2);
35
+                squareNum.append(square);
36
+            }
37
+
38
+        }
39
+        return squareNum.toString();
17 40
     }
18 41
 
19 42
     public static String getRange(int start) {
@@ -26,11 +49,27 @@ public class NumberUtilities {
26 49
 
27 50
 
28 51
     public static String getRange(int start, int stop, int step) {
29
-        return null;
52
+
53
+        StringBuilder range = new StringBuilder();
54
+
55
+        for (int i = start; i < stop; i += step){
56
+            if (i < stop){
57
+                range.append(i);
58
+            }
59
+        }
60
+        return range.toString();
30 61
     }
31 62
 
32 63
 
33 64
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
65
+        StringBuilder expoNum = new StringBuilder();
66
+
67
+        for (int i = start; i < stop; i += step){
68
+            if (i < stop){
69
+                int expo = (int) Math.pow(i,exponent);
70
+                expoNum.append(expo);
71
+            }
72
+        }
73
+        return expoNum.toString();
35 74
     }
36 75
 }

+ 17
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Dosyayı Görüntüle

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

+ 41
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java Dosyayı Görüntüle

@@ -3,18 +3,55 @@ 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 smallTri = new StringBuilder();
7
+
8
+        String star = "*";
9
+
10
+        for (int i = 1; i < numberOfRows; i++){
11
+            for (int j = 0; j < i; j++) {
12
+                smallTri.append(star);
13
+            }
14
+            smallTri.append("\n");
15
+        }
16
+        return smallTri.toString();
7 17
     }
8 18
 
9 19
     public static String getRow(int numberOfStars) {
10
-        return null;
20
+        StringBuilder stars = new StringBuilder();
21
+
22
+        String asterisk = "*";
23
+
24
+        for (int i = 0; i < numberOfStars; i++){
25
+            stars.append(asterisk);
26
+        }
27
+        return stars.toString();
11 28
     }
12 29
 
13 30
     public static String getSmallTriangle() {
14
-        return null;
31
+        StringBuilder smallTri = new StringBuilder();
32
+
33
+        String star = "*";
34
+
35
+        for (int i = 1; i < 5; i++){
36
+            for (int j = 0; j < i; j++) {
37
+                smallTri.append(star);
38
+            }
39
+        smallTri.append("\n");
40
+        }
41
+        return smallTri.toString();
15 42
     }
16 43
 
17 44
     public static String getLargeTriangle() {
18
-        return null;
45
+        StringBuilder smallTri = new StringBuilder();
46
+
47
+        String star = "*";
48
+
49
+        for (int i = 1; i < 10; i++){
50
+            for (int j = 0; j < i; j++) {
51
+                smallTri.append(star);
52
+            }
53
+            smallTri.append("\n");
54
+        }
55
+        return smallTri.toString();
19 56
     }
20 57
 }

+ 4
- 4
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java Dosyayı Görüntüle

@@ -171,21 +171,21 @@ public class NumberUtilitiesTest {
171 171
 
172 172
 
173 173
     @Test
174
-    public void testGetEvenNumbers() {
174
+    public void testGetOddNumbers() {
175 175
         // : Given
176 176
         String expected = "5791113151719";
177 177
         int start = 5;
178 178
         int stop = 20;
179 179
 
180 180
         // : When
181
-        String actual = NumberUtilities.getEvenNumbers(start, stop);
181
+        String actual = NumberUtilities.getOddNumbers(start, stop);
182 182
 
183 183
         // : Then
184 184
         Assert.assertEquals(expected, actual);
185 185
     }
186 186
 
187 187
     @Test
188
-    public void testGetOddNumbers() {
188
+    public void testGetEvenNumbers() {
189 189
         // : Given
190 190
         String expected = "681012141618";
191 191
         int start = 5;
@@ -193,7 +193,7 @@ public class NumberUtilitiesTest {
193 193
         int step = 5;
194 194
 
195 195
         // : When
196
-        String actual = NumberUtilities.getOddNumbers(start, stop);
196
+        String actual = NumberUtilities.getEvenNumbers(start, stop);
197 197
 
198 198
         // : Then
199 199
         Assert.assertEquals(expected, actual);