瀏覽代碼

Merge 6aa70e1f5adac67a94adeafdcdfe071bc803a7a7 into 24118cb7b3c6ce6a0b3be331c7987add797baa81

bell7692 6 年之前
父節點
當前提交
02af247fe9
沒有帳戶連結到提交者的電子郵件

二進制
.DS_Store 查看文件


+ 32
- 5
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java 查看文件

@@ -1,19 +1,36 @@
1 1
 package io.zipcoder.microlabs.mastering_loops;
2 2
 
3 3
 
4
+
5
+
4 6
 public class NumberUtilities {
7
+
8
+
5 9
     public static String getEvenNumbers(int start, int stop) {
6
-        return null;
10
+        StringBuilder myRange = new StringBuilder();
11
+        for (int i = start; i < stop; i+=2){
12
+            myRange.append(i);
13
+        }
14
+        return myRange.toString();
15
+
7 16
     }
8 17
 
9 18
 
10 19
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
20
+        StringBuilder myRange = new StringBuilder();
21
+        for (int i = start; i < stop; i+=2){
22
+            myRange.append(i);
23
+        }
24
+        return myRange.toString();
12 25
     }
13 26
 
14 27
 
15 28
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
29
+        StringBuilder myRange = new StringBuilder();
30
+        for (int i = start; i < stop; i+=step){
31
+            myRange.append((long)(Math.pow(i,2)));
32
+        }
33
+        return myRange.toString();
17 34
     }
18 35
 
19 36
     public static String getRange(int start) {
@@ -26,11 +43,21 @@ public class NumberUtilities {
26 43
 
27 44
 
28 45
     public static String getRange(int start, int stop, int step) {
29
-        return null;
46
+       StringBuilder myRange = new StringBuilder();
47
+        for (int i = start; i < stop; i+=step){
48
+            myRange.append(i);
49
+        }
50
+        return myRange.toString();
51
+
52
+
30 53
     }
31 54
 
32 55
 
33 56
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
57
+        StringBuilder myRange = new StringBuilder();
58
+        for (int i = start; i < stop; i+=step){
59
+            myRange.append((long)(Math.pow(i, exponent)));
60
+        }
61
+        return myRange.toString();
35 62
     }
36 63
 }

+ 42
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java 查看文件

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

+ 30
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java 查看文件

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

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java 查看文件

@@ -187,10 +187,10 @@ 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
-        int step = 5;
193
+
194 194
 
195 195
         // : When
196 196
         String actual = NumberUtilities.getOddNumbers(start, stop);