浏览代码

Merge 05084dd3a04af8c11d8a052796bfd824aba39f16 into 6026e5f68429fdeebffb1a314db6451e44f108c4

dcamac 6 年前
父节点
当前提交
2623c3b62e
没有帐户链接到提交者的电子邮件

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

@@ -5,7 +5,11 @@ import java.util.Scanner;
5 5
 /**
6 6
  * Created by leon on 1/31/18.
7 7
  */
8
+
9
+// saved
8 10
 public class MainApp {
9 11
     public static void main(String[] args) {
12
+
13
+
10 14
     }
11 15
 }

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

@@ -3,17 +3,47 @@ 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
+
7
+        StringBuilder myString = new StringBuilder();
8
+
9
+
10
+        for (int i = start; i < stop; i += 2) {
11
+            myString.append(i);
12
+        }
13
+
14
+        return myString.toString();
15
+
16
+
7 17
     }
8 18
 
9 19
 
10 20
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
21
+
22
+        StringBuilder myString = new StringBuilder();
23
+
24
+
25
+        for (int i = start; i < stop; i += 2) {
26
+            myString.append(i);
27
+        }
28
+
29
+        return myString.toString();
30
+
12 31
     }
13 32
 
14 33
 
15 34
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
35
+
36
+
37
+        StringBuilder myString = new StringBuilder();
38
+
39
+
40
+        for (int i = start; i < stop; i += step) {
41
+            myString.append(i * i);
42
+
43
+        }
44
+
45
+        return myString.toString();
46
+
17 47
     }
18 48
 
19 49
     public static String getRange(int start) {
@@ -26,11 +56,28 @@ public class NumberUtilities {
26 56
 
27 57
 
28 58
     public static String getRange(int start, int stop, int step) {
29
-        return null;
59
+
60
+        StringBuilder myString = new StringBuilder();
61
+
62
+        for (int i = start; i < stop; i += step) {
63
+            myString.append(i);
64
+        }
65
+
66
+        return myString.toString();
30 67
     }
31 68
 
32 69
 
33 70
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
71
+
72
+        StringBuilder myString = new StringBuilder();
73
+
74
+
75
+        for (int i = start; i < stop; i += step) {
76
+            myString.append((long)(Math.pow(i, 2)));
77
+
78
+        }
79
+
80
+        return myString.toString();
81
+
35 82
     }
36 83
 }

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

@@ -2,14 +2,63 @@ 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 myString = new StringBuilder();
7
+
8
+        for (int i = 1; i < 6; i++) {
9
+
10
+            for (int j = 1; j < 6; j++) {
11
+
12
+                myString.append(String.format("%3d |", i * j));
13
+
14
+
15
+            }
16
+
17
+            myString.append("\n");
18
+
19
+        }
20
+
21
+        return myString.toString();
22
+
6 23
     }
7 24
 
8 25
     public static String getLargeMultiplicationTable() {
9
-        return null;
26
+
27
+            StringBuilder myString = new StringBuilder();
28
+
29
+            for (int i = 1; i < 11; i++) {
30
+
31
+                for (int j = 1; j < 11; j++) {
32
+
33
+                    myString.append(String.format("%3d |", i*j));
34
+
35
+                }
36
+
37
+                myString.append("\n");
38
+
39
+            }
40
+
41
+            return myString.toString();
42
+
10 43
     }
11 44
 
12 45
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
46
+
47
+        StringBuilder myString = new StringBuilder();
48
+
49
+        for (int i = 1; i <= tableSize; i++) {
50
+
51
+            for (int j = 1; j <= tableSize; j++) {
52
+
53
+                myString.append(String.format("%3d |", i*j));
54
+
55
+            }
56
+
57
+            myString.append("\n");
58
+
59
+        }
60
+
61
+        return myString.toString();
62
+
14 63
     }
15 64
 }

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

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

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

@@ -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;