瀏覽代碼

Merge fe52ffbc4c9374b239c7165e27feb4e71b113339 into 24118cb7b3c6ce6a0b3be331c7987add797baa81

SupaGrammer 6 年之前
父節點
當前提交
14e1e358ae
沒有帳戶連結到提交者的電子郵件

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

@@ -5,7 +5,10 @@ import java.util.Scanner;
5 5
 /**
6 6
  * Created by leon on 1/31/18.
7 7
  */
8
-public class MainApp {
8
+public class MainApp extends TableUtilities{
9 9
     public static void main(String[] args) {
10
+        TableUtilities result = new TableUtilities();
11
+        System.out.print(result.getMultiplicationTable(20));
12
+
10 13
     }
11 14
 }

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

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

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

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

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

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

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

@@ -8,7 +8,7 @@ public class NumberUtilitiesTest {
8 8
     public void testGetRange1A() {
9 9
         // : Given
10 10
         String expected = "0123456789";
11
-        int stop = 11;
11
+        int stop = 10;
12 12
 
13 13
         // : When
14 14
         String actual = NumberUtilities.getRange(stop);
@@ -87,7 +87,7 @@ public class NumberUtilitiesTest {
87 87
     @Test
88 88
     public void testGetRange3B() {
89 89
         // : Given
90
-        String expected = "100101103104105106107108109";
90
+        String expected = "100101102103104105106107108109";
91 91
         int start = 100;
92 92
         int stop = 110;
93 93
 
@@ -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;

+ 5
- 3
src/test/java/io/zipcoder/microlabs/mastering_loops/TableUtilitiesTest.java 查看文件

@@ -7,6 +7,8 @@ import org.junit.Test;
7 7
  * Created by leon on 1/31/18.
8 8
  */
9 9
 public class TableUtilitiesTest {
10
+
11
+    TableUtilities table = new TableUtilities();
10 12
     @Test
11 13
     public void testGetLargeMultiplicationTable() {
12 14
         String expected =
@@ -21,7 +23,7 @@ public class TableUtilitiesTest {
21 23
                 "  9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 |\n" +
22 24
                 " 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 |100 |\n";
23 25
 
24
-        String actual = TableUtilities.getLargeMultiplicationTable();
26
+        String actual = table.getLargeMultiplicationTable();
25 27
         Assert.assertEquals(expected, actual);
26 28
     }
27 29
 
@@ -35,7 +37,7 @@ public class TableUtilitiesTest {
35 37
                         "  4 |  8 | 12 | 16 | 20 |\n" +
36 38
                         "  5 | 10 | 15 | 20 | 25 |\n";
37 39
 
38
-        String actual = TableUtilities.getSmallMultiplicationTable();
40
+        String actual = table.getSmallMultiplicationTable();
39 41
         Assert.assertEquals(expected, actual);
40 42
     }
41 43
 
@@ -62,7 +64,7 @@ public class TableUtilitiesTest {
62 64
                 " 18 | 36 | 54 | 72 | 90 |108 |126 |144 |162 |180 |198 |216 |234 |252 |270 |288 |306 |324 |342 |360 |\n" +
63 65
                 " 19 | 38 | 57 | 76 | 95 |114 |133 |152 |171 |190 |209 |228 |247 |266 |285 |304 |323 |342 |361 |380 |\n" +
64 66
                 " 20 | 40 | 60 | 80 |100 |120 |140 |160 |180 |200 |220 |240 |260 |280 |300 |320 |340 |360 |380 |400 |\n";
65
-        String actual = TableUtilities.getMultiplicationTable(20);
67
+        String actual = table.getMultiplicationTable(20);
66 68
         Assert.assertEquals(expected, actual);
67 69
     }
68 70